Shop OBEX P1 Docs P2 Docs Learn Events
Saving Variable Values to EEPROM — Parallax Forums

Saving Variable Values to EEPROM

civstercivster Posts: 17
edited 2005-07-19 04:07 in BASIC Stamp
Is it possible to save the value of a variable to the eeprom in an event of a power failure?

My current application uses the BS2P and it interacts with a user through an LCD module and some buttons. The user will press a button to increase or decrease the variable's value, however, I don't want to write to the eeprom everytime a value is changed for its a quick way to kill the eeprom.

Any recommendations would be greatly appreciated.

P.S. This application will be powered by a power supply not batteries.

Comments

  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2005-07-18 05:13
    Hello,

    ·· Depending on the way your power supply is built, you may be able to save the values at the time of power loss.· If your filter caps are large, you can detect power loss on the main AC input to the power supply.· When this occurs the Stamp Module will then have time to write data to the EEPROM before power is lost.· Again, it depends on the power supply.

    ·· Another idea is to only write to the EEPROM when the setting changes.· I know you're worried about killing an EEPROM cell, but if it's a human controlled setting, you won't wear out the EEPROM cell that quickly.· I built a digital thermostat that stored the data in EEPROM whenever the user changed the current temperature.· I had the same concerns, but then calculated out the number of times it might be changed in a day, and divided that by the lifetime of the EEPROM (Max Writes) and it would've taken a LONG time to kill the EEPROM.

    ·· Later I moved that data to the (Battery Backed) RAM on the DS1302 RTC clock though, so I guess that points out one other unlikely possibility.· If you had another device connected that had it's own SRAM...· =)


    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
    csavage@parallax.com
  • allanlane5allanlane5 Posts: 3,815
    edited 2005-07-18 14:08
    You could create a variable, that you increment each time through the 'main' loop. If your 'main' loop takes about 5 mSec, then count to 1000 (5 seconds) and save then. You could even save only if the value has actually changed in the 5 second period.

    This should cut down dramatically on the number of writes you do to the eeprom.
  • civstercivster Posts: 17
    edited 2005-07-18 16:54
    The though of saving to eeprom only when a value has changed crossed my mind, however, I've calculated that these values will probably get changed 30 times per day. With the BS2P's eeprom rated to 100,000 write/erase cycles, that gives me an expected life of 3,333 days or 9.132 years. I'm hoping for a life expectancy of approximately 50 years (I should be retired by then).

    The power for this application is a 24VDC source which I regulate to ~5VDC.
  • allanlane5allanlane5 Posts: 3,815
    edited 2005-07-18 17:16
    So, save twice a day.
  • ForrestForrest Posts: 1,341
    edited 2005-07-18 18:06
    You should double-check those maximum EEPROM write's - I believe they're either 1,000,000 or 10,000,000.
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2005-07-18 19:03
    Forrest,

    ·· The numbers are correct.· 10,000,000 is the rating on the BS2 EEPROM.· You can find this information here:

    http://www.parallax.com/html_pages/tech/faqs/gnrl_info.asp#Loss%20of%20Program%20without%20Power



    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
    csavage@parallax.com
  • ForrestForrest Posts: 1,341
    edited 2005-07-18 20:45
    According to Page 4 of the Parallax Basic Stamp FAQ:

    The EEPROM used in the BASIC Stamp 1 and 2 is guaranteed to function properly for 40 years and for 10,000,000 write cycles per memory location. The EEPROM used in the BASIC Stamp 2e and 2sx is guaranteed for 1,000,000 write cycles per memory location.
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2005-07-18 21:58
    Forrest,

    ·· My guess, since you did not mention anything past the BS2sx and BS2e is that it's an older document.· The original poster mentioned using a BS2p, in our current documentation, which I provided a link to, that EEPROM lifetime is rated at 100,000 writes.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
    csavage@parallax.com
  • Paul BakerPaul Baker Posts: 6,351
    edited 2005-07-18 22:24
    This is somewhat of a complex solution, but it will work. You can use multiple locations to store the data value.

    Say your calculations dictate you need 4x the writing capacity of a cell, reserve 8 bytes of your eeprom for the variable (assuming you're storing an 8 bit value), the first 4 bytes are "wear counters".

    The first time the program is run, it detects that all 4 wear counters·contain $00 (or $FF depending on the unprogrammed state of the EEPROM), it will then write $FF to each location (or leaves them "as is" if $FF is the unprogrammed value). It then proceeds to write to the first data byte (5th byte) the value you wish to store, each time this is done, decrement the associated wear counter (1st byte). Once the wear counter becomes 0, that cell is "worn out" (not really, but it indicates its time to move to the next cell). At that point it proceeds to use the second data byte (6th byte) and the second wear counter (2nd byte), moving onto the next data byte and wear counter when the proceeding wear counter becomes 0. Once the last wear counter reaches 0, all wear counters are written back to $FF and the first data byte and wear counter is·used again.

    When the stamp reboots from a power failure, it looks at each wear counter until it finds one that is not 0, that is the current cell and you proceed to use that cell until the wear counter reaches 0.

    This process can be expanded to however many cells you determine you'll need for the EEPROM to last 50 years.

    One point though, the EEPROM is only rated to store data for 40 years, this isn't a problem with the data you are storing, but the actual program may begin to fail after that period because it hasn't been changed in 40 years.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·1+1=10

    Post Edited (Paul Baker) : 7/18/2005 10:27:28 PM GMT
  • civstercivster Posts: 17
    edited 2005-07-19 04:07
    Paul,

    I was thinking of a similar process that you mentioned at work today. Have a counter that will count how many times a particular eeprom location has been written with data and if that counter reaches a particular count, move to a different location.

    Now it's just a matter of coding the process.
Sign In or Register to comment.