Need help with storing data in eeprom.
ihmech
Posts: 179
I am controlling a water valve with a servo.· I have a variable to store open/close cycles until a reset, this is for keeping track of matainence of the servo and valve.· I want to also keep a total of open/close cycles in the eeprom, kinda like an odometer.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
"If it ain't broke, your not trying"
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
"If it ain't broke, your not trying"
Comments
But be careful -- if you "WRITE" the same location in eeprom a million times, you'll wear it out. If you're changing your water valve once every 5 minutes, this shouldn't be a problem (except for testing -- you might write way more often while testing, which would be bad). But if you're writing once a second, you can wear it out in a few weeks.
They DO make external eeproms which are quite nice and can be replaced when a location wears out.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
"If it ain't broke, your not trying"
Note your program is loaded into eeprom from "high-memory" down, while DATA is allocated from location zero up.
Note that it IS a byte, 0-255 only. You may want to consider if you'll exceed that and need to add another byte variable to keep track if you go over 255. Your code can make a word variable out of two byte variables to go up to·65535.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
·"If you build it, they will come."
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
·"If you build it, they will come."
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
"If it ain't broke, your not trying"
Note also the "Memory Map" that you can look at after a "compile" check, only tells you what the IDE will write to the BS2 -- it doesn't actually examine the BS2 eeprom.
Would I do this:
"READ 0, total_cnt"
"total_cnt = total_cnt + 1"
"WRITE 0, total_cnt"
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
"If it ain't broke, your not trying"
WRITE location, byte
READ location, byte
Did I mention that no DATA statement was necessary? [noparse]:)[/noparse]
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
·"If you build it, they will come."
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
·"If you build it, they will come."
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
"If it ain't broke, your not trying"
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
"If it ain't broke, your not trying"
And if I see even one DATA statement, so help me...
[noparse]:)[/noparse]
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
·"If you build it, they will come."
Would I do this:
"READ 0, total_cnt"
"total_cnt = total_cnt + 1"
"WRITE 0, total_cnt"
Fair enough, except to read or write 'Word' sized variables, you use the modifier "Word".
So, it would be:
· READ 0, word total_cnt
· total_cnt = total_cnt + 1
· WRITE 0, word total_cnt
OK, a word is two bytes. So if I said: READ 1, word X
then that reads a word from bytes stored in EEPROM location 1 and 2, and the next potentially unused EEPROM location would be:
READ 3, word Y which would read from EEPROM location 3 and 4, and so on.
Is that right?
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
·"If you build it, they will come."
(Fair enough, except to read or write 'Word' sized variables, you use the modifier "Word".
So, it would be:
READ 0, word total_cnt
total_cnt = total_cnt + 1
WRITE 0, word total_cnt)
I didn't have any luck with it. When I use "READ 0, Word total_cnt" I get a number of "57856" But, when I use just "READ 0, total_cnt" I get a "0". Now, I did write a value of 255 into "total_cnt" and then read it to make sure it was there. I tried adding "1" to it and then read it agian. I got a "0". It makes sense to me why it did what it did. I am guessing I need to do something with my variable in a LOWBYTE and HIGHBYTE type of thing. I'm gonna do some more reading in my books. I would like my counter at least top out at 999. I figured it would be enough, it comes out to be about 9.6 years if it cycles 2 times a week.
I just hope I don't need to use "DATA" anywhere... erco may have my legs broke. LOL!
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
"If it ain't broke, your not trying"
' {$STAMP BS2e}
' {$PBASIC 2.5}
B0=0' LSB of word variable play around with values of B0 and B1 and re-run program
B1=1' MSB of word variable
WRITE 5, B0' store B0 in EEPROM location 5
WRITE 6, B1' store B1 in EEPROM location 6
READ 5, Word W4' W4 is made up of B0 and B1, read from EEPROM locations 5 and 6
DEBUG ? W4
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
·"If you build it, they will come."
But yes, if you're gonna read it as a "word", you need to write it as a "word". Writing a BYTE, then reading a WORD, could put the lowbyte where you don't expect it.