DATA, READ and WRITE are the correct commands for the internal EEPROM in the 16F688 chip. Make sure your version of PICBasic supports this chip, and you have any patches installed. Check on MeLabs website.
DATA, READ and WRITE are the correct commands for the internal EEPROM in the 16F688 chip. Make sure your version of PICBasic supports this chip, and you have any patches installed. Check on MeLabs website.
Just to add ... PBP takes care of all the EECON1 and EECON2 stuff so you do not have to - you just need DATA, READ, and WRITE.
See example code on melabs website
http://www.melabs.com/resources/samples/pbp/ee.bas
Paul Borgmeier
Salt Lake City, Utah
USA
Thanks for the help. I am still a bit unclear, so I want to know if I can do something like this (I would do it myself with the PIC but I won't be able to until tomorrow):
Beep var byte
flash var byte
Gosub EEPROM
Start:
if portb.4=1 then
beep=beep+1
gosub EEPROM
if beep=1 then
high portb.0
endif
if portb.3=1 then
flash=flash+1
gosub EEPROM
if flash=2 then
high portb.1
endif
goto start
EEPROM:
write 0, beep
write 1, flash
return
I know this isn't a very complex code but the reason I need to write to the EEPROM is because I need to change functions on an alarm system after the PIC is already programmed. Some questions.... if for example I just say "flash var byte," does the PIC automatically recognize flash as a zero? Lets say the PIC has been running for a while and flash=7. Now, I power off the PIC and it holds the 7 in memory. If I goto power up the PIC and I have it go directly to the write command, will it simply just write over flash but still make it 7? Also, do I need the read command or can I just say "If beep=1 then......?"
Thanks,
Chris
Hi Chris,
Maybe one of the pros will add some more answers, but for now...
PBP does not initialize RAM variables to any value, and on power up the RAM variables will contain an unknown value. You can use the CLEAR command to clear all RAM variables to 0, or just set each variable to 0 manually: flash=0if for example I just say "flash var byte," does the PIC automatically recognize flash as a zero?
After any power up, you'll need to READ your saved settings from EEPROM to restore your user selections.
Keep in mind the EEPROM has a limited number of write/erase cycles (check data sheet for details).
Arch
Last edited by Archilochus; - 2nd June 2006 at 23:21.
OK, so I understand when you have used the PIC for a while you just simply read the EEPROM and everything is OK. What I don't understand is the very first time you power up the PIC, how do you deal with the memory? I mean, if you go to read it, it's an unknown value.
Look at the DATA command in the PBP manual. It will tell you that you can use this command to set the initial values stored in EEPROM when the device is initially programmed.
Use the WRITE command to change the values when the program is running.
Use the READ command to read the values when the program is running.
Paul Borgmeier
Salt Lake City, Utah
USA
Example 1
In the above example, when you turn your PIC on, the LED may flash an indeterminate number of times... because the contents of variable FLASH cannot be guaranteed at Power-Up and you've NOT preset the contents before using that variable.... don't assume any variable is zero following power-up, so you must use the CLEAR instruction (see manual) or preset the variable...Code:CounterA var Byte Flash var Byte LED var PortB.0 TRISB=0 For CounterA=1 to Flash High LED Pause 500 Low LED Pause 500 Next CounterA EndLoop: Pause 1000 Goto EndLoop End
Example2
Now in the above example, the LED will always flash ONCE.Code:CounterA var Byte Flash var Byte LED var PortB.0 TRISB=0 Flash=1 For CounterA=1 to Flash High LED Pause 500 Low LED Pause 500 Next CounterA EndLoop: Pause 1000 Goto EndLoop End
Let's now suppose, we want to ADD ONE FLASH to our program every time we power-up... we need to increment the current content of FLASH and save it... we will start with TWO FLASHES at initial Program time (and the first time the program is run) and increment thereafter...
Example 3
The DATA Command presets the EEPROM contents at the time you program your PIC. Don't assume your EEPROM contents are zero (unless your programmer has been set to burn the contents of your EEPROM that way), so the best way is ALWAYS to use DATA to preset the EEPROM contents that you are going to use.Code:CounterA var Byte Flash var Byte LED var PortB.0 TRISB=0 Data @0,2 Read 0,Flash For CounterA=1 to Flash High LED Pause 500 Low LED Pause 500 Next CounterA Flash=Flash+1 Write 0,Flash EndLoop: Pause 1000 Goto EndLoop End
READ now reads the EEPROM contents at Power-Up, presetting the variable FLASH. Prior to the program ending, the WRITE command sets a new value in EEPROM to be used the next time you Power-Up.
This is good for counting up to 255 flashes, because we're using a BYTE and the variable will roll through zero when we've used up all eight bits. If you need to save a WORD, you must do so as two BYTE halves, because EEPROMS only work in BYTEs. If you need to save a BIT, then you have to waste an entire BYTE to be able to do that - because EEPROMS only work in BYTES.
I trust that will answer most of your query...
Bookmarks