Quantcast
Channel: MEL PICBASIC Forum
Viewing all 4787 articles
Browse latest View live

LCD do display - but pins check out

$
0
0
Guys, I'm debugging the mistakes I made when prototyping a PCB. I say I made, the schematic tested out OK in the DRC check, but the board failed to pick up some nets and thus I'm missing a few tracks to some components. However I've trouble shouted through the board and The PIC is running fine, I have comms through an FTDI 232RL chip, and it reads the 18B20 sensor and an corresponding monitoring LED flashes away to confirm the code is running, but the LCD simply lights up with the backlight and doesn't display anything at all.

I've traced the routing for the 4 bit data lines using a multi-meter for continuity, and using a scope I get negative pulses on enable pin, and positive pulses on the R/W pin - these are around 1.2s frequency which seem slow given that the pic is running at 40 Mhz with a 10 Mhz xtal. There are no solid blocks regardless if the contrast is grounded or taken high (or 4k7 placed between it and the 5v rail). If I replace the PIC back on the development board it works fine ! and the schematic for the LCD was taken from the manual for the EasyPIC5 board - any suggestions or further testing I can do.

LAB-XUSB Experimenter Board help

$
0
0
Hello,

I bought the following product and am trying to run a sample program on it provided by their website. I have a PIC16F874A installed and can't seem to get the sample programs to work. All I get is a single row of solid blocks on the LCD.

Here is the original code:
Code:

' PICBASIC PRO program to display key number on LCD

' Define LOADER_USED to allow use of the boot loader.
' This will not affect normal program operation.
Define        LOADER_USED        1

' RESET_ORG can be set to move the BASIC program out of the way
' of any boot loader running from location 0, such as the
' Microchip USB boot loader
'Define        RESET_ORG        800h

Define        OSC        48                ' Core is running at 48MHz

' Define LCD connections
Define  LCD_DREG        PORTD
Define  LCD_DBIT        4
Define  LCD_RSREG      PORTE
Define  LCD_RSBIT      0
Define  LCD_EREG        PORTE
Define  LCD_EBIT        1


' Define program variables
col    var    byte            ' Keypad column
row    var    byte            ' Keypad row
key    var    byte            ' Key value


        INTCON2.7 = 0                ' Enable PORTB pullups
        ADCON1 = 15                ' Make PORTA and PORTE digital
        Low PORTE.2            ' LCD R/W low (write)
        Pause 100              ' Wait for LCD to start

        Lcdout $fe, 1, "Press any key"  ' Display sign on message

loop:  Gosub getkey            ' Get a key from the keypad
        Lcdout $fe, 1, #key    ' Display ASCII key number
        Goto loop              ' Do it forever


' Subroutine to get a key from keypad
getkey:
        Pause 50                ' Debounce

getkeyu:
        ' Wait for all keys up
        PORTB = 0              ' All output pins low
        TRISB = $f0            ' Bottom 4 pins out, top 4 pins in
        If ((PORTB >> 4) != $f) Then getkeyu    ' If any keys down, loop

        Pause 50                ' Debounce

getkeyp:
        ' Wait for keypress
        For col = 0 to 3        ' 4 columns in keypad
                PORTB = 0      ' All output pins low
                TRISB = (dcd col) ^ $ff ' Set one column pin to output
                Pauseus 1
                row = PORTB >> 4        ' Read row
                If row != $f Then gotkey        ' If any keydown, exit
        Next col

        Goto getkeyp            ' No keys down, go look again

gotkey: ' Change row and column to key number 1 - 16
        key = (col * 4) + (ncd (row ^ $f))
        Return                  ' Subroutine over

        End

Because PBP3 doesn't like it using the instruction Loop as a label, I changed it to Looper. Also changed INTCON2.7 = 0 to OPTION_REG.7 = 0 for the 874A and changed the define osc 48 to define osc 20.

I am new to this type of code and find it difficult to read, so the problem doesn't stand out for me. What I'm left with is this:

Code:

;----[16F874A Hardware Configuration]-------------------------------------------
#IF __PROCESSOR__ = "16F874A"
  #DEFINE MCU_FOUND 1
#CONFIG
cfg = _XT_OSC                ; XT oscillator
cfg&= _WDT_OFF                ; WDT disabled
cfg&= _PWRTE_OFF              ; PWRT disabled
cfg&= _BODEN_OFF              ; BOR disabled
cfg&= _LVP_OFF                ; RB3 is digital I/O, HV on MCLR must be used for programming
cfg&= _CPD_OFF                ; Data EEPROM code protection off
cfg&= _WRT_OFF                ; Write protection off; all program memory may be written to by EECON control
cfg&= _DEBUG_OFF              ; In-Circuit Debugger disabled, RB6 and RB7 are general purpose I/O pins
cfg&= _CP_OFF                ; Code protection off
  __CONFIG cfg

#ENDCONFIG

#ENDIF

;----[Verify Configs have been specified for Selected Processor]----------------
;      Note: Only include this routine once, after all #CONFIG blocks
#IFNDEF MCU_FOUND
  #ERROR "No CONFIGs found for [" + __PROCESSOR__ +"]"
#ENDIF

  define  osc 20                ' Core is running at 20MHz

' Define LCD connections
  Define  LCD_DREG        PORTD
  Define  LCD_DBIT        4
  Define  LCD_RSREG      PORTE
  Define  LCD_RSBIT      0
  Define  LCD_EREG        PORTE
  Define  LCD_EBIT        1


' Define program variables
  col    var    byte          ' Keypad column
  row    var    byte          ' Keypad row
  key    var    byte          ' Key value


        OPTION_REG.7 = 0            ' Enable PORTB pullups
        ADCON1 = 15                        ' Make PORTA and PORTE digital
        Low PORTE.2            ' LCD R/W low (write)
        Pause 100              ' Wait for LCD to start

        Lcdout $fe, 1, "Press any key"  ' Display sign on message

looper: Gosub getkey            ' Get a key from the keypad
        Lcdout $fe, 1, #key    ' Display ASCII key number
        Goto looper            ' Do it forever


' Subroutine to get a key from keypad
getkey:
        Pause 50                ' Debounce

getkeyu:
        ' Wait for all keys up
        PORTB = 0              ' All output pins low
        TRISB = $f0            ' Bottom 4 pins out, top 4 pins in
        If ((PORTB >> 4) != $f) Then getkeyu    ' If any keys down, loop

        Pause 50                ' Debounce

getkeyp:
        ' Wait for keypress
        For col = 0 to 3        ' 4 columns in keypad
                PORTB = 0      ' All output pins low
                TRISB = (dcd col) ^ $ff ' Set one column pin to output
                Pauseus 1
                row = PORTB >> 4        ' Read row
                If row != $f Then gotkey        ' If any keydown, exit
        Next col

        Goto getkeyp            ' No keys down, go look again

gotkey: ' Change row and column to key number 1 - 16
        key = (col * 4) + (ncd (row ^ $f))
        Return                  ' Subroutine over

        End

Does anything jump out to anyone? I'm just playing with this board and am trying to get it to work. It says "PIC18F4550 recommended" but lists the 16F874 as a compatible chip. Thanks in advance.

Tony

Product in question
Schematic and code links

AD conversion problem for pic16F684

$
0
0
Hello, I am starting with a first programm to test a AD-conversion.After the compiling to a HEX-file I do test the programm in REAL PIC SIMULATOR.
But nothing works, I do not see a AD value for the 2 ports !! I think even the hex file is not correct ! please help me, what is wrong ?? Here is my program:
Code:

'*  Name    : ADtest16F684.BAS                                      *
'*  Date    : 10-5-2015                                          *
'****************************************************************
#config
 __config _INTRC_OSC_NOCLKOUT & _WDT_OFF & _MCLRE_OFF & _CP_OFF
#ENDCONFIG
Define OSC 4          ' Match the crystal on the board
OSCCON = %01100101 'select intern clock 4 Mhz
TRISA = %00111111 'all inputs
TRISC = %00000000  'all outputs
Define ADC_bits 8
define ADC_sampleus 100
ADCON1 = %00110000 'interne ad-clock is ook define ad_clock 3
ADCON0 = %11001001 'normal left justified channel AN2= enabled
define ADC_CLOCK 3 'werkt misschien niet

CMCON0 = 7 'comparators off CxIN pins are dig I/O
PORTC.0 = 0 : PORTC.1 = 0:PORTC.2 = 0
PORTC.3 = 0 : PORTC.4 = 0:PORTC.5 = 0
shortad var WORD  'is de ingang AN0
longad var word    'is de ingang AN1
ANSEL = %00000100 'kies RA2 als analoog ingang
pause 10
ADCIN 2,shortAD    'short is 0 - 255 van AD shortAD ingang AN0
pause 100
ADCON0 = %11010001 'normal left justified channel AN4= enabled
ANSEL = %00010000  'kies RA4 als analoog ingang
pause 10
adcin 4,longAD      'long is 0 - 255 van AD longAD ingang AN1
pause 100

'shortAD = 138
PORTC.5 = 1
if shortAD < 45 then PORTC.0 = 1 : goto x7 
if shortAD < 90 then PORTC.1 = 1 : goto x7
if shortAD < 135 then PORTC.2 = 1 : goto x7
if shortAD < 180 then PORTC.3 = 1 : goto x7
if shortAD < 215 then PORTC.4 = 1 : goto x7
PORTC.5 = 1
x7:
end

Code for thermostat with 18b20

$
0
0
How to insert a setup for another temperature example, Set Temp2 for the second relay in this code? Thankes

Code:

tmp1    var byte
    tmp2    var byte
    Temperature    Var  Word      ' Temperature storage
    TempC        Var Word
    Float        Var Word
    TargetTemp      Var  Word      ' Desired Temperature
    Hyst        Var  Word      ' Hystereris
    V            Var  Word      ' Var. for display

    B1              Var Byte            ' Byte for TargetTemp calculation
    B2              Var Byte            ' Byte for TargetTemp calculation
 
    Count_Remain    Var Byte      ' Count remaining
    Count_Per_C    Var  Byte      ' Count per degree C
    Sign        Var  Byte      ' +/- sign
    Mode          Var  Byte      ' 0=Temp. display, 1=Set Temp, 2=Set Hysteresis

    Twist        Var Bit

    '*****************************************************************************
    ' Resolution
    '*****************************************************************************

    DS18B20_9bit    CON %00011111      ' 93.75ms, 0.5°C
    DS18B20_10bit    CON %00111111      ' 187.5ms, 0.25°C  <-- My favorite
    DS18B20_11bit    CON %01011111      ' 375ms,  0.125°C
    DS18B20_12bit    CON %01111111      ' 750ms,  0.0625°C  (default)

    DATA 46, 224, 20                        ' Temp MSB, TEMP LSB, Hysteresis DIV 10


    Mode=0                                  ' Temperature display mode
    Twist = 0

    PAUSE 500
    LCDOUT $FE, 1, $FE, $0C                ' Clear display, cursor off
    PAUSE 250




OWOUT DQ, 1, [$CC, $4E, 0, 0, DS18B20_10bit]    'Skip ROM search and write N_bits
                                              '  resolution to scratch pad
Read 0, B1                              ' Read TargetTemp MSB
Read 1, B2                              ' Read TargetTemp LSB
TargetTemp=B1*256+B2                    ' Calculate TargetTemp value (Default=20.0 C.)
Read 2, B1                              ' Read Hysteresis
Hyst=10*B1                              ' Calculate Hysteresis value (Default= 2.0 C.)
 
'-------------------------------------------------------------------------------   
MainLoop:
If Mode_B=0 then                      ' Mode switch pressed
  Pause 50                              ' Debounce
  LcdOut $FE, $8F, "*"                  ' Show that command is accepted
  If Mode_B=0 then MainLoop            ' Wait until button is released
  Mode=Mode+1                          ' Increment mode
  If Mode=1 then                        ' Save Target Temperature (Mode1 -> Mode2)
  Write 0, TargetTemp / 256      ' TargetTemp MSB
  Write 1, TargetTemp MOD 256          ' TargetTemp LSB
  EndIf
  If Mode > 1 Then                      ' Save Hysteresis (Mode 2 -> Mode 0)     
  Mode=0                    ' Only 0, 1, 2 are valid
  Write 2, Hyst / 10                  ' Divide Hyst value to fit in Byte
  EndIf
EndIf


If Mode =0 then                        ' Set Target Temperature                 
  LcdOut $FE, $80, "Set Temp.      "  ' Show function
 
  V=TargetTemp                          ' TargetTemp in V
  Gosub SelectSign                      ' Select +/blank/-
  Gosub DisplayTemp                    ' Display Target Temperature
  If (UP_B=0) Or (DOWN_B=0) then    ' Up or Down button pushed
  If DOWN_B=0 then                    ' Down button
    If TargetTemp > 7500 then          ' Not lower than -25 C. (10000-MinTemp * 100)
    TargetTemp=TargetTemp-25          ' Decrease temperuture with 0.25 C.
    EndIf
  EndIf
  If UP_B=0 then                    ' Up button
    If TargetTemp < 17500 then          ' Not higher than 75 C. (10000+MaxTemp * 100)
    TargetTemp=TargetTemp+25          ' Increase temperature with 0.25 C.
    EndIf
  EndIf
  GoSub SetTargetTemp                  ' Display TargetTemp and delay 0.25 Sec.
  EndIf
EndIf

If Mode=1 then                        ' Set Hysteresis                         
  LcdOut $FE, $80, "Hysteresys:    "  ' Show function
  Sign= " "                              ' No sign
  V= 10000+Hyst                        ' Set value for V
  Gosub DisplayTemp                    ' Display Hysteresis
  If (UP_B=0) Or (DOWN_B=0) then    ' Up or down button pushed
  Sign= " "                            ' No sign for Hysteresis
  If DOWN_B=0 then                    ' Down button
    If Hyst > 10 then Hyst=Hyst-10      ' Not less than 0.1 C.
  EndIf
  If UP_B=0 then                    ' Up button
    If Hyst < 1000 then Hyst=Hyst+10    ' Not more than 10.0 C.
  EndIf
  V= 10000+Hyst                        ' Set value for V
  Gosub DisplayTemp                    ' Display Hysteresis
  Pause 250                            ' Delay 0.25 Sec.
  EndIf
EndIf

if Mode > 0 then Mainloop              ' Setting TargetTemperature or Hysteresis

 goto mainloop                        ' Check again
'-------------------------------------------------------------------------------



        ' SUBROUTINES:
        '----------------------------------------
        SelectSign:
        If v = 10000 then                      ' Temperature = 0 C.
          Sign=" "            ' No sign
        Else
          If v < 10000 then                ' <> 0
          Sign="-"            ' Temperature below 0 C. 
          Else
          Sign="+"            ' Temperature above 0 C.
          EndIf
        EndIf
        Return
        '----------------------------------------
        DisplayTemp:
        If V >= 10000 then                    ' Above 0 C.   
          Temperature=V-10000                 
        Else                                 
          Temperature=10000-V                  ' Below 0 C.
        EndIf
        LcdOut $FE, $C0, Sign, DEC (Temperature / 100), ".", DEC2 Temperature, " ",223,"C "
        Return
        '-----------------------------------------
        SetTargetTemp:
        V=TargetTemp
        Gosub SelectSign
        Gosub DisplayTemp
        Pause 250
        Return

Getting around asm - argument out of range - error

$
0
0
hi guys , i am getting the above error due to the following bit of code

reason for the error is that SDC_PAGE IS BYTE, Event_Record_INX is a word

although the code works for what is intend , i would like re do it so that it would not generate the warning error


Code:

' get block  and page value from Event record Index - values 0-1023
  IF Event_Record_IDX <  256 THEN 
    SDC_Block = (Current_Event*4) - 3                        ' 1ST 256K BLOCK - records 0 -255
    SDC_Page  = Event_Record_IDX                            ' pages 0 - 255
  endif
  IF Event_Record_IDX => 256 AND Event_Record_IDX < 512  THEN
    SDC_Block = (Current_Event*4) -2                        ' 2ND 256K BLOCK - records 256-511
    SDC_Page  = Event_Record_IDX - 255                      ' pages 256-511
  ENDIF 
  IF Event_Record_IDX => 512 AND Event_Record_IDX < 768  THEN
      SDC_Block = (Current_Event*4) -1                        ' 3RD 256K BLOCK - records 512-767
      SDC_Page  = Event_Record_IDX - 511                      ' pages 512-767
  ENDIF
  IF Event_Record_IDX => 768 THEN
    SDC_Block = Current_Event * 4                            ' 4TH 256K BLOCK - records 768-1023
    SDC_Page  = Event_Record_IDX - 767                      ' pages 768-1023
  ENDIF

Spi flash example - 4kb/32kb/64kb erase support / winbond 25q series

$
0
0
Hi Guys
had need to review the SPI flash routines to incorporate the 4kb/32kb/64kb erase support and other features offered by the WINBOND series of flash chips

this is a re- vamp of the code i did last year and i posted for the 8mb chip to support another flash chip manufacture

hope you find it of use and dont have to re do the code wheel - again
this should be as an include to your main code

regards

Sheldon

Code:

'*          : All Rights Reserved                                                        *
'*  Date    : 4/05/2015                                                                *
'*  Version : 2.0                                                                        *
'*  Notes  : Rewriten for Winbond SPI W25Q series Serial NOR Flash chips ..            *
'*          :  W25Q32 - 32Mbit(4MB)                                                      *
'*          : --- Orginal code (V1.1) based on support for M25P80 8Mbit chip or simular  *
'*          :  no support of duel / quad SPI & QPI options                              *
'*          :  No support for Fast Write ( W25Q80 )                                      *
'*          :  NO support of Suspend write/read operations                              *
'*          : chips are 3.3v - NOT 5V input tollerant                                    *
'*          : Code supports .....                                                        *
'*          : 1  Software Protection of data , hardware WP and Hold pins high            *
'*          : 2. 4KB sectors , 32/64KB blocks, for protection / erasing, copy commnads  *
'*          : 3. Manufacture ID /Chip size detection / selection for number of blocks    * 
'*          : 4. Copy commands reseved use of scratch pad area of Block 0                *
'*      : code does not need Longs to be enabled to save on code space                  *
'*****************************************************************************************
'                                                                                        *
' Operational Notes on Timing Max and Min times                                          *
' =============================================                                          *
'                                                                                        *
'  1. powerup delay to CE & WR = 5ms min                                                *
'  2. 256 bytes max per read / write                                                    *
'  3. spi bus (mode 3) (clock idle High) and flash CE configures in main program        *
'  4. avg is 20ma during write /25ma for erase, 15ma for read                          *
'  5. min 50ns  on CE deselect                                                          *
'  6. SPI chip interface timing has min of 5ns requirements , so no timing issues      *
'      for picbasic which has a min of 2us using shiftout cmd                          *
'  7. software reset via spi = min 30us - BUSY & SUS bits are checked before issue      *
'  8. sector erase (4KB)= min 45mS  max 400ms,                                          *
'  9. block Erace(32KB) = min 120ms max 1.6sec                                          *
'  10. Block Erase (64KB)= min 150ms max 2.0sec                                          *
'  11. Chip Erase(32Mbit)=min 10sec max 50sec                                            *
'  12. hold/reset pin - factory dephalt is hold                                          *
'  13. WP pin is not active - SRP1=0 , SRP0 =0 - FACTORY DEPHALT                        *
'  14. 25QxxIQ has QE bit in register 2 = 1 from factory and cannot be cleared          * 
'----------------------------------------------------------------------------------------*


 '---------- Winbond SPI FLASH (W25Q series) commands set --------------------------------
 ' NOTE: No support for DUAL/QUAD/ QPI instruction sets
 '
 '            SDC_cmd, SDC_CmdCode
 ' command 1-13 use the Write enable command ---
 Flash_Wr_reg1        con 1  ' $01      'def write the Status Register 1(S7-S0)  - 1 byte data - write enable req prior
 Flash_Wr_reg2        con 2  ' $31      'def write the Status Register 2(S15-S8)  - 1 byte data - write enable req prior
 Flash_Wr_reg3        con 3  ' $11      'def write the Status Register 3(S23-S16) - 1 byte data - write enable req prior
 Flash_WR            con 4  ' $02      'def Page Program mode command        ( 24bit address req)+ (1 byte data)- write enable req prior
 Flash_Wr_Secreg      con 5  ' $42      'def Program Security Register write  ( 24bit address req)+ (1 byte data)- write enable req prior  - holds access upto 256byte data per register 
 Flash_Sec_Ers        con 6  ' $20      'def Sector Erase command (4KB)      ( 24bit address req)              - write enable req prior
 Flash_Blk32_Ers      con 7  ' $52      'def Block 32KB Erase Command (32KB)  ( 24bit address req)              - write enable req prior
 Flash_Blk64_Ers      con 8  ' $D8      'def Block 64KB Erase Command (64KB)  ( 24bit address req)              - write enable req prior
 Flash_Indv_Blk_LCK  CON 9  ' $36      'def Indervidual Block/Sector Lock    ( 24bit address req): WEP must be 1 to setup and use  - write enable req prior
 Flash_Indv_Blk_UNLCK CON 10 ' $39      'def Indervidual Block/Sector UNLock  ( 24bit address req): WEP must be 1 to setup and use  - write enable req prior
 Flash_Secreg_Ers    con 11 ' $44      'def Erase Program Security Register  ( 24bit address req) - write enable req prior
 Flash_Bulk_Ers      con 12 ' $C7      'def Bulk Erase command                - write enable req prior
 Flash_Globe_LCK      con 13 ' $7E      'def Globel Block /Sector Lock      : WEP must be 1 to setup and use  - write enable req prior
 Flash_Globe_UNLCK    con 14 ' $98      'def Globle Block/Sector Unlock    : WEP must be 1 to setup and use  - write enable req prior
 

 ' commands 14-12 use the Read command ---
 Flash_RD            con 15 ' $03      'def read data at normal speed        ( 24bit address req)+ (1 byte data)
 Flash_Rd_HS          con 16 ' $0B      'def read data at High speed (Fast Read) ( 24bit address req)+ (1dummy+1 byte data)
 Flash_Rd_Secreg      con 17 ' $48      'def Program Security Register read  ( 24bit address req)+ (1 dummy +1 data)( simular to fast read command )
 Flash_Rd_Sig        con 18 ' $4B      'def read Flash device Unique ID signature - cmd + 4 dummy bytes , returns 64bit ID MSB first
 Flash_Rd_Blk_LCK    con 19 ' $3D      'def Read Block/Sector Lock status  , returns 0= unlocked 1= Locked ( 24bit address req)+ (1 byte data)
 Flash_Rd_ID          con 20 ' $9F      'def Read manufacture 1byte/device/UID code 2 bytes ( JEDEC ID) - returns 3 bytes
 Flash_Rd_reg2        con 21 ' $35      'def read the Status Register 2(S15-S8)  - returns 1 data byte
 Flash_Rd_reg3        con 22 ' $15      'def read the Status Register 3(S23-S16) - returns 1 data byte

 '---- not in command lookup table - used directly -----
 Flash_Wr_dis      CON $04        'def write disable command
 Flash_Rd_reg1    con $05        'def read the Status Register 1(S7-S0)  - done by Bsy_chk routine
 Flash_Wr_en      con $06        'def write Enable command 
 Flash_Vol_Wr_EN  con $50        'def  Volatile SR Write Enable - used to change write_reg1 bits but wont set WEL flag - quick way of changeing register for protection bits ( no write enable
 Flash_EN_Rst      con $66        'def Enable Software Reset  - reset takes min 30us
 Flash_Rst        Con $99        'def Do a Reset  - Must be combined with Flash_EN_Rst
 Flash_PWR_Dwn    con $B9        'def Deep Power Down command 
 Flash_PWR_Up      con $AB        'def Release From Deep Power Down command 
 
' ----------  FLASH varables -------------------------

 Data_Length    var byte      ' table of data length
 SDC_buffer            Var Byte[256]  ' indexed data - max 256 bytes - 8mb FLASH has a 256 Limit from page Writes - Note 512 used in SD routines
 SDC_data_in    Var Byte      ' Data in byte from flash
 SDC_data_out  Var Byte      ' Data out byte from Flash -
 SDC_address_High  Var word  ' address higher word sent to FLASH
 SDC_address_Low    Var word  ' address lower  word sent to FLASH
 SDC_index            Var Word      ' Loop index counter
 SDC_CmdCode    Var Byte      ' used here as Sector protect value for the Write Status register
 SDC_cmd            Var Byte      ' command code for selected operation
 SDC_Block      var Byte      ' 64K block Address  - uses SDC_address_High.low , each block 0-63(32Kbit) 0-127(64Kbit), addressing used both
 SDC_Sector    var byte      ' 4K Sector address  - uses SDC_address_Low.highbyte , each sector = 16 pages , so sector 0 = page 0-15  - values = 0 -15
 SDC_Page      var byte      ' Page Address
 SDC_Byte      Var byte      ' Bytes Address
 Flash_RD_Fast  var bit        ' Flag for High speed read 1 = Highspeed read speed 0= normal read speed
 Flash_Install  var bit        ' flag set when flash install program used 
 Flash_32KB_Block_Sel  var bit ' Sets StartPage address for 32KB Block erase - 0 = lower block - sets start page address - page 0 - 127, 1 = High block - page 128 - 256
 Flash_Protect  var bit        ' Set Protect =1 / Unprotect =0 of a block , used as a read of block status as well
 
 Flash_Blk_Orgin    var byte  ' Orgin sector value    - copy sector command
 Flash_Blk_Dest    var byte  ' destination  sector value  - copy Sector Command 
 Flash_Sec_Orgin    var byte  ' Orgin sector value    - copy sector command
 Flash_Sec_Dest    var byte  ' destination  sector value  - copy Sector Command 
 Flash_Page_Orgin  var byte  ' Orgin Page value
 Flash_Page_Dest    var Byte  ' Dest Page Value
 Flash_Page_Length  var byte  ' number of pages
 Flash_Clear        var bit  ' flag to clear orginal sector 1= clear 
 
 Flash_Manf_id  var byte    ' manufacture id code
 Flash_Manf_type var byte    ' device type code
 Flash_Manf_cap  var byte    ' device capacity code
 Flash_tmp      var byte    ' used as a temp varable
 Flash_tmp2      VAR BYTE    ' used as a temp varable 
 Flash_Reg_val  var byte    ' Flash register value

 Flash_Addr_Copy_Low  var word  ' low word temp storage of input address and data length
 Flash_Addr_Copy_High var word  ' high word temp storage of input address and data length


'----------- Varable settings at startup ---------------------------------------------

  Flash_RD_Fast = 0        ' Set to normal read at start
  Flash_Install = 0        ' set to 0 when flash  install not being used 
  Flash_Clear = 0          ' set not to clear
  Flash_32KB_Block_Sel = 0 ' set to use low address
 
'---------------- Routines  ------------------
goto JumpOverSPIFLASH    ' Jump to end of this include file  for Correct compile 
 
  Flash_Init:
    ' do a read register command to clear the Flash register after power up
 
        FLASH_CE = 0                                  ' Bring the FLASH chip select line low 
          SDC_data_out = Flash_Rd_reg1                ' Send the read status register command byte
              Gosub Flash_spiw                                  ' Write Byte SDC_data_out. , subroutine writes a byte on the SPI bus
        FLASH_CE = 1                                  ' Bring the FLASH chip select line high 
        gosub buffer_clear                            ' clear varables area
     
  return                                                       
' -----------------------------
  Buffer_Clear:
 
      For SDC_index = 0 To 255
          SDC_buffer[SDC_index] = 0
      next SDC_index
  return

'---------------------------------------
  Flash_Reset:
  ' routine to software reset the flash
  ' enable reset and reset must be consecutive
 
    FLASH_CE = 0                                  ' Bring the FLASH chip select line low 
      SDC_data_out = Flash_EN_Rst                ' Send Enable Reset command
      gosub Flash_spiw                                  ' Write Byte SDC_data_out. , subroutine writes a byte on the SPI bus
      SDC_data_out = Flash_Rst                    ' Send Reset command
            Gosub Flash_spiw                                  ' Write Byte SDC_data_out. , subroutine writes a byte on the SPI bus 
    FLASH_CE = 1                                  ' Bring the FLASH chip select line high 
   
  return

'-----------------------------------------

 Flash_Format:
 'Note wont work if any sectors are protected
  ' can take upto 50sec on 32mbit chip
  sdc_cmd = Flash_Bulk_ers  ' excutes as it sounds
  gosub Flash_Comand
 return
 
'-------------------------------------------------
 
 Flash_Sec_Erase:
  ' assumes Block address  0-63
  ' assumes Sector value 0 -15
  ' erases 1 Sector at a time  address (4KB)= 15 pages
  ' Erases wont work on sectors that are write protected
  ' sector erase take 400ms per instruction
  SDC_pAGE = 0                          ' ensure 0
  SDC_pAGE = sdc_SECTOR << 4            ' shift given sector 0-15 to upper page value 
 
  sdc_cmd = Flash_Sec_Ers              ' Erases  sector ( 256 pages x 256 bytes )
  SDC_address_high.byte0 = SDC_Block    ' set sector Address 0 to 15 for chip
  SDC_address_low.byte1  = SDC_Page    ' set page start address 0 to 255 
  SDC_address_low.byte0  = 0            ' set byte start address 0 to 255
  gosub Flash_Comand                    ' Do the command
 return

'----------------------------------------------------

 Flash_Blk32_Erase:
  ' assumes Block address 
  ' assumes flag 32KB_Block_Sel 0 = bottom 32Kb start address page, 1= 128page address
  ' erases 1/2 a 64KB Block at a time  address (32KB)= 128 pages
  ' Erases wont work on sectors that are write protected
  ' sector erase take min 120ms max 1.6Sec per instruction
  IF Flash_32KB_Block_Sel = 0 THEN          ' select offset page for 32 byte sector
      SDC_PAGE = 0
  ELSE
      SDC_PAGE = 128
  ENDIF   
  sdc_cmd = Flash_blk32_Ers            ' Erases 32kB block ( 128 pages of 256 bytes )
  SDC_address_high.byte0 = SDC_Block    ' set block Address for chip
  SDC_address_low.byte1  = SDC_PAGE    ' set page start address 0 to 255 
  SDC_address_low.byte0  = 0            ' set byte start address 0 to 255
  gosub Flash_Comand                    ' Do the command
 return
'-------------------------------------------------------------
 
 Flash_Blk64_Erase:
  ' assumes Block address 
  ' erases a 64KB Block at a time  address (64KB)= 256 pages
  ' Erases wont work on sectors that are write protected
  ' sector erase take min 160ms max 2.0Sec per instruction
 
  sdc_cmd = Flash_blk64_Ers            ' Erases 64kB block ( 256 pages of 256 bytes )
  SDC_address_high.byte0 = SDC_Block    ' set Block Address for chip
  SDC_address_low.byte1  = 0            ' set page start address 0 to 255 
  SDC_address_low.byte0  = 0            ' set byte start address 0 to 255
  gosub Flash_Comand                    ' Do the command
 return

'------------------------------------------------------------------
 
 Flash_Block_Copy:
  ' copys data from 1 block to another
  ' erases destination block - optional clean of orginal block
  ' assumes Flash_Sect_Orgin,Flash_Sect_Dest ,Flash_clear
  ' Erases wont work on blocks that are write protected
  ' block erase take 400ms per instruction
      SDC_Block  = Flash_Blk_Dest            ' clear destination block
      gosub Flash_blk64_Erase                ' use SDC_blk64_erase info to erase destination sector
      for SDC_PAGE = 0 to 255              ' copy pages to selected block ( 256bytes at a time)
          SDC_Block  = Flash_blk_Orgin      ' block to copy
          SDC_byte    = 0
          Data_Length = 255
          gosub Flash_Read                  ' read the data to buffer
          SDC_Block  = Flash_Blk_Dest      ' block to copy to
          SDC_byte    = 0                  ' use block details for destination
          Data_Length = 255
          gosub Flash_Write                ' write the data
      next SDC_PAGE
    if Flash_clear = 1 then
        SDC_Block = Flash_Blk_Orgin        ' clean up orgin Block
        gosub Flash_Blk64_Erase             
    endif
 return
   
 Flash_Page_Copy:
  ' copys data from pages 0 -255 from orgin or destination block
  ' assumes destination block pages are Erased
  ' assumes pages orgin and pages destination do not cross block boundrys
  ' assumes Flash_Page_Orgin,Flash_Page_dest ,Flash_Page_length, Flash_blk_orgin ,Flash_blk_dest 
  ' writes wont work on blocks that are write protected
     
    for  Flash_tmp2 =  Flash_Page_Orgin to Flash_Page_length    '  copy sector 0 pages to selected sector 0 ( 256bytes at a time)
          SDC_Block  = Flash_blk_Orgin                    ' sector to copy
          SDC_PAGE =  Flash_tmp2
          SDC_byte    = 0
          Data_Length = 255
          gosub Flash_Read                                  ' read the data to buffer
       
          SDC_Block  = Flash_blk_Dest                      ' sector to copy to
          SDC_PAGE = Flash_Page_Dest                        ' page to copy to
          SDC_byte    = 0               
          Data_Length = 255
          gosub Flash_Write                                ' write the data
          Flash_Page_Dest = Flash_Page_Dest + 1              ' increment destination page
    next Flash_tmp2
 
 
 return
'----------------------------------------
 
     
 Flash_Write:
  ' assumes data length , address and upto 256 bytes in  SDC_buffer[] to send
  ' writes bytes from SDC_buffer[] into flash , block/page/byte loction must be "FF" at start
  ' write wont work on sectors that are write protected
 
  sdc_cmd = Flash_WR              ' excutes as it sounds - max of 256 bytes
  SDC_address_high.byte0 = SDC_Block  ' set block Address for  chip
  SDC_address_low.byte1  = SDC_Page    ' set page start address 0 to 255 
  SDC_address_low.byte0  = SDC_byte    ' set byte start address 0 to 255
  gosub Flash_Comand
 
  return
 
'--------------------------------------

 Flash_Read:
  ' assumes Block, page, bytes , data length input
  ' gets upto bytes data into SDC_buffer[] from flash
  ' uses normal read speed if no Flash_RD_Fast flag set
  if Flash_RD_Fast = 1 then
    sdc_cmd = Flash_Rd_HS          ' excutes fast read  - max of 256 bytes
  else
    sdc_cmd = Flash_RD            ' excutes normal read - max of 256 bytes cos of buffers size/ page size
  endif
  SDC_address_high.byte0 = SDC_Block  ' set block Address  for chip
  SDC_address_low.byte1  = SDC_Page    ' set page start address 0 to 255 
  SDC_address_low.byte0  = SDC_byte    ' set byte start address 0 to 255
  gosub Flash_Comand
  return

 
 Flash_WR_protect:
 ' routine to protect/ unprotect a set block of 60-63 in 32kbit chip ( block 60-63 used by K9 system stored settings )
 ' Assumes Flash_Protect - 0 = unprotect 1 = Protect a sector
 ' SETUP FOR 32kB CHIP
 '  Bit7 = SRP0 - Set to 0 - software protect (SRP1= 0 DEPHALT(stat_reg2(bit0), not WP pin
 ' bits6(SEC) = 0 = 64KB Block 1= 4Kb SECTOR , Bit5(TB) 0= Top, 1 = Bottom , Bit4-2 = BP2-BP0. bits 1-0 (wel, busy )
 
 
    if Flash_Protect = 0 then
        SDC_buffer[0] = $00 ' clear all blocks
    else
        SDC_buffer[0] = $0C ' set blocks 60-63 protected for K9 project
    endif
 
      sdc_cmd = Flash_Wr_reg1  ' write register
      data_length = 1          ' set 1 byte 
      gosub Flash_Comand
  return

'-----------------------------------------------------------
  Flash_WR_Disable:
  ' reset the write enable latch
      FLASH_CE = 0                                ' Bring the FLASH chip select line low 
        SDC_data_out = Flash_Rd_reg1                ' Send the read status register command byte
            Gosub Flash_spiw                                ' Write Byte SDC_data_out. , subroutine writes a byte on the SPI bus
      FLASH_CE = 1                              ' Bring the FLASH chip select line high 
  return
'----------------------------------------------------
  Flash_Read_ID:
  ' returns JEDEC ID-  returns Manufacture ID byte($EF), mem type - (ID15-ID8)1byte ,capacity(ID7-ID0)1byte) 
     
    sdc_cmd =  Flash_Rd_ID              ' Flash Read ID
    data_length = 3                    ' 3 bytes 
    gosub Flash_Comand
    Flash_Manf_id =  SDC_buffer[0]
    Flash_Manf_type = SDC_buffer[1]
    Flash_Manf_cap  = SDC_buffer[2]
    IF FLASH_Install = 1 then
      HSEROUT ["Flash Manf ID = ",hex Flash_Manf_id,"  Flash Device ID = ",hex Flash_Manf_type,"  Device Capacity ID = ",hex Flash_Manf_cap,13,10] 
    endif
  return 
'---------------------------------------------------------------------
  Flash_Read_Sig:
  ' returns the flash chips Unique ID number ( 64Bit)
    sdc_cmd = Flash_Rd_Sig              ' Flash Read Unique ID signature
    data_length = 4                      ' 4 bytes                ' Flash Read Unique ID signature
    gosub Flash_Comand
  return
  '----------------------------------------------
 Flash_Read_BLK_Lock:
 'routine to read block or sector locks ( WPS=1 to work )
  sdc_cmd =  Flash_Rd_Blk_LCK                          ' Flash Read block lock 
  data_length = 1                                    ' 1 bytes 
  gosub Flash_Comand
  Flash_Protect = SDC_buffer[0] & $01                  ' get bit0 value - 1 = locked .0 = unlocked 
  return
 '---------------------------------------------
 
  Flash_Power_Down:
 ' sEt Flash chip to low power mode from standby mode - no command other tha powerup will wake the device
    FLASH_CE = 0                                ' Bring the FLASH chip select line low 
      SDC_data_out = Flash_PWR_Dwn              ' command to pwr down
          Gosub Flash_spiw                                ' Write Byte SDC_data_out. , subroutine writes a byte on the SPI bus
    FLASH_CE = 1                                ' Bring the FLASH chip select line high 
  return
'---------------------------- 
  Flash_Power_Up:
 ' sEt Flash chip to standby mode - no command other tha powerup will wake the device
' sEt Flash chip to low power mode from standby mode - no command other tha powerup will wake the device
    FLASH_CE = 0                                ' Bring the FLASH chip select line low 
      SDC_data_out =  Flash_PWR_Up              ' command to pwr up
          Gosub Flash_spiw                                ' Write Byte SDC_data_out. , subroutine writes a byte on the SPI bus
    FLASH_CE = 1                                ' Bring the FLASH chip select line high 
  return
 '-----------------------------


' -----------------------------
 Flash_Read_reg2:
 ' reads write register 2 - 
 ' Register 2 controls .....
 ' bit7(S15)- Suspend Status
 ' bit6(S14)CMP -Compliment Protect - dephalt CMP =0
 ' bit5-3(S13-11)- Security Register(One time programable) dephalt = 000
 ' bit1(S9) - QE - Quad SPI Mode Enable ( default =0 when chips NOT FVIQ type)
 ' bit0(S8) - SRP1-  Protect bit1 _ dephalt =0
 ' 
  sdc_cmd =  Flash_Rd_reg2                          ' Flash Read register 2 
  data_length = 1                                  ' 1 bytes returned   
  gosub Flash_Comand
 return
 
 '---------------------------------------
 
 Flash_Read_reg3:
 ' Register 3 controls .....
 ' bit7(S23) - Hold(0- dephalt)/Reset(1) pin fuction 
 ' bit6,5(S22,S21)(0,0=100%),(0,1=75%),(1,0=50%),(1,1=25%- dephalt) read driver strength- used to overcome cpacitive loading ,trace impendace issues with signal at high speed reads
 ' bits4,3 - reserved (writen as 0)
 ' Bit2(S18) WPS - selects Protection methode - Indervidual block/sectors(WPS=1) or by grouping range(WPS=0) ( WPS dephalt is 0 )
 ' Bits1,0-reserved(writen as 0) 
  sdc_cmd =  Flash_Rd_reg3                          ' Flash Read register 3 
  data_length = 1                                  ' 1 bytes returned   
  gosub Flash_Comand
  return
 
  '-----------------------------------
 
  Flash_Comand:
  ' assumes SDC_address long
  ' assumbe data_Length = Lookup table to fill
  ' max 256 bytes per write
  ' erase of the sector by byte  / bulk is needed prior to write , as a write only changes a 1 to 0 , and the erase chanse the 0 to 1
  if  SDC_cmd = 0 then return          ' invalid command if value =0


 
 Lookup SDC_cmd,[$00,$01,$31,$11,$02,$42,$20,$52,$D8,$36,$39,$44,$C7,$7E,$98,$03,$0B,$48,$4B,$3D,$9F,$35,$15],SDC_CmdCode ' asignes command code to flash chip 
 
      Flash_tmp = 0                        ' ensure 0 - counter for busychk loop
      gosub BSY_chk                        ' do for all commands
       
    if SDC_cmd <=14 then                  ' do for writes, sector erase,bulk erase , write reg protect - commands 1-14 use writen enable first
        FLASH_CE = 0                      ' Bring the FLASH chip select line low.
            SDC_data_out = Flash_Wr_en        ' Send  command byte that requires a write
        Gosub Flash_spiw                      ' Write Byte SDC_data_out. , subroutine writes a byte on the SPI bus
              FLASH_CE = 1                      ' Bring the FLASH chip select line high to complete the write command
            pauseus 1                          ' pause between prev command  - may not need
    endif
       
    FLASH_CE = 0                          ' Bring the FLASH chip select line low
    SDC_data_out =  SDC_CmdCode          ' Send the  command byte,
            Gosub Flash_spiw                          ' Write Byte SDC_data_out. , subroutine writes a byte on the SPI bus
   
    if SDC_cmd =>4 and SDC_cmd <=11 or _          ' if write commands 4-11 then use 24bit address assigned
        SDC_cmd =>15 and SDC_cmd <=19 then          ' or read commands 15-19 then use 24bit address assigned , command 18 uses 24bit address as 3 dummy bytes
          SDC_data_out = SDC_address_high.Byte0    ' set block Address for chip
              Gosub Flash_spiw                                    ' Write Byte SDC_data_out.
              SDC_data_out = SDC_address_low.Byte1      ' set page address 0 to 255 
              Gosub Flash_spiw                                ' Write Byte SDC_data_out.
              SDC_data_out = SDC_address_low.Byte0      ' set byte address 0 to 255
          Gosub Flash_spiw                                    ' Write Byte SDC_data_out.
          if SDC_cmd => 16 and SDC_cmd <= 18 then  ' if read High speed mode ,Program security read , Read Unique ID(add 4th dummy byte)
            SDC_data_out = $00                    ' add a dummy byte to command 
            Gosub Flash_spiw                                ' Write Byte SDC_data_out.
          endif
      endif
   
      if SDC_cmd <=5 or _                                ' if write commands require data bytes sent
        SDC_cmd >=15 and SDC_cmd <= 22 then            ' or read commands require data bytes returned   
               
          For SDC_index = 0 To Data_Length-1            ' DATA LENGTH -1  so byte 0 is counted when setting data length 
              if SDC_cmd <= 5 then                      ' if write ,Write register,write Security register then
                SDC_data_out = SDC_buffer[SDC_index]    ' send contents of indexed SDC_buffer
                gosub  Flash_SPIw                          ' write byte SDC_data_in from SPI bus.  Returns SDC_data_in.
              endif
           
              if SDC_cmd =>15 and SDC_cmd <=22 then      ' if read scommand returns data
                gosub  Flash_SPIR                          ' Read Byte SDC_data_in from SPI bus.  Returns SDC_data_in.
                      SDC_buffer[SDC_index] = SDC_data_in
              endif
          Next SDC_index
      endif
      pauseus 1
    FLASH_CE = 1                          ' Bring the FLASH chip select line high.

  return
'----------------------------------------------------
 Bsy_chk:
        FLASH_CE = 0                                ' Bring the FLASH chip select line low 
        SDC_data_out = Flash_Rd_reg1                ' Send the read status register1 command byte
            Gosub Flash_spiw                                ' Write Byte SDC_data_out. , subroutine writes a byte on the SPI bus
        Flash_tmp = 0                              ' clear counter                 
      while  Flash_tmp <150                        '  loop test read for busy ( counter may need to be increased depending on size of chip and time for block erase command to complete     
        gosub Flash_SPIR                                  ' Read Byte SDC_data_in from SPI bus.  Returns SDC_data_in.
        IF FLASH_Install = 1 then
          Flash_Reg_val  = SDC_data_in              ' get value of read status reg value for use in flash install program checks for protection
          HSEROUT ["STATUS REG = ",hex Flash_Reg_val,13,10]  ' show on terminal when doing flash install
          HSEROUT ["Flash_tmp = ",dec2 Flash_tmp,13,10]   
        endif
        Flash_tmp = Flash_tmp + 1
        SDC_data_in =  SDC_data_in & $01          ' mask bit 0 ( busy flag)
        if SDC_data_in = 0 then Flash_tmp = 151    ' Test bit 0 if write not busy,force exit of loop
      wend                 
      FLASH_CE = 1                                    ' Bring the FLASH chip select line high
 return 
'-------------------------------
  Flash_SPIR:
    SO = 1                                            ' Shift out high bits while shifing in data.
        Shiftin SO, SCK, 6,[SDC_data_in]        ' MSb first, clock idle high.
 
  Return
'-------------------------------

  Flash_SPIW:
      shiftout SI,SCK,5,[SDC_data_out]            ' MSb first, clock idle high.
                     
  return
'------------------------





JumpOverSPIFLASH:

ESP_BasicPro?!

$
0
0
These last couple of months I have been playing around with the ESP8266 wifi module.

I have been dabbling with all development platforms (native AT, Lua, Arduino-for-ESP IDE et al), and I confess its been an uphill task, specially after having been spoilt by the ease of use of PBP.

Seeing the similarities between the microcontraller on the ESP (as also its raw power), I was just wondering, how easy life would be if Melabs were to launch a product like the PBP, ported to the ESP?

This might not be as far-fetched as it sounds, as the Arduino folks have done something similar; one can use the Arduino IDE with an 'ESP patch' to directly program the ESP, without involving an actual ARduino. So, in effect, most resources available to the Almel folks using the Arduino IDE are now made available for the ESP8266.

Just a thought! Shall definitely be one of the first customers!

Regards.

Issues with a bootloader

$
0
0
Hi,

I'm experimenting with the Tiny Bootloader (http://www.etc.ugal.ro/cchiculita/so...bootloader.htm). I've configured the loader for the PIC I'm using (18F4580) and after I've squirted the loader hex code to the PIC and the tiny boot loader PC application sees and identifies the chip. I can then load my HEX code via the Tinyloader PC app, which then runs fine on the PIC, but after doing so the tinyloader application can no longer find the PIC.

The website mentions
Quote:

In order for the bootloader to be launched after each reset, a "goto bootloader" instruction must exist somewhere in the first 4 instructions
I'm gathering that the process of uploading my code over writes the flash memory containing the bootloader, or the "goto" part.

can anyone comment or offer advice on what I need to do to my code in order to maintain the functionality of the bootloader.

Anyone else programming with Microchip C30? (Bitwise operation)

$
0
0
Hi Guys,

I’ve been porting my reusable code to C30 from BASIC/assembler with mixed results where speed is concerned.
Bitwise operation has always been clumsy in C. Below are two examples, one in asm, and one C,
that both rotate a ten byte array bitwise one place to the right.
Does anyone have a faster (not smaller) way in C?

There could be other areas I need to check such as LCD timings, etc.
dsPic is much faster, and I thought speed difference of the code would be compensated for by that.
Cheers, Art.

Code:

RotateRight ;bitwise rotate array
rrf HMK+0 ,F ;a faster way
rrf HMK+1 ,F ;
rrf HMK+2 ,F ;
rrf HMK+3 ,F ;
rrf HMK+4 ,F ;
rrf HMK+5 ,F ;
rrf HMK+6 ,F ;
rrf HMK+7 ,F ;
rrf HMK+8 ,F ;
rrf HMK+9 ,F ;
bcf HMK+0 ,7 ;preclear MSB
btfsc status ,C ;check carry bit
bsf HMK+0 ,7 ;set MSB
return ;or return

Code:

void rotateRight() {
temp = (HMK[9] << 7); // rotate HMK right once
HMK[9] = (HMK[8] << 7) | (HMK[9] >> 1);
HMK[8] = (HMK[7] << 7) | (HMK[8] >> 1);
HMK[7] = (HMK[6] << 7) | (HMK[7] >> 1);
HMK[6] = (HMK[5] << 7) | (HMK[6] >> 1);
HMK[5] = (HMK[4] << 7) | (HMK[5] >> 1);
HMK[4] = (HMK[3] << 7) | (HMK[4] >> 1);
HMK[3] = (HMK[2] << 7) | (HMK[3] >> 1);
HMK[2] = (HMK[1] << 7) | (HMK[2] >> 1);
HMK[1] = (HMK[0] << 7) | (HMK[1] >> 1);
HMK[0] = temp | (HMK[0] >> 1);
}

Debug/Serout/Serout2 - please, need advice ...

$
0
0
Hi !
I try to understand how work communication between PIC and PC.
I wrote this simple code :
Code:

@ __config _INTRC_OSC_NOCLKOUT & _WDT_ON & _PWRTE_ON & _MCLRE_OFF & _BODEN_ON
 
INCLUDE "modedefs.bas"     
DEFINE OSC 04               
        CMCON = 7             
        VRCON = 0             
        WPU = 0                'no wake pullup
        IOCB = 0                'no int. on change
        ANSEL = %01010001     
        ADCON0 = %10000001      'right just , Vref=Vdd , ch.0 , A/D on
        GPIO = %00101001        'preset I/O
        TRISIO = %00001001      'configure direction  1=input 0=output
        OPTION_REG = %10000111

advalue        var    word

Main:
PauseUs 50                  'sample time
        ADCON0.1 = 1                'start A/D
        @ nop
        while ADCON0.1 = 1         
        wend
        advalue.byte1 = ADRESH        'get A/D value
        advalue.byte0 = ADRESL

        if advalue < 650 then
          SerOut2 GPIO.5,300,["Advalue : ",DEC advalue, 13, 10]  ; the calculated result is "93" , but ?!
        endif
goto Main

I use on GPIO.0 one 1k resistor to +5 volts and one 100R resistor to ground. Advalue is 93, don't ?!

But on RealTerm I am unable to see the "real" value of Advalue, no matter what option I choose...
What I do wrong ?!
Thanks in advance !
Attached Images
 

help using SHIFTOUT to control SPI digital potentiometer

$
0
0
I'm trying to use the MCP4231-104 7bit digital potentiometer to make an LED get dimmer and brighter. That's not my ultimate goal with the digipot, but I'm just trying to make it do something basic before moving on. I'm using the PIC16F676 for the MCU. CS is connected to PORTC.0, SDO is connected to PORTC.1, and SCK is connected to PORTC.2

Main:

PORTC.0 = 0
SHIFTOUT PORTC.1, PORTC.2, 1, [0\7]
PORTC.0 = 1
PAUSE 100

PORTC.0 = 0
SHIFTOUT PORTC.1, PORTC.2, 1, [64\7]
PORTC.0 = 1
PAUSE 100

PORTC.0 = 0
SHIFTOUT PORTC.1, PORTC.2, 1, [128\7]
PORTC.0 = 1
PAUSE 100

PORTC.0 = 0
SHIFTOUT PORTC.1, PORTC.2, 1, [64\7]
PORTC.0 = 1
PAUSE 100

goto main
end


This isn't doing anything. I've also tried enter the shiftout bits as binary, but I get the error msg "Malformed Binary Integer".

Writing & Reading 2 Bytes to/from Flash

$
0
0
Hello,

I want to store and recall 2 Bytes from Flash on a PIC18F27J13 for pairing up a remote control to a mobile unit.
These are RadioID(RF Address) and RadioCH (Channel#) which are both just 8 bit VARs each.
I think I have the basic idea as below but I cannot figure out how to configure Flash programming.
The (2) memory locations ($1800 and $1900) I picked as it looks like my main code is way below.

Although I think this wastes a whole 64Byte block per each, I do not care at the moment.
I did try setting the above VARs to Words instead of Bytes but that did not help.
Reading through various posts I think I need to define an area of memory for these (2) bytes and un-protect them from being written to. However, I have be unable to find any simple examples and am stumped.

Thanks,


'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''''
'' Flash Experiments


Flash:


If Push = 0 Then ' Should Increment ID & CH by 1
Pause 10
If Push = 1 Then ' Noise
Return
EndIf
Do
Pause 10
Loop Until Push = 1 ' Wait for User Release
GLED = 1
EraseCode $1800 ' Whole Block of 4 Words $1800 to $1864?
EraseCode $1900
RadioID = RadioID + 1
RadioCH = RadioCH + 1
WriteCode $1803, RadioID ' Writes to 4th Word in Block
WriteCode $1903, RadioCH
ReadCode $1803, RadioID ' Reads from 4th Word in Block
ReadCode $1903, RadioCH
hserout2["RadioID: ", dec RadioID, 13,10]
hserout2["RadioCH: ", dec RadioCH, 13,10]
GLED = 0
EndIf


GoTo Flash


'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''''

Hserout

$
0
0
Hi,
I need to do serial output with PIC12F675 using HSEROUT.

It's possible?

Sorry for my English speech.

Pulse capture on 12F1822

$
0
0
Hello all, I'm having a bit of a problem using the CCP hardware to measure a pulse length on a 12F1822. I know where the program stalls but I can't figure out why. I debugged the program by lighting an LED at the various points. It never moves past the second while/wend loop when the trailing edge of the pulse is supposed to trigger an interrupt. Any ideas on why the interrupt is never happening? Thanks.

Code:

#CONFIG
        __CONFIG _CONFIG1, _FOSC_INTOSC & _WDTE_OFF & _PWRTE_ON & _CP_OFF & _CPD_OFF & _BOREN_OFF & _CLKOUTEN_OFF & _IESO_OFF & _FCMEN_OFF
        __CONFIG _CONFIG2, _PLLEN_ON
#ENDCONFIG

OSCCON = %01110000                '8Mhz clock with 4x PLL (32MHz)
APFCON = %00000001                'CCP1 on RA5 (pin 2) T1G is RA4 (pin 3)
PORTA = 0                                'RA5 all low
TRISA = %00100000                'RA5 input
CM1CON0 = 0                        'turn off comparitor 1
CM1CON1 = 0                        'configuration of comparator
MDCON = 0                                'turn off data modulator
DACCON0 = 0                        'turn off DAC
DACCON1 = 0                                'turn off DAC
ANSELA = 0                                'All pins digital
OPTION_REG = %10000000        'Weak pull ups off
FVRCON = 0                                'Bunch of stuff disabled
CPSCON0 = 0                                'Cap touch sensors off
CCP1AS = 0
PIE1.2 = 1
PIE2 = 0
T1GCON = 0


DEFINE OSC 32
Include "modedefs.bas"

Symbol                Capture = PIR1.2
T1                        VAR word
PW                        VAR word

reload:
low porta.2
CCP1CON = %00000101

T1CON = 0
TMR1H = 0
TMR1L = 0
T1CON.0 = 1

Capture = 0
while !Capture
wend

T1.HighByte = CCPR1H
T1.LowByte = CCPR1L

CCP1CON = %00000100
Capture = 0


while !Capture
wend


PW.HighByte = CCPR1H
PW.LowByte = CCPR1L

PW = PW-T1

serout porta.4,N9600,[#PW,"uS High",13,10]
goto reload

end

formula for converting linear taper digital pot to audio taper

$
0
0
I'm using ADCIN on a linear analog pot to generate a number 0 - 127. This number is used to simultaneously control several 100k digital pots. Unfortunately, one of the digital pots needs to be linear taper....or I should say, its optimal taper would be linear. And I'm not so good with the math and logarithmic equations and such.

Could someone please help me with the formula to create a taper that approximates the line in graph "TAPERS A SERIES" found here: http://www.taiwanalpha.com.tw/englis...p_e_106_10.htm

Steampunk GPS

$
0
0
Hi Guys :)

I wanted to show a new project which is an add on for an older project.
And an idea why timing math problem might make a headache ;)
and to show off.
It’s a GPS made back in the 1920’s when GPS first arrived on the scene.

It’s a Ublox LEA-5T timing module, I think now a generation behind the current one.
15ns timing error when in timing mode and kept stationery after an auto position survey.
I’m using positioning mode which is 30ns timing error.
There is a copper plane under the module insulated with a thin dielectric.

The antenna is QFH (Quadrifillar Helecoidial) backfire antenna for L2 (1.575 GHz),
and is made with copper elements, brass tube stem (as they did back in that age),
and rigid coax fed through to the tip of the antenna. It’s a backfire antenna,
because if it was a transmitter, it would radiate toward the feed point at the tip of the antenna.

The SPI EEPROM is for faster startup time by storing almanac information,
but I did not use it in the end, I gathered it just start fresh each power cycle.

The Ublox timing module was salvaged form an existing product board that was rejected by
the manufacturer, and I had to remove it. Out of six modules, this was the only one successful
where I didn’t lose some pads off the module. It later got a frequency output SMA connector
underneath which isn’t present in the photos. Hope you like it :)

ws812 matrix rotate

$
0
0
just playing with a ws812 8x8 array and looking for a nice way to rotate a 8x3x8 byte array through 90 degrees, I have left and right shift and up/down sorted but a tidy rotate cw and ccw method is eluding me . any thoughts

the code here is a work in progress and not very well commented
Attached Files

12f638 simple temperature monitor

$
0
0
Hi all, I'm probably overlooking something simple I missed but I just can't seem to figure it out. I was messing around with a DS18B20 and a 12f683 and copied some simple code to read the DS and display the temperature from it on an LCD screen. So far so good all of that works. I tried to add what I thought was going to be a simpe IF statement to check the temperature and turn an LED on but this is turning out to be difficult for me. What is happening is the temp displays just fine on the LCD but my LED never comes on or at best it blinks on ever so slightly. I've tried a bunch of different combinations hooking the LED up so that low LED will turn it on instead of high LED and different combinations of using a pull up or down depending on what high or low I was trying but nothing seems to work. Below is the program I am using. I am used the U2 programmer from MELABS. If anyone could shed some light on this I would appreciate it. Just to make sure I am using the correct low/high syntax I have my code toggling the LED before I get into the main loop and the LED in fact works like a charm it just breaks when I get into mainloop.
Thanks
David

Include "modedefs.bas" ' Mode definitions for Serout

LCD Var GPIO.1 ' LCD TX pin
DQ VAR GPIO.4 'pin 3 One-wire Data-Pin "DQ"
C VAR BYTE 'Status of I/O port bit
R_Temp VAR WORD 'RAW Temperature readings
C_neg VAR BIT '=1 if cold bit for C set
NEGPOS VAR BYTE 'Sign char "-" or "+"
C_Temp VAR WORD 'Celsius Temperature
F_Temp VAR WORD 'Fahrenheit Temperature
maxT var word
LED CON 2 ' pin 5 for led indicator

ANSEL = 0 ' Set all digital
CMCON0 = 7 ' Analog comparators off
Pause 500 ' Wait .5 second for LCD to init
low led
pause 1000
high led
pause 1000
mainloop:
gosub start_Convert
goto mainloop
Start_Convert:
OWOUT DQ, 1, [$CC,$44] 'Send calculate temperature command
REPEAT
PAUSEUS 25 'Waiting loop until reading complete
OWIN DQ, 4, [C] 'Monitor for pin high transition ...
UNTIL C <> 0 ' ... reading complete
OWOUT DQ, 1, [$CC, $BE] 'Send read scratchpad command
OWIN DQ, 2, [R_Temp.LowByte,R_Temp.HighByte] 'get temperature
IF R_Temp.11 = 1 THEN 'check below zero bit
C_neg=1
NEGPOS = "-"
R_Temp = ~R_Temp + 1
ELSE
NEGPOS = "+"
C_neg=0
ENDIF
C_Temp = R_Temp >> 3 'Get whole # of degrees & first decimal bit
'SEROUT LCD , n2400,[$fe, 1,"C = ",NEGPOS,#C_Temp/2," deg. C",10,13]
F_Temp = C_Temp * 9
F_Temp = F_Temp / 5
F_Temp=F_Temp/2 'remove decimal bit
IF NEGPOS="+" THEN
F_Temp = F_Temp + 32
ENDIF
IF NEGPOS ="-" AND C_Temp <= 36 THEN 'was 18 - caused error!
F_Temp = 32 - F_Temp
NEGPOS = "+"
ENDIF
IF NEGPOS="-" AND C_Temp > 36 THEN 'was 18 - caused error!
F_Temp = F_Temp - 32
ENDIF
SEROUT LCD , n2400,[$fe, 1,"F = ",NEGPOS,#F_Temp," deg.F",10,13]
C_Temp = C_Temp/2
if F_temp =>70 then
low LED
else
high LED
endif
return
end

Multi servo interrupt program in Mikro Basic converting to PBP

$
0
0
I found a neat multi-servo program using interrupts, written in Mikro Basic (I think - looks familiar). I translated as much as I could into PBP, but I cannot get the system to work. I've gone over it many times and ran the ICD in MCS+ trying to troubleshoot it. (Unfortunately ICD doesn't seem to work for interrupts).

I uploaded both programs for "someone" to take a look at to see if there is something obvious I'm doing wrong.

FYI, some things that I did change: 1) I don't have the For-Next loop to add all the servos, I only use one for now. 2) my servo is servo1 at PortA.0. 3) Mikro program used a 16F877 at 20MHz, I am using a 16F887 at 4 MHz so I changed the timer preload and prescaler accordingly. I'm comfortable with timers and interrupts, but i'm certainly no expert.

Any help is appreciated of course.

Original Mikro Basic is Servo8_1000. My PBP attempt is Servo8_100FEMOD

Oh...I have some difficulties with loading the 16bit timer TMR1

incrementing 16 bit timer value

$
0
0
I have TMR1 loaded with $FC7C, using the commands TMR1H=$FC and TMR1L=$7C.

I was hoping to make this one variable like T1Val=$FC7C so that I can increment or decrement this timer value. Can this be done? Can I concantenate both bytes as in T1Val=TMR1H +TMR1L (where T1Val var word)?
Viewing all 4787 articles
Browse latest View live