Multiple eeprom writes

adub

New Member
I need to save a value to the internal eeprom often. At least once a second or even more often than that. That means in 24 hours I'd have saved to the same address at least 86400 times. In not many days that address would no longer be valid.

I know demonicpicaxeguy showed it would last longer than 100,000 writes but even 4,000,000 would still only be roughly 46 days.

Someone, maybe hippy, posted some code quite a while back that avoided saving over and over to the same internal eeprom location by saving a "pointerish" location to the actual data and rotating the saved byte through all the available eeprom addresses.

Thought it was very clever but of course I didn't think I'd ever need it so didn't save it.

Searched for eeprom. Can't believe how many times that's been used!

Anyone know which thread that was in or have the code?

Arvin
 

Dippy

Moderator
I don't have a link Arvin, sorry, same search pains as you.

If you want to save to the next EEPROM location why not just increment the Write <location> variable?

May I ask why you need to do this, as there may be another solution. Is it just in case power fails or have you run out of variables or something else?

Endurance is 'typically' 100,000 for Flash (the area where your code is stored) and 1,000,000 for the EEPROM (Data Sheet for 16F88 / PICAXE 18X).
 

demonicpicaxeguy

Senior Member
i'm not sure where the code was but it's justa case of incrementing the address what you can so is just keep going and when it gets to 255 it'll just overflow and go back to 0 again

i'm actaully going to do another test again only this time with an sd card they rate the nand memmory in that to 1millions erase cycles but i think it can handle a great deal more than that,

i'm also going to re visit the eeprom test i did because i think it could be done a great deal better with some environmental control eg temperature and different voltage levels

it brings me to somthing interesting i've noticed with the sd cards they must be 5v tolerant i've got one here thats been running on 5v perfectly for about 4 months now without a problem .,....
 

hippy

Technical Support
Staff member
I cannot find the code but suspect how it would need to be done depends a lot on why you're storing the data and how much needs to be restored after a power outage.

The only scenario I can see in my crystal ball which matches what you ask for is that you have a counter for how many things have reached the end of a production line or similar and need to restore the count as best you can after a power-outage.

In that case, at first power-up I'd clear Eeprom then have the count stored in sequential locations; the last non-zero location found after a power-up is therefore the last count captured. When a count has been written, the previously used location is zeroed. At power-up, even if it had failed during a write cycle it would be possible to find the last used count, with the error being off by one at worst ( insignificant to counts probably missed during power outage ) ...

Code:
  If Eeprom Marker not valid
    First ever execution, erase Eeprom
    Write Eeprom Marker 
  End If
  Find last used location
  Read count from last used location
  Do
    Display count
    Wait for trigger
    Increment count
    Increment location 
    Write count to new location
    Zero previous location
   Loop
The reliability can be improved ( at cost of code complexity ) by retaining the last N counts which helps determine if the last count was stored or corrupted during writing; every count should be one more than its previous.

Resetting the count to zero would be a case of zeroing all Eeprom.

The scheme can be adapted to store samples instead of just counts. There'd need to be a marker which says if the location were used or not if the the valid samples could be any of the values the location holds.

"Location" in the above is one or a set of bytes used to hold a count or sample, not simply an Eeprom byte address.
 

adub

New Member
hippy, you've done it again. The pseudo code is all I need.

It's not for me. Its for help to my son's professor and he dosen't know for sure why but the guy wants to keep track of which of many relays was last turned on and it's important to go on to the next in the series even if there's been a power outage.

Thanks all. It's always a pleasure recieving such good suggestions from so many pleasant people.

Arvin
 

hax

New Member
You could of course detect the power outage while running the picaxe on battery backup or super capacitor... Which would give the picaxe enough time to write the variable to memory... Only need a couple of seconds of backup power...
 

westaust55

Moderator
Arvin,

A thought . . .

Do you have a DS1307 RTC chip for time purposes?

If so, then consider using the spare RAM within the DS1307 (about 57 bytes from memory - no pun intended :) ) as this would/could be battery backed to hold a value thru a power outage.
 
Last edited:

MartinM57

Moderator
If you want to do a "proper job" then some Ramtron FRAM would be the right answer

You can write to it at 2000 times per second for about 10 years before it wears out and it doesn't need any battery backup

FM25640 is the part number - but be aware that it's in a pretty small package!
 

Dippy

Moderator
How about a combination of some of the above.
Fat cap on supply, ADC the supply, if you see it drooping then store variable(s) in EEPROM.
Depends on circuit and time availability of course.
 

adub

New Member
Thanks all, interesting suggestions. I'll pass them along since it wasn't even for me.

I was asked how to program it to save a number to eeprom and I told them about the wearing out of one memory location by too many saves.

hippy's solution is simple and requires nothing more than a little programming. That doesn't require any changes to the fellow's hardware. I don't even know what it's for. Art students have some "unusual" ideas. :)

Arvin
 

hippy

Technical Support
Staff member
Art students have some "unusual" ideas. :)

I think we've noticed that round here ;-) I think it's excellent you've stepped forward as the interface between the artist and the 'engineering advisors' and I'm sure everyone benefits from that. Keep asking away if you have any more questions. I think a lot of us would love to see pictures and have more info on the Art itself when completed.

One thing to note; if the data stored may itself be zero ( no relays on, say ) the way to get round that is to store the data as two chunks, one which indicates if there is data, the second what the actual data is. Zeroing the previous data is a case of clearing just the chuck which indicates that is valid data ( don't need to clear the data itself ). In this case an odd step-size through addresses stops those chunks which get more regularly erased form always falling on the same address.

The easy alternative if the relays are 'sequenced' in a pattern is to store the sequence number and make sure that runs from 1 to N.

@ westaust55, MartinM57 ...

With regard to FRAM and DS1307 etc battery backed-up RAM, the issue there is not so much the survival of the data but what happens in the rare case ( Sod's Law says "every case" ) that power dies during the actual write process, particularly where part of the data may have been written.

The best way round that, as Haxby suggests, is to have a power-fail detect which is known will last long enough for a write to complete safely. Check the power state then write or go into a tight loop.

@ Dippy : Only writing on power-fail is a good idea as long as it can be guaranteed to get the data out and written before it does expire.

Extending the idea of battery backed-up RAM, I had quite good success with the following circuit ...

Code:
           |\ |
    +V >---| >|----.----------.
           |/ |    |          |
                   |      .---^---.
                   |      |       |
                   }----->| Ix    |
              +  __|__    |       |
                 ==.==    `---.---'
                   |          |
    0V >-----------^----------'
This capacitor backs-up the SFR at $C0..$FF for those PICAXE's which have it. When the +V power fails the PICAXE gets powered from the capacitor via the internal diode which clamps the pin to the chips +V. SFR should remain intact until that drops to ~1V5 ( check particular datasheets ).

It held data for quite a while using a fairly small cap and would probably be near indefinite with an actual battery ( although just diode mixing the battery and +V might make more sense ).

The only precaution is to not draw more than 25mA or the internal diode will probably melt ( could connect to multiple pins for extra current ) and it's probably not an approved method so on your own risk be it. Make sure the pin does have an internal diode to +V !
 

Dippy

Moderator
Hippy:
"@Dippy: Only writing on power-fail is a good idea as long as it can be guaranteed to get the data out and written before it does expire. "

- well, er... umm... I won't mention Sherlock. I sort of had managed to work that one out... I don't know how to answer that without sounding a bit sarccy so I won't :)
If you've got enough capacitance to last a second or two (assuming ,all Sherlocks, that we haven't got a chuffing great Sleep) then that's easily enough to detect and store.
Saves all that writing to EEPROM and removes worry. I've used it myself.
With unregulated battery supply a bit more thought maybe required.
 

Mycroft2152

Senior Member
I need to save a value to the internal eeprom often. At least once a second or even more often than that. That means in 24 hours I'd have saved to the same address at least 86400 times. In not many days that address would no longer be valid.

I know demonicpicaxeguy showed it would last longer than 100,000 writes but even 4,000,000 would still only be roughly 46 days.

Arvin
The most elementary solution to extend the write life of the EEPROM is to write more than one data byte at a time.

Look at your program, of your have a few "spare" variables, assign the data to them and write them all at one time.

If you have 5 "spare" variables, then you have extended the life fo the EEPROM five-fold.

It is not as elegant as the FRAM or capacitor back up, but simpler and cheaper.

Myc
 

BCJKiwi

Senior Member
I would expect the point made by Westaust55 re the DS1307 is that the DS1307 would be using the coin cell backup battery as part of it's circuit and running from this battery to maintain time when the power was off. Thus the DS1307 Battery backed NV Ram would retain the data as long a the coin cell was OK (~ 10 years).
 
Top