EEPROM (Special Considerations)
When writing to the on-board EEPROM on an AVR device, a special sequence must be used to ensure the integrity of the EEPROM data. Following is an assembly program example that shows an EEPROM write sequence:
writeEEPROM:
   ldi   r16, (1<<EEMWE)     ; Set the EEPROM Master Write Enable bit
  (cli)                      ; (only needed if any interrupts are in use) 
   out  EECR, r16
   ldi  r16, (1<<EEWE)       ; Within 4 cycles, set the EEWE bit
   out EECR, r16
done:
  (sei)                      ; (only needed if any interrupts are in use)
The four cycle time-out for the EEPROM write is not supported by the AT90EM04 when in stopped mode. This means that single stepping this sequence will not strobe an EEPROM write.

Tip!
You can use the run to cursor to the instruction after the EEPROM write enable bit is set (labeled done in the example above), instead of doing single stepping. Or use the macro defined below. If using the macros both inside and outside an interrupt code, the cli and sei instrucion pari must be included (atomic operation).

MACRO:

.macro writeEEPROM
   ldi   r16, (1<<EEMWE)
  (cli)
   out  EECR, r16
   ldi  r16, (1<<EEWE)
   out EECR, r16
  (sei)
.endmacro
USAGE:
   writeEEPROM


See Also