How to increment EEPROM value

Hi,
I’m developing a firmware to count how many times board gets reset and save the count in EEPROM. I tried it with uint8_t resetValue and it gives me 255 output. Can someone tell me how to do it?

regards,
V.

Hello,

I am not sure I am understand

uint8_t resetValue; // Holds an indeterminate value
uint8_t resetValue = 0;
resetValue++; // increment +1

255 is max value of uint8_t

BR, Jan

1 Like

The default value of unprogrammed EEPROM memory is 0xFF.

1 Like

Most EEPROMs are erased with a special erase command which is (relatively) slow, only operates on full words, causes wear and sets all bits to 1s.
After that you can clear bits but once a bit is cleared it can not be set (without erase).

To use EEPROM as a counter you need to view it as a series of bits rather than a number.
At each STARTUP clear the next bit.
For example if the EEPROM is 32 bit word based after the erase it will be FFFFFFFF.
After the first startup clear the first bit resulting in 7FFFFFFF. On each startup scan for the next bit that is on and clear it. so that after 11 startups you end up with 000FFFFFF, after 12 0007FFFF, after 13 0003FFFF and so own.
After 31 startups you would have 00000000 and you need to decide to either stop counting and consider more than 31 resets to be “many many”
or
extend the count into another word allowing for another 32 startups…
or
erase the word i.e. setting it back to FFFFFFFF but on the next startup you are unable to tell if you had 2 startups or 33 .

If you are transmitting the “counter” bits than you can maintain a “wrap” counter at the other end.

1 Like

Thank you for all your replies. It works now.