On 12 Dec 2017 at 11:31, Isaac M. Bavaresco wrote: > The mechanism of the sequence count works differently than the time > roll-over. >=20 > With two bytes you will have 65536 possible values, but your EEPROM > memory has room for, say, 128 records. >=20 > So if any record has sequence number more than 128 counts AHEAD of the > one you have, it is older. We could increase (double it, for instance) > this threshold to account for some lost record in-between. >=20 > You compare sequence numbers by subtracting them and doing an unsigned > compare: >=20 > if( (uint16_t)( Seq2 - Seq1 ) < THRESHOLD ) > =A0=A0=A0 { > =A0=A0=A0 /* Record with sequence Seq2 is newer than record with sequence= Seq1 */ > =A0=A0=A0 } >=20 > This method takes care of roll-over, you will never need to check for > overflow. >=20 > Example: >=20 > #define THRESHOLD=A0=A0=A0 256 >=20 > int=A0=A0=A0 NewestRecord=A0=A0=A0 =3D -1; > for( int i =3D 0; i < NumRecords; i++ ) > =A0=A0=A0 { > =A0=A0=A0 if( ValidateRecord( i )) > =A0=A0=A0 =A0=A0=A0 { > =A0=A0=A0 =A0=A0=A0 if( NewestRecord < 0 || (uint16_t)( GetSequence( i ) = - > GetSequence( NewestRecord )) < THRESHOLD ) > =A0=A0=A0 =A0=A0=A0 =A0=A0=A0 NewestRecord=A0=A0=A0 =3D i; > =A0=A0=A0 =A0=A0=A0 } > =A0=A0=A0 } >=20 > if( NewestRecord < 0 ) > =A0=A0=A0 { > =A0=A0=A0 /* There is no valid record. */ > =A0=A0=A0 } > else > =A0=A0=A0 { > =A0=A0=A0 /* This is the record with the latest saved value. */ > =A0=A0=A0 } >=20 Ok, cool, I see how that works, nice. For that matter though it is conceivable that the record counter and the ho= urs=20 counter could be the same thing. Kind of what I was thinking all along but = probably=20 never spelled it out. Count increments by a value of one each time interval= , store=20 record in next EEPROM location(s). At power up find the highest record=20 number/hours count using the method you descibe. The only slight challenge I will have, and not mentioned previously, is tha= t a service=20 person has access to change the engine hours count - for example when servi= cing=20 a machine and swapping out this module. In that one and only case the "new"= hours=20 count could be lower than any of the "old" records stored in EEPROM, or "ah= ead"=20 by more than the number of records stored. The solution would be to delete,= or=20 mark as invalid, all the old values at that time. I don't like to have code= that does=20 this, with the chance that it could run errantly, but concede it may be nec= essary. I=20 have safeguards in place to reduce the chances of the service person routin= es=20 being accessed improperly. --=20 Electronic Design Solutions 9 Titoki Place, Pukete Hamilton 3200, New Zealand P: +64 7 849 0069 M: +64 27 433 4069 --=20 http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist .