ow to retain a variable value between power cycles...

Discussion in 'Embedded Systems and Microcontrollers' started by seemanta, Jan 23, 2008.

  1. seemanta

    Thread Starter New Member

    Jun 29, 2006
    I am using 8052 for my own hobby project.
    In my project, I need to maintain an array index which will be maintained by the system as long as a power is supplied to it. The index will monotonically increase with time, say for example once a day.

    However, after a reset or a power cycle when my program starts to run again, it should "remember" the last value of the index and should continue execution from that point onwards.

    Since, I would need to write to this variable, it definitely cannot be in ROM.
    Shall I use NVRAM or similar battery backed up memory?

    Problem is, I just need a 2 byte wide variable. So for just 2 bytes, is it worth buying a NVRAM memory chip? Please advise.

  2. eeboy

    Active Member

    Sep 27, 2007
    It depends on how many times you need to write this value. Adding an SPI EEPROM would be simple (no battery backup). However, an EEPROM has a limited number of write cycles.
  3. AlexR

    Well-Known Member

    Jan 16, 2008
    This is quite true but it is a rather high limit.
    I don't know about the 8052 but I do know that the eeprom in the PICs has a life of about 1 000 000 write cycles. If you write to it once a day thats about 2.7 thousand years.
  4. Papabravo


    Feb 24, 2006
    What would happen in the case of a power failure? You can't anticipate those, so you would have to write the value periodically to minimize the chance of an unexpected power cycle upsetting the apple cart.

    A better solution might be to see if the 8051 variant you are using has non-volitle memory on board. Many 8051 chips now have that feature so it won't cost very much at all. I'll bet there is even a pin compatible part somewhere you could use.

    Many 8051 parts now have writeable flash memory for the code space. You can use this for data in a pinch. If you are not currently enjoying the benefits of flash memory and in system programming you really should reevaluate your situation. ROM parts are fast becoming dinosaurs.
  5. eeboy

    Active Member

    Sep 27, 2007
    True... I missed the once per day bit in the original post.

    Papabravo brings up a good point. However, if you are stuck with your current 8052 all is not lost. You can build circuitry to detect the loss of power and write the value. You'll need to be able to buffer the supply (Super Cap is good for this) just long enough to write the value and terminate. This will be on the order of 10ms or so (write times vary). This reduces the number of writes to an absolute minimum as it's only done when necessary.
  6. mrmeval

    Distinguished Member

    Jun 30, 2006
  7. seemanta

    Thread Starter New Member

    Jun 29, 2006
    Thanks to all for your wonderful inputs. I agree with the power failure issue.

    However, I was looking more like using the variable directly from non-volatile storage so that the issue of detecting power failures etc. does not have to be considered at all. The NVM would always contain the latest copy of the variable.

    And regarding the use of flash/ISP et al, I am happy to say that I am indeed using those.
    But I am not sure if there is any way I can directly modify the flash from within my 8051 application code. The specific part that I am using here is AT89S52.

    Are there certain variants that would allow me to directly read-modify-save the non volatile area from within my application code?

    Thanks in advance!