1

I am using the internal EEPROM of atmega8A using avr's EEPROM library. My code looks like this

 #define EEPROM_ADDR    0x0A

int main(void)
{
    _delay_ms(2000);
    LED_Initialize();
    vBlink_Led(100, 2);

    //eeprom_write_byte((uint8_t*)EEPROM_ADDR, 8);
    val = eeprom_read_byte((uint8_t*)EEPROM_ADDR);
    while (1);
}

when i uncomment the line eeprom_write_byte((uint8_t*)EEPROM_ADDR, 8); and then read using val = eeprom_read_byte((uint8_t*)EEPROM_ADDR);, the correct value 8 is read. But when I comment on the line and then reflash the code, the values changes to 255.

any suggestions?

note - i have unchecked the box in avrdudes for erase flash and eeprom

Devjeet Mandal
  • 345
  • 1
  • 4
  • 23
  • If that's really your code, then how are you determining what `val` is? – Joseph Sible-Reinstate Monica Nov 18 '19 at 18:08
  • Are you really sure the flash isn't erased during programming? Make sure "preserve EEMEM" fuse is set. You should probably read out the EEPROM with your programming tool after flashing and verify that it works. – Rev Nov 19 '19 at 09:19

1 Answers1

2

Normaly, when "Chip Erase" operation is performed, the EEPROM is being cleared as well.

To prevent this, you have to program (i.e. set to zero) EESAVE fuse bit, which is 3rd bit in the high fuse byte (please refer to the datasheet chapter 29.2 Fuse Bits)

AterLux
  • 4,566
  • 2
  • 10
  • 13