-----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=20 >is part of a larger structure of NV data. > >I want to avoid rewriting the whole block each time the counter is updated= and came > up with the following. >Flash when erased reads all ones. Writing changes ones to zeros. Overwriti= ng can=20 >happen but only to change ones to zeros (I assume the PIC flash behaves li= ke other=20 >NOR flash in this regard). By setting aside a byte or two as a counter, ea= ch count could > be represented as a zeroed bit, each bit zeroed being an increment. When = all are=20 >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 bi= t counter >erased to go again. The way I implemented a similar scheme was to put all the data I wanted to = update regularly in a structure, and included a counter and a checksum with= in the structure. I then defined an area of EEPROM to use which was a mult= iple of the structure size. To save the new values to EEPROM, I increment = the counter, update the checksum and write to the next slot in EEPROM. Whe= n I reach the end of the EEPROM area I wrap back to the start. In your cas= e the counter can be the run time value. At power on, you scan the EEPROM area to find the latest record (i.e. highe= st count) with a valid checksum. The location of the EEPROM slot with the = latest record must be stored in a variable, as this determines where your n= ext write will start. If the time count could ever roll over then the rec= ord with the highest value is no longer the latest one. However you can de= tect this by storing the minimum and maximum values when you scan the EEPRO= M. If the difference between minimum and maximum is larger than the number= of records in the EEPROM, then a rollover has occurred and you can then de= termine the newest value that has been written after the roll over. This scheme spreads the EEPROM wear over a selectable range of cells, and a= lso protects against corruption. If a record gets corrupted the last good = 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 --=20 http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist .