READ WRITE to EEPROM help please.
beazleybub
Posts: 102
The basic rundown for the read and write commands located in the "what's a microcontroller?" book on pages 199-201 do not give a very detailed explanation.
Would someone please explain to me what I am doing wrong?
I am trying to have counter increment by one every time the button on P0 is pressed and then subtract it when the button is released. This is not the actual program I am working on. I just am trying to get a grip on how to write data to the eeprom store it, read it then subtract part of it. Kind of like this.
Push the button
Add +1 to variable counter and write it to eeprom so if power is lost it's still there
Release Button
Wait 10 seconds
Subtract -1 from variable counter and write the new value to eeprom
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
What if there was nothing? Nothing is something!
Would someone please explain to me what I am doing wrong?
I am trying to have counter increment by one every time the button on P0 is pressed and then subtract it when the button is released. This is not the actual program I am working on. I just am trying to get a grip on how to write data to the eeprom store it, read it then subtract part of it. Kind of like this.
Push the button
Add +1 to variable counter and write it to eeprom so if power is lost it's still there
Release Button
Wait 10 seconds
Subtract -1 from variable counter and write the new value to eeprom
' {$STAMP BS2} ' {$PBASIC 2.5} counter VAR Byte btnWrk VAR Byte answer VAR Byte result VAR Byte Main: DEBUG HOME DEBUG ? counter DEBUG ? answer BUTTON 0, 1, 255, 20, btnWrk, 0, No_Press counter=counter+1 WRITE result,counter No_Press: PAUSE 2500 READ result, answer DO WHILE answer >=0 counter=counter-1 WRITE result,counter LOOP GOTO Main
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
What if there was nothing? Nothing is something!
Comments
2) You're going to "wear out" that memory location. EEPROMs have limited write capability (maybe 100,000 times), then that location doesn't work anymore. Your DO LOOP at the end of your test program is going to sit there writing to that location until you stop the Stamp.
When you want to save information to EEPROM, test your program without actually writing to the EEPROM or insert a DEBUG statement before the WRITE so you're reminded of what's getting written to the EEPROM. It's also helpful to put a test before the WRITE so you don't write if that value is already stored there, like
Honestly I have no idea what "result" is. I just am trying to get a grip on how to read & write to the eeprom.
I thought my DO LOOP would stop when·variable counter was less than 1.
I am·attempting to do is allow the program below to not loose value if power is lost.
The program below is intended for a dollar bill changer application and if I can learn how to implement read and write into it I should be able to protect people from loosing their money if a power loss event were to happen.
Should I be writing to the eeprom like (WRITE 0, counter) instead of (WRITE counter, result)
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
What if there was nothing? Nothing is something!
Post Edited (beazleybub) : 6/29/2008 4:57:10 PM GMT
Go back to basic principles when you're trying to solve something. In your case, you want to have your unit continue after a power failure as if nothing happened. What's a power failure? Is it short? Is it long? What's your power supply like? How long can your supply provide power if its input fails? What happens if the power fails right in the middle of an operation? How long does it take to save the state of your data? Can your device be aware of its power status? How much reserve power would it need? How does your device know it's recovering from a power outage?
I get concerned when people have "no idea what 'result' is". How are you going to understand reading and writing EEPROM when you don't understand the building blocks, the pieces of information used in the process? Start with that. Read the manual. Try the examples given. If they don't work as expected, read further, figure it out through careful step-wise experimentation, or ask.
To answer your last question ... The statement is WRITE <location>,<value>. This writes an 8-bit value (<value>) into a particular EEPROM location (<location>). If you want to write a value more than 8 bits, you have to split it up into two 8-bit values. The manual gives an example of the use of the WORD prefix to do this for you. The manual also discussed the use of EEPROM memory, how your program gets put into the upper end of the EEPROM and the lower end of the EEPROM is available for data. Using a <location> of zero is a good starting point since few programs would be so large that they'd need it.
Post Edited (Mike Green) : 6/29/2008 5:06:49 PM GMT
Your example of WRITE result, counter is OK, but it doesn't give a value for result which is the destination address.
So, after you declare your variables, place a line like result = $00.· Since I'm a "magic number" guy, I'd simply go with WRITE 0, counter.
To read back the stored value, the program line would be READ 0, whatever.· You have to READ it into a variable (one other than counter or result.)
As has been noted, having a limited number of write cycles, if you write to an address often enough you'll toast that location.· What you really need is a battery-backed RAM or an external I2C eeprom (still subject to limited write cycles) which is more easily replaceable.
The manual has about three pages on the topic and only one way to skin a cat.
I have racked my brain for over 10 hours about this and keep coming back to a big fat zero.
I don't understand how it works and the "What's A Microcontroller" book didn't go deep into it.
Thanks for the principles "What happens if the power fails right in the middle of an operation?"
That's a whole different bridge to cross. I really just want to keep it simple.
I will read pages 199 - 201 again but I find that I will still draw a blank from them.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
What if there was nothing? Nothing is something!
·Guess my idea is what is toast. I really was hoping this was to be simple but with eeprom getting worn out I need to rethink my approach.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
What if there was nothing? Nothing is something!
Post Edited (beazleybub) : 6/29/2008 5:16:39 PM GMT
Limited write cycles notwithstanding, you can still investigate the soundness of your ideas.
Though I really need to get a grip on the read write first.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
What if there was nothing? Nothing is something!
Post Edited (beazleybub) : 6/29/2008 9:50:38 PM GMT
I did successfully get data written into the eeprom.
This was proven by retokenizing the program and not pressing the button.
The debug terminal displayed the last count that was stored into memory.
I am starting to understand how this works I think.
Let me see if I get this right.
Any time you want to write a value to memory you have to specify where to write it and then where the value is coming from.
Thus WRITE 0(what address to write to), counter(where the value comes from)
Because the line before my first write says counter=counter+1 the VAR stored the value 1 then my first write told the stamp to write the value from counter to eeprom address o.
Am I getting this now?
Then my read line askes for the data stored at address o and then sends it to the VAR answer then the next debug command sends the VAR answer which is one to the debug terminal.
Did I finally get the grip I was looking for? Thanks!
' {$STAMP BS2}
' {$PBASIC 2.5}
counter VAR Byte
btnWrk· VAR Byte
answer· VAR Byte
result· VAR Byte
result = $00
Main:
DEBUG HOME
DEBUG ? counter
BUTTON 0, 1, 255, 20, btnWrk, 0, No_Press
counter=counter+1
WRITE 0,counter
No_Press:
READ 0, answer
DEBUG ? answer
GOTO Main
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
What if there was nothing? Nothing is something!
Thanks for the link, I really do need to study RCTIME. I have glanced over it previously and I see how it would be useful.
Thank you again!
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
What if there was nothing? Nothing is something!
Since this is RAM, you wouldn't need to worry about wearing out the storage and it still can keep your data even when there is a power outage. So far, the system has gone through several power outages with no loss of data. The longest outage to date was 2 days while I was moving the system from my shop to its final location.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
What if there was nothing? Nothing is something!
Look at my code and see that it works. Though we know it would wear out my eeprom in a short time I still learned some extremely valuable information from you and Mike. I'd like to see if there is a way to implement this code to write to the DS1302 instead of the EEPROM. Please let me know what you think about that. Thanks guys!
Would you believe what I have learned in in under two weeks! I am so proud of myself and the community for caring enough to reach out and help others.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
What if there was nothing? Nothing is something!
Post Edited (beazleybub) : 6/30/2008 4:09:41 AM GMT
Also here http://www.mouser.com/Search/ProductDetail.aspx?qs=at%2fPaO0D8Ja5Ob3X2RA9Og%3d%3d
Can either of these be used with the Bs2 and BS2sx? Which is better?
Please tell me what you think.
I also have another question....
When you say that my program above as written will wear out my·EEPROM do you mean the entire·EEPROM·or just the address I am writing data to?
Couldn't I write my program to write to diferent addresses in sequence like 0,1,2,3 and so on and wouldn't that give me 100,000 write cycles per address? (address 0 - 10 X 100,000 = 1,000,000)
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
What if there was nothing? Nothing is something!
Post Edited (beazleybub) : 6/30/2008 12:55:03 PM GMT
And yes, writing different locations will give you additional time before you wear out your eeprom. However, an external eeprom costs $1 or so. Wearing out a $50 module in this way is not very economic. Even IF you do it 4 times slower.
Allanlane5,
My project is to be completed using an OEM BS2sx using the 602-00013 external EEPROM not a module.
Anyway,·I suggested earlier to use some NVRAM which I posted a link to above this post.
I feel like it would be better than slow cooking an EEPROM anyway.
I just asked that question for mainly learning purposes.
Thanks for your input.
(By the way..Is that a monkey inside the spacesuit?! Or do I need my eyes checked?!)
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
What if there was nothing? Nothing is something!
Post Edited (beazleybub) : 6/30/2008 1:25:24 PM GMT