In this tutorial we will study how to use internal EEPROM of the AVR. EEPROM is generally used when some kind of permanent storage in real time is requried.
0 AVR EEPROM.png
The ATmega32 contains 1024 bytes of data EEPROM memory. It is organized as a separate data space. The EEPROM data bytes are addressed linearly between 0 and 1023.

For demonstration we will use UART. So, it is recomended to go through UART tutorials first.

EEPROM Registers

EEDR (EEPROM Data Register)

This register contains data to be written to the EEPROM in write operation and for read operation, it has data read out from EEPROM.

EEDR
D7 D6 D5 D4 D3 D2 D1 D0
MSB - - - - - - LSB

EECR (EEPROM Control Register)

This registers controls all EEPROM operations.

EECR
D7 D6 D5 D4 D3 D2 D1 D0
- - - - EERIE EEMWE EEWE EERE

• Bits 7 to 4 – Reserved Bits

These bits are reserved bits and will always read as zero.

• Bit 3 – EERIE: EEPROM Ready Interrupt Enable

To enable the EEPROM Ready Interrupt write one to EERIE ( first set the I bit in SREG )and zero to disable it. The EEPROM Ready interrupt generates a constant interrupt when EEWE is cleared.

• Bit 2 – EEMWE: EEPROM Master Write Enable

You must write one to EEMWE to enable EEPROM write operation. If EEMWE is zero, setting EEWE will have no effect.

• Bit 1 – EEWE: EEPROM Write Enable

EEWE is the write strobe to the EEPROM. To write the value into the EEPROM this bit must be written to one after you set up address and data correctly . Before that the EEMWE bit must be set to one, otherwise no EEPROM write takes place.

• Bit 0 – EERE: EEPROM Read Enable

It is the read strobe to the EEPROM. Write one to the EERE after setting up the correct address in the EEAR Register to trigger the EEPROM read.

You should poll the EEWE bit before starting the read operation. If a write operation is in progress, it is neither possible to read the EEPROM, nor to change the EEAR Register.

EEAR (EEPROM Address Register)

• Bits 15..10 – Reserved Bits

These bits are reserved bits and will always read as zero.

• Bits 9..0 – EEAR9..0: EEPROM Address

Tthe EEPROM address, where you want to write data or read data from, write it here.

EEAR
15 14 13 12 11 10 9 8
- - - - - - EEAR9 EEAR8
7 6 5 4 3 2 1 0
EEAR7 EEAR6 EEAR5 EEAR4 EEAR3 EEAR2 EEAR1 EEAR0

EEPROM Read/Write Procedure:

Follow following steps to do EEPROM read/write operation :

Write Operation :

1. Wait till previous write operation is completed(i.e. wait till EEWE becomes zero).

2. Load the EEPROM address into EEAR at which the data has to be stored.

3. Load the data into EEDR which has to be stored in EEPROM.

4. Set the EEMWE (EEPROM Master Write Enable).

5. Within four clock cycles after 4th step, set EEWE(Eeprom Write Enable) to trigger the EEPROM Write Opeartion

Read Operation :

1. WAit for completion of previous Write operation.

2. EEWE will be cleared once EEPROM write is completed.

3. Load the EEPROM address into EEAR from where the data needs to be read.

4. Trigger the EEPROM read operation by setting EERE (EEPROM Read Enable).

5. Wait for some time (about 1ms) and collect the read data from EEDR.

Code

Since we are using UART for the demo, we will write a program to write a string at 0x00 location and read it again.

The main.c code is shown below, the complete code can be downloaded at the end.

For both writing a string to the EEPROM and reading a string from EEPROM we will use two variables viz., EEPROM Address (to store string or to read string from) and string address ( write_string and read_string in the code below).

#include <avr\io.h>    // io.h contains the defnition of all ports and SFRs //
#include "uart.h"	 //User defined UART library which contains the UART routines
#include "eeprom.h"  //User defined library which contains eeprom routines
 
/* start of the main program */
void main() 
{
  unsigned int eeprom_address=0x00;
  unsigned char write_string[] = {"Hello World"}, read_string[15];
 
  /* Initialize the Uart before Transmitting/Receiving any data */
    UART_Init();
 
   while(1)
    {
         UART_TxString("\n\rWrite : ");                       //Print the message on UART
         UART_TxString(write_string);                      //Print the String to be written 
         EEPROM_WriteString(eeprom_address,write_string);  // Write the String at memory Location 0x00
 
         UART_TxString("\tRead : ");                         //Print the message on UART
         EEPROM_ReadString(eeprom_address,read_string);   // Read the String from memory Location 0x00	
         UART_TxString(read_string);                      //Print the read String
     }		
  }


Storing Numbers

Below is the sample code to store the numbers

Video Tutorial

For those of you, who would like to watch instead of read we have made a video with all the gyan.


Downloads

Download the complete project folder from the below link: https://github.com/ExploreEmbedded/ATmega32_ExploreUltraAvrDevKit/archive/master.zip


Have a opinion, suggestion , question or feedback about the article let it out here!