Saving Data in EEPROM

B4Lamb

Member
Hi,
I'm using a PicAxe 20X2 in a project and wanted to save some variables in EEPROM so when powering up I can continue to use the last values of the variables from the last use of the device. The EEPROM/Data method is no use for variables I found out. Is there any other method other than adding external I2C memory??
B4Lamb.
 

PhilHornby

Senior Member
Variables are stored in EEPROM using the WRITE command and can be retrieving using the READ command. They can be preset and downloaded with the program, using the EEPROM statement.

See here: read - BASIC Commands - PICAXE for some example code.

(Just don't keep repeatedly excessively overwriting the same location - else you'll wear it out.)
 

B4Lamb

Member
That's brilliant... thank you.
If there is one suggestion id make about the manual part 2 is that it doesn't list similar instructs. E.g look at x y z as alternative/ related instructions.
I'm aware of the number of write cycles so would only use sparingly within an if then test to limit the writes to that necessary.
 

Goeytex

Senior Member
You can certainly save the variables to EEPROM as Phil has shown. However, there will need to be a method that writes writes the variables to EEPROM prior to powering off the Picaxe. You could initiate a "Save to EEPROM" routine based upon a regular interval (Timer) or based upon another event such as an external pushbutton.

Another option might be to use a transistor switching/latching circuit to power the Picaxe On/Off. And then use a Push Button to initiate a power off routine that tells the Picaxe to: 1. Cleanly stop what it is doing, 2. Save variables to EEPROM and 3. Release the latch that is keeping the power on. This will require a couple of Transistors a few resistors, 2 momentary switches and 2 I/O pins.

There are of course other ways I just gave a few.
 

AllyCat

Senior Member
Hi,
If there is one suggestion id make about the manual part 2 is that it doesn't list similar instructs. E.g look at x y z as alternative/ related instructions.
One problem is that PICaxe Basic has so many "instructs" that quite a lot are "similar / alternative / related". For example: READ / WRITE , PEEK / POKE , GET / PUT , TABLE , EEPROM / DATA , LOOKUP / LOOKDOWN , before considering other "assignments" such as LET (or =) , SETBIT / CLEARBIT , SWAP , etc..

But if you look at the online Tab : PICAXE > Basic Commands > Command categories > Variables , you will find all those grouped together, with a few more that you might not have considered ;) But that still doesn't cover other useful / related keywords such as @BPTR and TIME , etc., (because they're not Commands but Variables themselves) !

Cheers, Alan.
 

westaust55

Moderator
@B4Lamb,
Without knowing what hardware you have,
How many bytes of data you wish to save,
And noting that you mention preference not to add external i2c memory,

If your project happens to include a Real Time Clock, then many RTC devices include some RAM memory that is battery backed (when the RTC has a battery connected).
From memory: :)
The DS1307 has 64 bytes,
the DS3232 has 256 bytes.
 

B4Lamb

Member
You can certainly save the variables to EEPROM as Phil has shown. However, there will need to be a method that writes writes the variables to EEPROM prior to powering off the Picaxe.
Thanks Goeytex. Its only a few variables that need to be saved ( the mode it is operating in and the display page it was using) . These byte variables are only changed in use by the operation of external switches so its easy to save these at the time they are changed and not have to wait until just before power down. They will be fairly infrequently changed so insignificant risk of damage to the memory during its operational life.
Indeed I have implemented the Write and Read instructions now into the code and it works well so thank you all.
 

B4Lamb

Member
Thanks Alan,
Yes you are correct, so many instructions. I'm used to assembly code of PICs and the original ones of those that only have 37 instructions.
I assume being targeted for educational needs PicAxe has deliberately maintained older instructions and not deprecated many so as to employ the same compiler for the full range of devices it supports. Its not a problem, I will just have to do more reading.
Cheers
John (aka B4lamb)
 

inglewoodpete

Senior Member
One problem is that PICaxe Basic has so many "instructs" that quite a lot are "similar / alternative / related". For example: READ / WRITE , PEEK / POKE , GET / PUT , TABLE , EEPROM / DATA , LOOKUP / LOOKDOWN , before considering other "assignments" such as LET (or =) , SETBIT / CLEARBIT , SWAP , etc..

But if you look at the online Tab : PICAXE > Basic Commands > Command categories > Variables , you will find all those grouped together, with a few more that you might not have considered ;) But that still doesn't cover other useful / related keywords such as @BPTR and TIME , etc., (because they're not Commands but Variables themselves) !
Manual 2 (Commands) makes great bedtime reading. Initially, you read about the all of commands:). Then you fall asleep :sleep: and, finally, you dream about PICAXEs o_O.
 

AllyCat

Senior Member
Hi,
... many RTC devices include some RAM memory that is battery backed (when the RTC has a battery connected). ....
the DS3232 has 256 bytes.
Sadly, the vast majority (I'm tempted to say "all") of the low-cost RTC breakout modules use the DS3231, which does not have any RAM. For a particular project I wanted some external I2C RAM so used the MCP7940 (DIP chip), but the additional hassle of fitting an external oscillator and battery was "annoying". Also with the much lower accuracy and higher voltage requirement of the DS1307, and apparently much higher price of single DS3232 chips, compared with the excellent little "3231 for (Raspberry) Pi" or fully assembled DS3231 modules, often including a 24LC32 EEPROM and battery/holder, etc., the choice becomes almost a "no brainer" for the majority of PICaxe users.
I'm used to assembly code of PICs and the original ones of those that only have 37 instructions.
I assume being targeted for educational needs PicAxe has deliberately maintained older instructions and not deprecated many so as to employ the same compiler for the full range of devices ...
Yes, these PICs do have a "minimal" instruction set, even compared with other 8-bit micros. I found it quite frustrating (having started with Z80 , 6502 and 8051, etc.) but I do quite like the "elegant simplicity" of the PIC's Skip instruction(s). I understand why PICaxe Basic has "so many" instructions, but do wish they were "categorised" and/or divided into several different "Instruction Lists", targeted at the different types of user. For example almost half of all the Instructions are applicable only to the X2 family, which I believe the majority of "educational" users don't need. Conversely, there are a few instructions (such as Forward and Backward) which are targeted at "novice" or special application users and a number of the "Legacy" instructions are actually marked as "deprecated".

Then there are quite a lot of "Pseudo" (or system macro) instructions, which can be very useful, but I would prefer it to be clearer that they may carry a considerable speed and/or program size penalty, compared with a "Do It Yourself" strategy. ;) There are actually numerous "compilers" because each PICaxe chip has its own particular pinout, instruction set and limitations. Finally, there are a few instructions that are completely undocumented, which you only discover when you are hit by a "Reserved Word" syntax error. :(

Cheers, Alan.
 

J G

Active member
Finally, there are a few instructions that are completely undocumented, which you only discover when you are hit by a "Reserved Word" syntax error.
There's also the odd instruction that is implemented differently from the manual or not implemented at all, such as the #UNDEF preprocessor directive in PE6 :) (not that I've had that much cause to use it though).
This will not preprocess:
Code:
#DEFINE ABC
#UNDEF ABC
 

AllyCat

Senior Member
Hi,

The online Command Syntax says that the inverse of #define is #undefine , which does appear to work. It's only the #ifdef and #ifNdef that are abbreviated. ;) But I wish I could remember my example of an undefined command.

Cheers, Alan.
 

J G

Active member
Hi,

The online Command Syntax says that the inverse of #define is #undefine , which does appear to work. It's only the #ifdef and #ifNdef that are abbreviated. ;) But I wish I could remember my example of an undefined command.

Cheers, Alan.
Ok. Thanks for that. Must be the manuals I am using then (Manual 2, 7.9.2, 2015 pdf). Looking at the online one also presented me with the #legacy directive that isn't in the pdf manuals, which I wish I had know about a few weeks ago - would have saved quite some time sitting on the floor of a pump shed trying to get code small enough to fit :).
 

Goeytex

Senior Member
I recently had to write a fairly complex program in MPASM for which I am no expert. It had to support nearly 100 different PIC 18F chips. About 2 weeks in I started having dreams about little #IFDEF, MOVLW and BANKSEL critters causing traffic jams and eating pizza. Be grateful for BASIC.
 
Top