
20
ATmega8515(L)
2512A–AVR–04/02
The following codeexamplesshow one assembly and one Cfunction forwriting to the
EEPROM.Theexamples assume that interrupts are controlled(e.g.,bydisabling inter-
rupts globally) sothat no interruptswill occurduring execution of these functions.The
examples alsoassume that no Flash Boot Loader ispresent in the software. If such
codeispresent, the EEPROM write function mustalso wait for any ongoing SPMcom-
mand to finish.
Assembly Code Example
EEPROM_write:
; Wait for completion of previous write
sbic EECR,EEWE
rjmp EEPROM_write
; Set up address (r18:r17) in address register
out EEARH, r18
out EEARL, r17
; Write data (r16) to data register
out EEDR,r16
; Write logical one to EEMWE
sbi EECR,EEMWE
; Start eeprom write by setting EEWE
sbi EECR,EEWE
ret
CCode Example
void EEPROM_write(unsigned int uiAddress, unsigned char ucData)
{
/*
Wait for completion of previous write
*/
while(EECR & (1<<EEWE))
;
/* Set up address and data registers */
EEAR = uiAddress;
EEDR = ucData;
/*
Write logical one to EEMWE */
EECR |= (1<<EEMWE);
/* Start eeprom write by setting EEWE */
EECR |= (1<<EEWE);
}
Comentarios a estos manuales