Interfacing I2C EEPROM IC (24C04) With 8051 Microcontroller
In this article, we are going to see how to interface an EEPROM IC to 8051 microcontroller. There are different kinds of EEPROM IC available in the market. The most commonly used EEPROM family is 24CXX series devices such as 24C02, 24C04, 24C08 etc. Here we are using the most common EEPROM IC 24C04, an LCD module and AT89S52 microcontroller (8051 variant) for this tutorial. AT89S52 is a typical 8051 microcontroller manufactured by Atmel. Interfacing an EEPROM to microcontroller is pretty simple. You only need to make 2 connections between the 24C04 IC and 8051 microcontroller. So lets get to business!
An EEPROM(Electrically Erasable Programmable Read Only Memory) – is a non-volatile flash memory which has the capability to retain data even if the power is removed.
Notes On I2c EEPROM IC 24C04
In this tutorial to demonstrate interfacing EEPROM to 8051, we are using an EEPROM IC based on I2C protocol (Two Wire Protocol). It has a memory capacity of 4 kilobits. The memory is divided into 32 pages, with each page containing 16 bytes. 24C04 is an 8 pin IC. The module communicates with the microcontroller using a serial communication protocol called I2C.
The I2C bus physically consists of 2 active wires. The wires, called SDA and SCL, are both bi-directional. SDA is the Serial Data line, and SCL is the Serial CLock line. Every device connected to the bus has its own unique device address, no matter whether it is an MCU or EEPROM IC. Each of these chips can act as a receiver or transmitter, depending on the functionality.
24C04 will act as a slave in the communication network and the controller can access the slave only by initiating a start condition along with a device address and page number from which the data is to be read. There after we need to send the number of the byte in order to access the value inside.
For writing into a specified memory location we need to access the slave by initiating a start condition along with a device address and page number from which data to be written. Then we need to set the number of the byte and the data. A stop condition need to be generated in order to terminate reading or writing operation.
The interface to the 8051 is simple I2C with SDA and SCL pins connected to the any two pins of 8051. At the software side we are using a user defined library named “I2C” for I2C communication. This library allows you to communicate with I2C / TWI devices.
I hope you understood so far! Lets get to the circuit diagram! So given below is the circuit diagram to connect an external memory EEPROM to 8051.
Make the connections as shown! Now lets get to the coding part.
The Program/Code Explanation
We begin including the “LCD8bit” library into the program. It is a user defined library for interfacing microcontroller with the LCD module in 8 bit mode. In this library we included a few functions for initializing LCD, for sending commands and for sending data which is to be displayed. The port which is to be connected to the data pins of the LCD and the pins which is to be connected to the command pins can be configured in the library itself. Here we defined port0 for data pins and P2.5, P2.6, & P2.7 for command pins.
Another user defined library used here is “delay” which consist of functions named “Delay_ms()” and “Delay_us()”. Delay_ms(1000) will halt the program for 1000 millisecond(ie. One second) and Delay_us(_) function is used when we need to halt the program for few microseconds.
Here we are focusing mainly on 24C04 interfacing. As mentioned earlier a library named “I2C” is used here for I2C communication. The I2CStart() function will initiate the communication. I2CSend() is the function used for sending data and I2Cread() is for reading data from a specific memory location.
A function named “EepromReadByte()” is defined at the main program for reading data from the eeprom ic. The address of the memory location from which data to be read should be given to the function as parameter. “EepromWriteByte()” is the function used for writing data. Here along with the address of the memory location the data which is to be written should be also given as parameter.
“EepromReadNBytes()” and “EepromWriteNBytes()” are the functions which are used for continuously reading or writing bytes from a particular page. “EepromEraseAll()” is the function used for deleting the contents in the EEPROM.