Permanent Data Storage with AVR Internal EEPROM
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. 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.
Contents
[hide]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
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
#include "uart.h" | |
#include "eeprom.h" | |
uint16_t writeNum_16 = 12345; | |
uint32_t writeNum_32 = 12345678; | |
uint16_t readNum_16; | |
uint32_t readNum_32; | |
#define num_16_address 0x00 | |
#define num_32_address 0x02 // As num_16 takes two bytes, new address will start from +2 location | |
int main(void) | |
{ | |
UART_Init(9600); | |
EEPROM_WriteNBytes(num_16_address,(uint8_t *)&writeNum_16,2); //write 2-bytes of data(writeNum_16) at 0x00. | |
EEPROM_WriteNBytes(num_32_address,(uint8_t *)&writeNum_32,4); //write 4-bytes of data(writeNum_32) at 0x02. | |
EEPROM_ReadNBytes(num_16_address,(uint8_t *)&readNum_16,2); //Read 2-bytes of data from 0x00 into readNum_16 | |
EEPROM_ReadNBytes(num_32_address,(uint8_t *)&readNum_32,4); //Read 4-bytes of data from 0x02 into readNum_32 | |
UART_Printf("num_16 = %5u num_32=%8U",readNum_16,readNum_32); | |
while (1); | |
return 0; | |
} |
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!

AVR I/O Register Configuration
In this tutorial we are going to discuss the port configuration of AVR/Atmel controllers or in general Atmega family. In this tutorial we will be using Atmega32 as reference, same will be applicable...

Basics of AVR 'C'
Let us look at the basics of 'C' for programming AVR Micrcontrollers in this tutorial. Simple stuff like setting and clearing bits is important to any project you do. It is often required to set...
AVR Hardware and Software Setup
In this tutorial we will look at the basic setup required to get started with AVR series of microcontrollers. There are two aspects to it, the software and the hardware. Fortunately, we for AVR...

AVR Timer programming
Basics Timers come in handy when you want to set some time interval like your alarm. This can be very precise to a few microseconds. Timers/Counters are essential part of any modern...