Position: Index > Unclassified >

Atmega EEPROM memory writing

2017-12-23 11:54  
Declaration:We aim to transmit more information by carrying articles . We will delete it soon, if we are involved in the problems of article content ,copyright or other problems.

All atmega family microcontrollers have internal EEPROM memory. They can have from 512bytes to 4kBytes. EEPROM memory has its own memory space and is linearly organized. In order to access EEPROM mempry in atmega there are three registers used: Address register, Data register and Control register.

Address register EEAR (EEPROM Address Register) is made of two 8 bit registers EEARH:EEARL. In this register the EEPROM cell address has to be loaded.

Data register EEDR (EEPROM Data Register). When writing to EEPROM the data is loaded to this register. When getting data from EEPROM a€“ you read data from this register.

Control register EECR (EEPROM Control Register) is used to control access to EEPROM memory.

EECR.PNG

EERIE – EEPROM Ready Interrupt Enable. This bit generates interrupt after write cycle is finished.

If bit is set to ’1′? and also I bit SREG, then EEPROM Ready interrupt is enabled. If bit is set to ’0′? – then interrupt is disabled.

EEMWE – EEPROM Master Write Enable.

The EEMWE bit determines whether setting EEWE to one cause the EEPROM to be written. When EEMWE is set, setting EEWE within four clock cycles will write data to the EEPROM at the selected address If EEMWE is zero, setting EEWE will have no effect. When EEMWE has been written to one by software, hardware clears the bit to zero after four clock cycles. See the description of the EEWE bit for an EEPROM write procedure.

EEWE – EEPROM Write Enable. This bit enables EEPROM writing. This is functional if EEMWE is set to ’1′? before enabling Writing to EEPROM.. There is a procedure which has to be followed in order to write to EEPROM properly:

1. Wait until EEWE becomes zero.

2. Wait until SPMEN in SPMCR becomes zero a€“ cannot be programmed during Flash write.

3. Write new EEPROM address to EEAR (optional).

4. Write new EEPROM data to EEDR (optional).

5. Write a logical one to the EEMWE bit while writing a zero to EEWE in EECR.

6. Within four clock cycles after setting EEMWE, write a logical one to EEWE.

EERE – EEPROM Read Enable. This bit enables EEPROM read from Address stored in EEAR register. Reading is done in one CPU cycle. EEPROM cannot be read while there is writing operation going. EEWE bit should be = 0?.

Enough theories lets see simple routines how EEPROM can be accessed using ASM or C language.

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

And C language 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);

}

And other pair of routines are for reading from EEPROM:


Assembly Code Example

EEPROM_read:

; Wait for completion of previous write

sbic EECR,EEWE

rjmp EEPROM_read

; Set up address (r18:r17) in address register

out EEARH, r18

out EEARL, r17

; Start eeprom read by writing EERE

sbi EECR,EERE

; Read data from data register

in r16,EEDR

ret

C example:

unsigned char EEPROM_read(unsigned int uiAddress)

{

/* Wait for completion of previous write */

while(EECR & (1<<eewe));

/* Set up address register */

EEAR = uiAddress;

/* Start eeprom read by writing EERE */

EECR |= (1<<eere);

/* Return data from data register */

return EEDR;

}

Using EEPROM can be very handy in many of your projects. For instance, if you’re going to construct wave form generator, it is handy to store signal (like sinusoid) values in eeprom as it would be impossible to calculate them in real time. Of course you can store some calibration values or other constants.

You can find more detailed EEPROM usage description in any Atmel datasheet.


Reprinted Url Of This Article:
http://www.scienceprog.com/atmega-eeprom-memory-writing/