Atmega EEPROM memory writing

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

Address register EEAR (EEPROM Address Register) comprises 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 – you read data from this register.

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

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

If the bit is set to ‘1’ and “I” bit in SREG, then EEPROM Ready interrupt is enabled. If the bit is set to ‘0’ – then the interrupt is disabled.

EEMWE – EEPROM Master Write Enable.

The EEMWE bit determines whether setting EEWE to one causes 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 not affect. 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 that has to be followed to write to EEPROM properly:

  1. Wait until EEWE becomes zero.
  2. Wait until SPMEN in SPMCR becomes zero – cannot be programmed during Flash write.
  3. Write a 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 to read from the Address stored in the EEAR register. Reading is done in one CPU cycle. EEPROM cannot be read while there is a writing operation going on. EEWE bit should be = 0.

Enough theories let’s see simple routines of 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 a waveform generator, it is handy to store signal (like sinusoid) values in EEPROM. It would be impossible to calculate them in real-time. Of course, you can store some calibration values or other constants.

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

9 Comments:

  1. apollo mbaziira

    hi,
    thanks for this posting. i want to use the ATMega32 to build a waveform generator and i am at a loss on how to proceed given that it is my first time to use this kind of chip.
    can you help me with some information on this?

    Best regards
    Apollo

  2. No problem with help, just be more specific where do you need it.

  3. no probs. its similar to other atmega controllers. u can be more specific

  4. Hello,
    nice post, very helpfull,

    if you could also add a small sample function call, or a tiny programm, that stores an int var for example into the eeprom, and read it out after, just to clearify how to use,

    this would be great,

    best regards,
    Fabian

  5. more explanation is required. not much beneficial for beginners.

  6. I am a Student. This is the first time using this ATMEGA 16 chip. My project is trigger controlling mechanism for machine gun, I required to store different bit pattern table for me to using while executing my program. So could you kindly provide me information and code for storing data in ATMEGA 16.It will be helpful me to continue my project. Best Regards!!!

    Thushan

  7. HI….i am an Engg. student. i am stuck up in my project using atmega16. i want to convert analog signal to digital one and then store the digital value into the memory. how can i do it? can u please guide me?

  8. Two Questions:

    Your code (matches the one of AT90CAN Datasheet on atmel.com) does not work on AT90CAN?

    2. which .h (include) header file contain the global interrupt (SREG bit) declaration?

  9. S.M. Bakhtiar

    Dear Concern ,

    The example is excellent .

    I am a beginner.Can you please give me a sample code that how to write int value and display it using read function. or any link will be help for me.

    Waiting for your reply.

Leave a Reply