Shop OBEX P1 Docs P2 Docs Learn Events
Need help with storing data in eeprom. — Parallax Forums

Need help with storing data in eeprom.

ihmechihmech Posts: 179
edited 2010-06-02 17:14 in BASIC Stamp
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"

Comments

  • allanlane5allanlane5 Posts: 3,815
    edited 2010-05-31 19:26
    So, I think the PBasic keywords you're looking for are "READ" and "WRITE".

    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.
  • ihmechihmech Posts: 179
    edited 2010-05-31 20:31
    This water valve will cycle once, maybe twice a week at the most. So, I don't think wearing out the eeprom will be a problem. Do I need to set aside space using DATA? Or am I thinking wrong?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    "If it ain't broke, your not trying"
  • BuGeYeSBuGeYeS Posts: 6
    edited 2010-05-31 21:22
    That would be a good idea, assign it to an address, so you can change it later if the location wears out.
  • allanlane5allanlane5 Posts: 3,815
    edited 2010-05-31 21:42
    Setting aside space using DATA would be a good idea.

    Note your program is loaded into eeprom from "high-memory" down, while DATA is allocated from location zero up.
  • ercoerco Posts: 20,256
    edited 2010-05-31 21:58
    No need to use DATA, just·WRITE a byte to location 0.

    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."
  • ercoerco Posts: 20,256
    edited 2010-05-31 22:02
    PS: Part of allanlane's warning is not to accidentally get into an endless write loop as you develop your code. You could rack up many write cycles without knowing.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·"If you build it, they will come."
  • ihmechihmech Posts: 179
    edited 2010-05-31 22:03
    I think I'm starting to understand this. So, I would need to put DATA 0 (2) at the begining of my program. And then everytime the valve cycled, I would need to read the current data value and take that value and add 1 to it. After that, I would need to write it to the eeprom. Am I correct?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    "If it ain't broke, your not trying"
  • allanlane5allanlane5 Posts: 3,815
    edited 2010-05-31 22:25
    Yes, that sounds right. And it's only "WRITE" that has that 1 million limit, "READ" can be done any number of times.

    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.
  • ihmechihmech Posts: 179
    edited 2010-05-31 23:06
    If I have "DATA 0 (2)" at the begining of my program along with "total_cnt VAR WORD".
    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"
  • ercoerco Posts: 20,256
    edited 2010-05-31 23:21
    No need to use a DATA statement with READ or WRITE to save bytes in EEPROM. Simply:

    WRITE location, byte

    READ location, byte

    Did I mention that no DATA statement was necessary? [noparse]:)[/noparse]

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·"If you build it, they will come."
  • ercoerco Posts: 20,256
    edited 2010-05-31 23:32
    ihmech: That should do it.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·"If you build it, they will come."
  • ihmechihmech Posts: 179
    edited 2010-06-01 00:26
    Ok, I got it. No need to use "DATA". Ha, Ha

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    "If it ain't broke, your not trying"
  • ihmechihmech Posts: 179
    edited 2010-06-01 01:02
    I got it figured out! I just started some new code on a new page, tweeked it, and got it working like I wanted it. So, I then incorperated it in my program and disabled it until I'm ready to put my device in service. Thanks for the help guys!!! That was the last thing I had to figure out on this project and when I have it totally done, I look forward to sharing it with everyone. THANKS!!

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    "If it ain't broke, your not trying"
  • ercoerco Posts: 20,256
    edited 2010-06-01 02:25
    XLNT! Happy to hear you're victorious and have conquered your problem. I look forward to hearing more about your project and seeing your code.

    And if I see even one DATA statement, so help me...

    [noparse]:)[/noparse]

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·"If you build it, they will come."
  • allanlane5allanlane5 Posts: 3,815
    edited 2010-06-01 13:59
    If I have "DATA 0 (2)" at the begining of my program along with "total_cnt VAR WORD".
    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
  • ercoerco Posts: 20,256
    edited 2010-06-01 17:22
    Cool, allanlane5. I didn't know you could READ words from EEPROM.

    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."
  • allanlane5allanlane5 Posts: 3,815
    edited 2010-06-01 17:57
    I believe that is true. I've never done it myself though.
  • ihmechihmech Posts: 179
    edited 2010-06-02 00:07
    Hey allanlane5, I tried what you said:

    (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"
  • ercoerco Posts: 20,256
    edited 2010-06-02 05:24
    Actually, reading (and writing) words works just fine with the little program below. Thanks, allanlane5, you taught me something new today!

    ' {$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."
  • allanlane5allanlane5 Posts: 3,815
    edited 2010-06-02 17:14
    I believe the 'word' modifier here does the LOWBYTE HIGHBYTE thing for you.

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