non volatile memory

I am finding it very difficult to understand how to use non volatile memory; the explanations in the manuals are surely far from clear.

Using an 18M2, I can readily create a two digit number using plus or minus buttons which for example is stored in variable b12.

I want that number to be stored in a non volatile memory such that next time the device is used, that same number is instantly available - preferably such that it will be available as a byte variable.

Quite possibly, the value that I want to store will change so I need to be able to re write the new value to the same memory location

Can someone please explain to me how do I do this...

Thanks
 

lbenson

Senior Member
Code:
symbol eepromAdr=b4

' your code to set b12 goes here

eepromAdr=7
write eepromAdr, b12

read eepromAdr,b5
sertxd(#b5)
(for whatever value of b12 you have set.)

If you powercycle the chip, the same code, omitting the "write", should print the same value, which has been stored in non-volatile eeprom memory at eeprom location 7. If you reprogram the PICAXE and don't include "#no_data", all eeprom locations will be set to zero. If you do include that directive, then eeprom will retain the value it had.
 

AllyCat

Senior Member
Hi,

Yes, with PICaxes, it's usually called the "Electrically Erasable" (EEPROM) or DATA memory. Most PICaxes have 256 locations (addresses) which the program can access with the READ and WRITE commands. It's the same memory that is written by the DATA or EEPROM command when programming and by default, if you don't specfy a value, it will be reset to zero. Hence the need to put a #NO_DATA command in every program where you don't want to clear the value(s). #NO_DATA is also commonly used to speed up the programming process a little.

Note that continually updating any single location can eventually "wear out" this type of memory. It's not normally a problem because the "life" is measured in hundreds of thousands of WRITEs (the number of READs is unlimited). But it does mean that the program should not write to this type of memory unneccessarily, for example by reading the value and not writing unless it needs to be changed. Another method (for just a few variable bytes) could be to "level" or average the wear; for example the software could implement a "round robbin" of say 10 or 20 locations, writing to the next address and clearing the present one (or setting to a value that never will be used) each time it needs to be changed. The software would then scan the locations to see which one contains the required value.

Cheers, Alan.
 
Thank you both for your replies.

I certainly have no concerns whatsoever about wearing out the memory.
As for the more vital part, I must admit that I hardly feel much wiser. I will try as suggested but even if it works, I'm really not grasping the logic of how or why. (Just trying to understand the variations in the way memory can be used is struggle enough!)


Are there any recommended publications which explain these sorts of things in far more detail and with much greater clarity than the online manuals?

Regards
 

lbenson

Senior Member
Perhaps it would help to practice in the simulator. This code will allow you to enter an eeprom address to write to, and a value to write. In the simulator, make sure you are looking at the EEPROM tab in the Memory pane.
Code:
#picaxe 08M2

pause 2000
do
  sertxd(cr,lf,"Enter address 0-255 of eeprom location to write to: ")
  serrxd b0
  sertxd(#b0,cr,lf,"Enter value to write: ")
  serrxd b1
  sertxd(#b1,cr,lf,"Write ",#b1," to address ",#b0)
  write b0,b1
loop
As you enter addresses and values you will see the eeprom memory become populated, as shown. You type the values in the "Transmit Buffer" (note "Raw" setting) and hit <Enter> or click <Send>.
08eeprom pgm.jpg

(If you change "write" to "poke" and look in the RAM tab, the same code will write to the RAM memory--note that variables b0 through b27 occupy RAM addresses 0-27.)
 
Last edited:
Thank you for that suggestion. I hadn't thought to use the simulator in that way but I can now see that this could be very helpful in working out what is happening...
 
Top