How to store the last counting value in EEPROM in ATmega8 after power failure
According to the parts of the code you've posted here you might exhaust EEPROM resource too soon that way. EEPROM has a limited number of erase-write cycles per cell. You are writing every time in a infinite loop without any sort of delay or even checking that your &quot;second&quot; value has been changed since last time. Atmega8 EEPROM cycle limit are listed as 100,000 erase-write cycles.

You are doing read-modify-write when setting EEMWE:  EECR |= (1&lt;&lt;EEMWE);
But according to a datasheet you have to write zero to EEWE in the same operation. Yes, they listed this code in the datasheet but it's not really correct according to the description above. It will work if you write rarely, as after writing the hardware will clear the EEWE. But if you call this function too often, you might still be reading 1 from this bit and writing it back again resulting a loop of writing nothing due to this. I'd rather suggest changing this line to EECR = (1&lt;&lt;EEMWE);. But there are even better way below.

As you are writing in C there is no need to reinvent the wheel. All EEPROM operations are already included with all C compilers for AVR I know. If you are working with AVR-GCC they have avr/eeprom.h header which adds all of them. See it's description here: https://www.nongnu.org/avr-libc/user-manual/group__avr__eeprom.html.
They have a nice functions eeprom_update_byte(...) which will ensure that the erase-write cycle will only occur if the data has been changed.

Still that will mean that you will write every second. With 100000 writes that's less that 28 hours only. In reality EEPROM can handle much more cycles, but it's not a good thing to rely on that as manufacturer only guarantees those 100K cycles. If you really need to save state every second, you have to implement some sort of wear leveling. Which is usually done by writing to a different address each time in a circular manner. More information on this question are out of scope of this question.

Also check &quot;Preventing EEPROM Corruption&quot; paragraph of the datasheet. If you will be writing to EEPROM during power failure it might cause EEPROM corruption resulting in reading back unidentified value next time. Again there is a lot of ways to circumvent those issues and make it reliable. But those are out of scope of this topic again.