How to interface EEPROM with PIC microcontroller

Microcontroller Topics
Post Reply
User avatar
Shehani
Lieutenant
Lieutenant
Posts: 61
Joined: Mon Aug 19, 2013 2:11 pm

How to interface EEPROM with PIC microcontroller

Post by Shehani » Tue Aug 27, 2013 3:36 pm

What is an EEPROM ?

EEPROM is an abbreviation for Electrically Erasable Programmable Read Only Memory and it is a Non-Volatile memory. It is used in computers and other electronic devices to store data that must me saved during no power supply. EEPROM is a class of ROM (Read Only Memory) which can be electrically erased in bit by bit and able to store new data. A small amount of EEPROM (usually 128/256 bytes) is available internally with PIC Microcontrollers. I have already posted about Using Internal EEPROM of PIC Microcontroller. But if the amount of data that we required to store in EEPROM is large, say in the order of Kilobytes then we have to interface external EEPROM with PIC Microcontroller.

There are many types of EEPROM chips are available from a number of manufactures. 24C series are the one of the most popular serial EEPROMs. It uses I2C (Inter-Integrated Circuit) bus to interface with Microcontroller and are available up to 128KB. I2C is a multi-master serial single ended computer bus used to interface low speed devices to a cellphone, embedded system, mother board or other electronic devices.

Here we are using 24C64 64K serial EEPROM. 24C64 works as a slave device on I2C bus. Register access can be obtained by implementing a START signal followed by device identification address. Then each memory locations can be accessed by using its address until a STOP condition is executed.

Pin Descriptions of 24C64 EEPROM
- A0, A1, A2 – Chip Address Inputs : These inputs are used for multiple device operations. The logic levels on
24C64 Pin Diagram
24C64 Pin Diagram
1.jpg (7.04 KiB) Viewed 3996 times
these input pins are compared with the corresponding bits in the slave address and the chip is selected if the compare is true. Thus up to eight devices can be connected to same bus using different chip select bit combinations.

- WP – Write Protect : When this pin is kept LOW (grounded) normal read and write operations are possible but when it is HIGH (Vcc) write operations will be inhibited. The internal PULL DOWN resistor of this pin keep the device unprotected when it is left floating.

- Vcc and Vss : Vcc is the positive DC supply pin. The device is able to work with in 1.8 to 5.5V range. Vss is the ground pin (0v).

- SDA – Serial Data : This is bidirectional pin used to transfer data and address to and from the device.

- SCL – Serial Clock : This pin is used to synchronize data transfer through SDA.


Device Addressing

24XX64 EEPROM Device Addressing
24XX64 EEPROM Device Addressing
2.png (11.09 KiB) Viewed 3996 times
Control Byte is the first byte received by a slave device after receiving start signal from a master device. For 24C64 first 4 bits are control code (1010) for the device identification. Next 3 bits of the control byte are Chip Select Bits (A2, A1, A0). These bits allows us to connect up to eight 24XX64 devices on the same bus. The Chip Select Bits of control byte must be correspond to the logic levels of A2, A1, A0 of the device to be selected. The last bit of Control Byte is used to define the operation to be performed. It is set to 1 when read operation is to be performed and set to 0 when write operation is to be performed.

Read the Next Page to read about reading and writing data from/to EEPROM. Please Jump to this page if you don’t need detailed explanation.

Writing Data to EEPROM


We can write data to 24C64 in two ways.

Byte Write

In this mode one byte is written at a time. After giving the START signal, the master send control byte, the control code (four bits), the chip select (three bits), and the R/W bit (which is a logic low) via I2C bus. This indicates the addressed device that, higher order address of memory location to be written will be follow after it has generated acknowledge signal. Thus next byte transmitted by master device will be higher order address and it will be stored in the address pointer of the 24XX64. The Least Significant Address byte will be followed after it has generated acknowledge signal. Then the master device will send data to written the address location. When the 24XX64 acknowledges, the master issues stop signal.
24XX64 EEPROM Byte Write
24XX64 EEPROM Byte Write
3.png (23.72 KiB) Viewed 3996 times
Note: Some delay should be given between sequential write processes otherwise write cycle may not occur.

Page Write

Page write is similar Byte write, instead of generating STOP signal master transmits up to 31 additional bytes. These are temporarily stored in the on-chip page buffer and will be written in to memory after the master has given STOP signal. If the master sends more than 32 bytes, previously written data will be over written
24XX64 EEPROM Page Write
24XX64 EEPROM Page Write
4.png (16.68 KiB) Viewed 3996 times
Note: Some delay should be given between sequential write processes otherwise write cycle may not occur.

Reading Data From EEPROM

Data can be read from 24XX64 EEPROM in 3 ways.

Current Address Read

The 24XX64 EEPROM contains an internal counter which maintains address of last memory location accessed, incremented by one internally. Thus upon receiving control byte with read/write control bit set to 1, 24XX64 issues acknowledge signal and transmits the 8-bit data word in the next memory location.
24C64 EEPROM Current Address Read
24C64 EEPROM Current Address Read
5.png (15.67 KiB) Viewed 3996 times
Random Read

Random read operation allows master to access any memory location in random manner. To perform this type of operation, we want to set the address in internal address counter to the required value. This is achieved by sending address to 24XX64 EEPROM as the part of write operation. After the acknowledge of this process, the master issues a START signal and control byte for read operation. Thus the required random memory location can be easily accessed.
24C64 EEPROM Random Read
24C64 EEPROM Random Read
6.png (20.2 KiB) Viewed 3996 times
Sequential Read

Sequential Reading is initiated in a similar way as Random Read, except that after the 24XX64 EEPROM transmits first data byte master issues acknowledge signal instead of STOP signal in Random Read operation. This will direct 24XX64 EEPROM to send next data byte.
24C64 EEPROM Sequential Read
24C64 EEPROM Sequential Read
7.png (14.98 KiB) Viewed 3996 times
MikroC Programming

MikroC Pro for PIC Microcontroller provides built-in library routines to communicate with I2C devices. Here we deals only with Byte Write and Random Read operations. Using these you can easily make programs for other operations.

MikroC Function for Write Data to EEPROM :

Code: Select all

void write_EEPROM(unsigned int address, unsigned int dat)
{
  unsigned int temp;
  I2C1_Start(); // issue I2C start signal
  I2C1_Wr(0xA0); // send byte via I2C (device address + W)
  temp = address >> 8; //saving higher order address to temp
  I2C1_Wr(temp); //sending higher order address
  I2C1_Wr(address); //sending lower order address
  I2C1_Wr(dat); // send data (data to be written)
  I2C1_Stop(); // issue I2C stop signal
  Delay_ms(20);
}
MikroC Function to Read Data from EEPROM :

Code: Select all

unsigned int read_EEPROM(unsigned int address)
{
  unsigned int temp;
  I2C1_Start(); // issue I2C start signal
  I2C1_Wr(0xA0); // send byte via I2C (device address + W)
  temp = address >> 8; //saving higher order address to temp
  I2C1_Wr(temp); //sending higher order address
  I2C1_Wr(address); //sending lower order address
  I2C1_Repeated_Start(); // issue I2C signal repeated start
  I2C1_Wr(0xA1); // send byte (device address + R)
  temp = I2C1_Rd(0u); // Read the data (NO acknowledge)
  I2C1_Stop();
  return temp;
}
Circuit Diagram for Demonstration :
Interfacing External EEPROM with PIC Microcontroller
Interfacing External EEPROM with PIC Microcontroller
8.png (215.81 KiB) Viewed 3996 times
Note: VDD , VSS of the pic microcontroller and VDD, GND of 24C64 are not shown in the circuit diagram. Both VDD ‘s should be connected to +5V and VSS to GND.

In this example we writes 00000001 to the first memory location, 00000010 to second, 000000100 to third etc sequentially up to 10000000. Then it is read sequentially and output through PORTB.

To simulate this project in Proteus you may need to connect I2C Debugger. SCL and SDA of I2C Debugger should be connected in parallel to SCL and SDA of 24C64. I2C Debugger can be found where CRO can be found in Proteus.

Article courtesy of electrosome.com
Post Reply

Return to “Microcontrollers”