Introduction

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. The ATmega32 contains 1024 bytes of data EEPROM memory. It is organized as a separate data space.

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

EEPROM Registers

EEDR (EEPROM Data Register)

EEDR
D7 D6 D5 D4 D3 D2 D1 D0
MSB LSB


EECR (EEPROM Data Register)

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

EEAR (EEPROM Address Register) (EEARH and EEARL)

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


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 
 
           // Write the String at memory Location 0x00
          EEPROM_WriteString(eeprom_address,write_string); 
 
          UART_TxString("\tRead : ") ;                                  //Print the message on UART
 
          // Read the String from memory Location 0x00
          EEPROM_ReadString(eeprom_address,read_string);	
          UART_TxString(read_string);			            //Print the read String
 
	  }		
  }