I think Veronica is trying to do this with FLASH, though, not EEPROM, so she is trying to avoid performing an erase of the entire block very frequently. I suppose that Mike's method might allow her to only erase the block when it was full of individual "time structures". Sean On Fri, Sep 21, 2012 at 6:18 AM, Michael Rigby-Jones wrote: > > > -----Original Message----- > From: piclist-bounces@mit.edu [mailto:piclist-bounces@mit.edu] On Behalf = Of Veronica Merryfield > Sent: 21 September 2012 04:39 > To: Microcontroller discussion list - Public. > Subject: [PIC] NV counter in flash > >>Anyone else tried this? Good/bad? >> >>I have a project with a PIC24 that is using the self programming of flash= to store some >> non-volatile data including a slow count - up time in minute increments.= The counter >>is part of a larger structure of NV data. >> >>I want to avoid rewriting the whole block each time the counter is update= d and came >> up with the following. > >>Flash when erased reads all ones. Writing changes ones to zeros. Overwrit= ing can >>happen but only to change ones to zeros (I assume the PIC flash behaves l= ike other >>NOR flash in this regard). By setting aside a byte or two as a counter, e= ach count could >> be represented as a zeroed bit, each bit zeroed being an increment. When= all are >>zeroed, a real counter is updated with the number of bits at zero and the= process can >> start over. As a further enhancement, each time the block is written for= an update of >>something else, the counter would be updated from the bit count and the b= it counter >>erased to go again. > > > The way I implemented a similar scheme was to put all the data I wanted t= o update regularly in a structure, and included a counter and a checksum wi= thin the structure. I then defined an area of EEPROM to use which was a mu= ltiple of the structure size. To save the new values to EEPROM, I incremen= t the counter, update the checksum and write to the next slot in EEPROM. W= hen I reach the end of the EEPROM area I wrap back to the start. In your c= ase the counter can be the run time value. > > At power on, you scan the EEPROM area to find the latest record (i.e. hig= hest count) with a valid checksum. The location of the EEPROM slot with th= e latest record must be stored in a variable, as this determines where your= next write will start. If the time count could ever roll over then the r= ecord with the highest value is no longer the latest one. However you can = detect this by storing the minimum and maximum values when you scan the EEP= ROM. If the difference between minimum and maximum is larger than the numb= er of records in the EEPROM, then a rollover has occurred and you can then = determine the newest value that has been written after the roll over. > > This scheme spreads the EEPROM wear over a selectable range of cells, and= also protects against corruption. If a record gets corrupted the last goo= d one is automatically used. > > Mike > > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D > This e-mail is intended for the person it is addressed to only. The > information contained in it may be confidential and/or protected by > law. If you are not the intended recipient of this message, you must > not make any use of this information, or copy or show it to any > person. Please contact us immediately to tell us that you have > received this e-mail, and return the original to us. Any use, > forwarding, printing or copying of this message is strictly prohibited. > No part of this message can be considered a request for goods or > services. > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D > > -- > http://www.piclist.com PIC/SX FAQ & list archive > View/change your membership options at > http://mailman.mit.edu/mailman/listinfo/piclist --=20 http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist .