Shop OBEX P1 Docs P2 Docs Learn Events
READ WRITE to EEPROM help please. — Parallax Forums

READ WRITE to EEPROM help please.

beazleybubbeazleybub Posts: 102
edited 2008-06-30 13:15 in BASIC Stamp
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

' {$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

  • Mike GreenMike Green Posts: 23,101
    edited 2008-06-29 15:07
    1) What's "result"?

    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
    READ location,temp
    IF temp <> value THEN WRITE location,value
    
  • beazleybubbeazleybub Posts: 102
    edited 2008-06-29 16:35
    Mike,

    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)
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    
    btnWrk  VAR Byte
    counter VAR Byte
    check   VAR Byte
    
     
    Main:
    
    PAUSE 10
    BUTTON 0, 1, 255, 20, btnWrk, 0, No_Press
    counter=counter+1:SEROUT 15, 84, [noparse][[/noparse]"?fthe count is:",DEC counter] ' display positive count on LCD
     
    No_Press:
    
     
    FOR check = 1 TO 10
    IF IN0 = 1 THEN GOTO Main
    PAUSE 60
    NEXT
    
     
    DO WHILE counter >= 1 'loop until counter is less than 1
    counter = counter - 1
    HIGH 14 :PAUSE 400 :LOW 14: SEROUT 15, 84, [noparse][[/noparse]"?fthe count is:",DEC counter]'-
    ' -display negitive count on LCD And flash an LED
    
    LOOP
     
    GOTO Main
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    What if there was nothing? Nothing is something!

    Post Edited (beazleybub) : 6/29/2008 4:57:10 PM GMT
  • Mike GreenMike Green Posts: 23,101
    edited 2008-06-29 16:50
    First make sure you understand how READ / WRITE / DATA work. Use the examples in the manual. Read the description in the manual again. If you don't understand something about it, ask. Try out what you do understand. Use very very simple program fragments full of DEBUG statements.

    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
  • PJAllenPJAllen Banned Posts: 5,065
    edited 2008-06-29 17:01
    You have to specify an address to WRITE to.· You'll need to WRITE to addresses where the program doesn't reside.· The Stamp program itself starts at $7FF, going backwards as your program gets longer; address $000 is the furthest away.·

    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.
  • beazleybubbeazleybub Posts: 102
    edited 2008-06-29 17:06
    But that's exactly why I came here for help.
    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!
  • beazleybubbeazleybub Posts: 102
    edited 2008-06-29 17:07
    Thanks, PJ

    ·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
  • Mike GreenMike Green Posts: 23,101
    edited 2008-06-29 17:14
    When you're stuck like that, it's often very helpful for you to state what you think you understand, otherwise we're guessing. PJ and I have different ways of looking at things. That's why these forums are so helpful. There are all kinds of vantage points. When you give more hints about what you know and understand, you make it easier and quicker for us to figure out where the gap in understanding may lie.
  • PJAllenPJAllen Banned Posts: 5,065
    edited 2008-06-29 17:32
    Keep an even keel, OM.

    Limited write cycles notwithstanding, you can still investigate the soundness of your ideas.
  • Mike GreenMike Green Posts: 23,101
    edited 2008-06-29 17:36
    Sorry. You make a good point.
  • beazleybubbeazleybub Posts: 102
    edited 2008-06-29 21:45
    I have an idea. Could I use some sort of brownout detector to monitor my circuit and if it detects a power loss situation write the count to an address on the eeprom·relying on a capacitor or external battery for the power needed to complete the write? This would save the eeprom from getting euthanized right?

    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
  • beazleybubbeazleybub Posts: 102
    edited 2008-06-29 22:25
    PJA,

    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? roll.gif

    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!
  • Mike GreenMike Green Posts: 23,101
    edited 2008-06-29 22:36
    Looks like you've got it to work. Your idea for the brown-out detector is good. Keep in mind that the Stamps already have a brown-out detector for the reset line. You're better off measuring the power input voltage in front of the regulator and filter capacitors. Eme Systems has some discussion of the use of the RCTIME statement to measure battery voltage here: www.emesystems.com. Look under Stamp / app-notes / RCTIME.
  • beazleybubbeazleybub Posts: 102
    edited 2008-06-29 23:45
    Mike said...
    Keep in mind that the Stamps already have a brown-out detector for the reset line.
    True, but I am building my prototype on the BS2 and the final product will be on the OEM BS2sx. I am aware that there are differences in the two chips.
    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!
  • MSDTechMSDTech Posts: 342
    edited 2008-06-30 00:18
    There are often many ways to approach a problem. In your case, you might want to consider just putting some battery backed up RAM into the design. I'm using the DS1302 timekeeping chip both to provide a time stamp to data reading but also to use its RAM (32 bytes) to store data I don't want to loose in case of a power outage. With a pair of AA rechargeable batteries attached to the DS1302, I calculated that it would store the data for several months. It does add a cost to the final design, so the cost sensitivity is also a concern.
    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.
  • beazleybubbeazleybub Posts: 102
    edited 2008-06-30 00:30
    Is the DS1302 easy to use with the bs2 and sx? Could you post an example of the code required to send data/values to it.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    What if there was nothing? Nothing is something!
  • ForrestForrest Posts: 1,341
    edited 2008-06-30 00:50
    You should check the Completed Projects forum - a BS2 and DS1302 project was posted yesterday.
  • beazleybubbeazleybub Posts: 102
    edited 2008-06-30 04:01
    PJA, You and Mike should be proud of mesmilewinkgrin.gif

    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!
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    
     
    counter VAR Byte
    btnWrk  VAR Byte
    answer  VAR Byte
    result  VAR Byte
    result = $00
    check VAR Byte
    
    READ 0, answer
    IF answer>=1 THEN
    counter=answer
    GOTO Power_Loss
    ELSE
    GOTO Main
    ENDIF
    
    Main:
     
    READ 0,answer
    DEBUG HOME
    DEBUG CLS
    DEBUG ? counter
    DEBUG ? answer
    BUTTON 0, 1, 255, 20, btnWrk, 0, No_Press
    counter=counter+1
    WRITE 0,counter
    
    No_Press:
     
    FOR check = 1 TO 10
    IF IN0 = 1 THEN GOTO Main
    PAUSE 60
    NEXT
     
    Power_Loss:
    
     
    DO WHILE counter >= 1 'loop until counter is less than 1
    READ 0, answer
    DEBUG HOME
    DEBUG CLS
    DEBUG ? counter
    DEBUG ? answer
    counter = counter - 1
    WRITE 0,counter
    HIGH 14 :PAUSE 400 :LOW 14: SEROUT 15, 84, [noparse][[/noparse]"?fthe count is:",DEC counter]'-
    ' -display negitive count on LCD And flash an LED
    LOOP
     
    GOTO Main
     
     
     
    
    READ 0, answer
    DEBUG ? answer
    GOTO Main
    

    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. roll.gif

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    What if there was nothing? Nothing is something!

    Post Edited (beazleybub) : 6/30/2008 4:09:41 AM GMT
  • beazleybubbeazleybub Posts: 102
    edited 2008-06-30 12:28
    Found some·SRAM here http://www.maxim-ic.com/quick_view2.cfm/qv_pk/2648

    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
  • allanlane5allanlane5 Posts: 3,815
    edited 2008-06-30 12:57
    It will wear out just the location you're writing to.

    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.
  • beazleybubbeazleybub Posts: 102
    edited 2008-06-30 13:15

    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.tongue.gif

    (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
Sign In or Register to comment.