Shop OBEX P1 Docs P2 Docs Learn Events
Data storage in EEprom — Parallax Forums

Data storage in EEprom

Jon MJon M Posts: 22
edited 2005-09-29 18:19 in BASIC Stamp
Hi,

I have a problem, which I am hoping all you clever bods out there can help me with.

I need to store data to eeprom on a regular basis.· However, with eeprom I can only write to a location so many times before it will fail.·Does anyone have a routine that will cycle the memory locations my data will be stored and retreived from.· The routine needs to remember the last used memory location even when the power is removed from the BS2p40.·

Thanks

Jon Millard

Comments

  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-09-27 19:22
    Are you really going to overwrite the same location a million times? Managing where you're going to write becomes a real problem, and if it is an actual problem then you might want to use an external device (like a FRAM) that has an even greater number of write-cycles.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • Jon MJon M Posts: 22
    edited 2005-09-27 19:34
    Hi Jon,

    I thought the write times was limited to about 100000 or so. Anyway, I have developed an electrical system that I use in office equipment that can be installed and operational for many years. I use the storage for life counters, document counts etc which gets stored after every batch of forms has been processed, or when document jams occur. Unfortunately, I don't think I would be able to add additional hardware without redesigning my PCB etc. Therefore I need to find a software solution if it is possible.

    the data I need to store are

    forms_low.LOWBYTE
    forms_low.HIGHBYTE
    Forms_high
    life_count
    counter

    Thanks

    Jon Millard
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-09-27 20:02
    I think it would be a pain to manage, but you could create some sort of record pointer to points to the start of your records.· I don't know what you would use to move the pointer, but when you do you'll have to transfer the data from the previous location AND store your pointer in EEPROM so that you can retrieve it after a reset.

    RecNum····· DATA·· 0

    FrmsLo0·· · DATA·· Word 0
    FrmsHi0···· DATA·· 0
    LifeCnt0··· DATA·· 0
    Cntr0·······DATA·· 0


    FrmsLo1···· DATA·· Word 0
    FrmsHi1···· DATA·· 0
    LifeCnt1··· DATA·· 0
    Cntr1······ DATA·· 0


    Since the number of bytes per "storage record" is fixed at five,·update·the·EEPROM like this:

    · READ RecNum, record
    · eePntr = FrmsLo + (record * 5)
    · WRITE eePntr, Word formsLo, formsHi, lifeCount, counter


    This doesn't address the the issue of making a copy of the previous record set which you'll want to do for retrieval after a possible reset.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • NewzedNewzed Posts: 2,503
    edited 2005-09-27 20:13
    One other problem that you may encounter is if you keep continually advancing yoiur starting point for DATA, eventually you will run into your program code, and that can cause problems.

    I don't know how often you record new data, but with an AT45DB041B EEPROM, if you recorded 8 bytes every hour the EEPROM would last for almost 8 years.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Sid Weaver
    Do you have a Stamp Tester yet?
    http://hometown.aol.com/newzed/index.html

    ·
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-09-27 20:29
    I would set a fixed number of records, and wrap back to zero with:

    · record = record + 1 // 5
    · WRITE RecNum, record


    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • K de JongK de Jong Posts: 154
    edited 2005-09-27 20:30
    Hi Jon Millard,

    Do you use real time in your system as well? Real time clocks like the DS1302/1307 have some free RAM onboard which can be made 'Non Volatile' using a backup battery or capacitor.

    You can write to this NV-RAM as many times as you like.

    Regards,

    Klaus
  • Jon MJon M Posts: 22
    edited 2005-09-27 21:28
    If only it was that simple!! Unfortunately I don't have a realtime clock so I can't use that idea either. Thanks for your help, I'll have to experiment with the ideas above and see what I can come up with. I guess I'm just trying to find a cheat around redesigning the control system.

    Thanks to you all

    Jon
  • Tracy AllenTracy Allen Posts: 6,658
    edited 2005-09-27 22:34
    Maybe something like this, that spreads out the main wear over 500 bytes of eeprom. It uses one group of eeprom locations until your counter variable gets to 250. Then it updates the group pointer, to use an new group of eeprom locations until the counter again hits 250, then the next group. Cycling back to the beginning after using 500 locations (so it won't overrun program space--you have a lot more space available if you upgrade to a BS2pe). I'm not sure which variable is the most appropriate one to use, but you get the idea, to update the group pointer only occasionally.

    READ 0,pointer
    WRITE pointer*5+1, WORD forms_low, Forms_high, life_count,  counter   ' 5 bytes?
    IF counter//250 = 0 THEN
      pointer=pointer+1//100
      WRITE pointer*5+1, forms_low.LOWBYTE, forms_low.HIGHBYTE, Forms_high, life_count,  counter
       WRITE 0,pointer
    ENDIF
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • civstercivster Posts: 17
    edited 2005-09-29 18:19
    This is the same issue I had when I designed my first application a few months ago as you can see in this post and in this one.

    My final design is to have a counter that counts how many times a "storage" cell has been written onto, and once that count reaches a particular value(in my case, 0) it moves to the next designated cell.

    In my application, a variable that will be saved to the eeprom will have eight memory cells allocated to it, four for the counters and four to store the variable value. For example, VarA will use memory address 0-7 with 0-3 being the counters with each cell having a value of 255 when the stamp is programmed. Cells 4-7 will hold the current value of the variable and the cell the holds the current value depends on which counter is being used. In other words, if counter 0 is being used, VarA will be written on 4, if counter is 2 then VarA will be written on 5, and so on.

    A routine in my application will perform the following when saving to eeprom:
    1) Determine which counter cell is being used (cell that does not have a value of 0) and its address.
    2) If all counter cells are zero, write 255 on all of them and then set the counter cell that is being used as the first cell in the counter group.
    3) Once the address of the cell counter is determined, the address of the storage cell can be determined with a simple math. In the example above, it will be the address of the cell counter plus 4.

    Here is the code for my routine:

    EEWriteVarA:
      FOR EELocation = 0 TO 3
        READ EELocation, EECntrValue
        IF (EECntrValue <> 0) THEN EXIT
      NEXT
      IF (EECntrValue = 0) THEN
        FOR EELocation = 0 TO 3
          WRITE EELocation, 255
        NEXT
        EELocation = 0
      ENDIF
      READ (EELocation + 4), EEVarValue
      IF (EEVarValue <> VarA) THEN
        WRITE (EELocation + 4), VarA
        WRITE EELocation, (EECntrValue - 1)
      ENDIF
      RETURN
    



    I hope that helps.
Sign In or Register to comment.