5

In my current project I need to store the current elapsed time in hours to retrieve it in case of power loss. Since I am using a Arduino Nano I would ideally like to use the built in EEPROM without additional hardware.

The written values and their order will always be:

0 -> 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 0 -> ....

If I write that on EEPROM per 0->0 full cycle the last bit will change 7 times and the other bits are pretty much idle. That's why I thought of distributing the changes more evenly by using instead the sequence

0 -> 1 -> 3 -> 7 -> 15 -> 31 -> 63 -> 0 -> ....

This would change every bit only once during a full 0->0 cycle. Does this improve EEPROM life expectancy or is the life expectancy linked to updating full bytes rather than individual bits?

edit: Since the question was closed for being off-topic, I altered the introduction a little bit. I am sorry if some of the comments are no longer fully valid. Since I don't fully understand why the question was closed (especially since there is EEPROM-tag), I would be grateful for a tip where on stackexchange the question would be best suited.

maxmilgram
  • 151
  • 3

2 Answers2

2

On your next design consider using FRAM, it is non volatile and supports almost unlimited (10 - 14th) read write cycles. The library I use or EEPROM supports the update function where it reads the memory first and does not write to it if it is to remain unchanged.

Gil
  • 1,863
  • 9
  • 17
1

It is possible to write to ATmega EEPROM address without clearing its previous value. The write mode is set with the EEPMx bits in the EECR register.

The avr-libc eeprom.h write functions only use the erase and write mode and the Arduino EEPROM library only uses avr-libc eeprom.h write function.

I modified the EEPROM_write example function from the datasheet to write without erase.

#include <EEPROM.h>

void setup() {

Serial.begin(115200);

int address = 0;

EEPROM.write(address, 0b11111111); Serial.println(EEPROM.read(address), BIN);

EEPROM_write_no_erase(address, 0b11111110); Serial.println(EEPROM.read(address), BIN);

EEPROM_write_no_erase(address, 0b11111101); Serial.println(EEPROM.read(address), BIN);

EEPROM_write_no_erase(address, 0b11101111); Serial.println(EEPROM.read(address), BIN);

}

void loop() {

}

void EEPROM_write_no_erase(unsigned int uiAddress, unsigned char ucData) { /* Wait for completion of previous write / while (EECR & (1 << EEPE)) ; / Set up address and Data Registers / EEAR = uiAddress; EEDR = ucData; / Write only / EECR |= (1 << EEPM1); EECR &= ~(1 << EEPM0); / Write logical one to EEMPE / EECR |= (1 << EEMPE); / Start eeprom write by setting EEPE */ EECR |= (1 << EEPE); }

the output is as expected:

11111111
11111110
11111100
11101100

I don't know if use of this feature improves the lifetime of the ATmega EEPROM.

Juraj
  • 18,264
  • 4
  • 31
  • 49