Easy 24C I2C Serial EEPROM Interfacing with AVR Microcontrollers

7,702

Circuit Image

An EEPROM is a type of non-volatile memory, which means it is used for permanently storing digital data without any power supply. EEPROM stands for Electrically Erasable Programmable Read-Only Memory. The advantage of this type of ROM is that it can be electrically erased, allowing it to be prepared for storing new data. This differs from CD-R disks, which can only be recorded once. A small amount of EEPROM is also available internally on AVR chips, making it suitable for storing small volumes of data (such as a few usernames and passwords). The internal EEPROM simplifies the design. However, for larger data storage needs, typically in the range of several tens of kilobytes, an external EEPROM chip must be interfaced with the AVR microcontroller. These EEPROMs can store pictures, sounds, and long texts. Many types of EEPROM chips are available from various manufacturers, with the 24C series serial EEPROMs being a common family, offering sizes up to 128KB. They utilize the I2C interface, a widely used serial communication standard. Each storage location in the chip has a unique address ranging from 0 to 8191, which functions as storage cells. When storing and retrieving data, the specific cell location must be indicated. For example, reading location 0003 will return the value 99. Each cell can store 8 bits of data, with a range from 0 to 255 (or -128 to +127). To store larger data types, such as integers, two cells must be used. To connect an ATmega32 with a 24C64 chip, a circuit diagram is provided. Any AVR development board can be utilized for this purpose, or the circuit can be assembled on a breadboard or veroboard. A sample program demonstrates the use of external EEPROM interfacing functions, utilizing the LCD library for AVRs to display information on a 16x2 LCD. The program writes 8 kilobytes of data to a 24C64 EEPROM, filling it with the value '7', and then reads back the data to verify that all locations contain '7'. If the condition is satisfied, the screen displays a "Write Successful" message. The program has been tested on an xBoard - an advanced ATmega32 development board, but any development board with ATmega16 or ATmega32 MCUs can be used. The 24C64 was mounted on a breadboard.

#include <avr/io.h>
#include "util/delay.h"
#include "24c64.h"
#include "lcd.h"

/* A simple delay routine to wait between messages given to the user so that they have time to read them */
void Wait() {
uint8_t i;
for(i = 0; i < 100; i++) _delay_loop_2(0);
}

void main() {
// Variables
uint8_t failed;
unsigned int address;

// Initialize LCD
LCDInit(LS_BLINK);

// Initialize EEPROM
EEOpen();
_delay_loop_2(0);

LCDClear();
LCDWriteString("External EEPROM");
LCDWriteStringXY(0, 1, "Test");
Wait();

LCDClear();
LCDWriteString("Writing. ");

// Fill the entire EEPROM (8KB or 8192 bytes) with the number 7
failed = 0;
for(address = 0; address < 8192; address++) {
if(EEWriteByte(address, 7) == 0) { // Write Failure
failed = 1;
}
}

// Read back the data to verify
for(address = 0; address < 8192; address++) {
uint8_t value = EEReadByte(address);
if(value != 7) {
failed = 1;
break;
}
}

if(failed == 0) {
LCDWriteString("Write Successful");
} else {
LCDWriteString("Write Failed");
}
}

This sample program serves as a practical demonstration of interfacing with an external EEPROM, providing a foundational understanding for hardware and software interactions in embedded systems. The use of the LCD library enhances user interaction by displaying real-time feedback during the EEPROM operations.An EEPROM is kinds of novalatile memory, that means it is used for storing digital data permanently without any power suply. EEPROM stands for Electrically Erasable Programmable Read Only Memory. The advantage of these kind of ROMs is that they can be erased Electrically to make them ready for storing new data.

Compare this with a CD R disks they can be recorded only once. A small amount of EEPROM is also available internally on the AVR chips. So if the volume of data you want to store is small (say few user names and password) then you can use it. The internal eeprom makes design small and simple. But if the amount of data you want to store is large, say in order of few tens of kilobytes then you have to interface a External EEPROM chip with your AVR MCU.

You can store pictures, sound and long texts in these eeproms. Their are many kinds of EEPROM chip available from many manufactures. One very common family is 24C series serial EEPROMs. They are available upto 128KB in size. They uses I2C interface with host controller (MCU) which is a very popular serial communication standard. I will write more indept tutorial on I2C in comming days and in this tutorial I will give you easy to use function that you can use without any knowledge of I2C interface.

The chip has storage location which have their unique address ranging from 0-8191. Consider these as storage cells so while storing and retriving data you have to tell the chip which cell location you want to read. For exaple if you read location 0003 you will get 99 (see image above). Note each cell can store 8BITs of data so range you can store is 0-255 (-128 to +127). So if you want to store bigget data like int you have to store them in two cells. Connect your ATmega32 with 24C64 chip as shown in the circuit diagram. You can use any avr development board for the purpose or assemble the whole circuit in a Breadboard or Veroboard.

The following sample program demonstrate the use of external EEPROM interfaing functions. The program makes use of the LCD library for AVRs to display information in a 16x2 LCD display. The program first writes 8Kbytes of data to a 24c64 eeprom to fill the whole eeprom with `7` and then it reads back to see if all the location has 7. If the condition is met the screen shows "Write Successfull" message. I have used my xBoard - An Advance ATmega32 development board to test the routines. You can use any devboard with ATmega16 or ATmega32 MCUs. The 24C64 was mounted on a Breadboard. /* A sample program to test the Extrenal EEPROM interfacing routines. The program fills the whole EEPROM with 7 and then reads the whole EEPROM memory to see if all of them contains 7.

This helps in quick testing of you hardware /software before using these routines in your own programs. The target for this program is ATmega8, ATmega16, or ATmega32 running at 16MHz. If you use slower crystal the program will simply run slow but without any problems. This program also makes use of eXtreme Electronics 16x2 LCD Interfacing routines. See the related page for more info Author : Avinash Gupta Date : 16 March 2009 Mail : me@avinashgupta.

com Web : NOTE: IF YOU USE THIS LIBRARY IN YOUR PROGRAMS AND FINDS IT USEFUL PLEASE MENTION US IN CREDIT AND RECOMEND OTHERS. */ #include #include "util/delay. h" #include "24c64. h" #include "lcd. h" /* A simple delay routine to wait between messages given to user so that he/she gets time to read them */ void Wait() { uint8_t i; for(i=0;i<100;i+) _delay_loop_2(0); } void main() { //Varriables uint8_t failed; unsigned int address; //Initialize LCD LCDInit(LS_BLINK); //Init EEPROM EEOpen(); _delay_loop_2(0); LCDClear(); LCDWriteString("External EEPROM"); LCDWriteStringXY(0, 1, "Test"); Wait(); LCDClear(); LCDWriteString("Writting.

"); //Fill whole eeprom 8KB (8192 bytes) //with number 7 failed=0; for(address=0;address<8192;address+) { if(EEWriteByte(address, 7)=0) { //Write Fail 🔗 External reference