EEPROMs using the I²C protocol have become a common and practical way to store information in electronic equipment. Their non-volatile memory retains valuable data even when a device is disconnected from its power supply for a long time. Also known as serial-protocol memories, 24CXX chips are found throughout consumer electronics. Here is how they work and how to use them.

These small integrated circuits store data in an organized form that must be written correctly. Their notable features include:

  • Electrically erasable and programmable read-only memory: EEPROM.
  • A rated endurance of one million write/read cycles.
  • Data retention for hundreds of years without power.
  • Page-based organization to simplify addressing and data storage.
  • A single supply voltage, typically 5Volts or 3.3 volts.
  • Compatibility with the serial I²C protocol, a Philips trademark.
  • Very low cost.
  • A wide choice of package styles to suit the application.
8C3B.jpg
Household appliances use these memories extensively to store programming data essential to their operation

The I²C bus and its signals

The I²C bus (Inter-Integrated Circuit) is a two-wire communication system developed by Philips Semiconductor in the early 1980s to reduce the cost of electronic equipment. Its data rate is considered medium to low—400 kHz to 100 kHz. Early applications included television contrast, brightness and volume controls; today, I²C bus connections appear in computers, industrial and entertainment equipment, medical devices, military systems and many other applications.

Before I²C, memory-to-microprocessor data transfers were parallel and required packages with many pins—24, 28 or more. Pins had to serve memory addressing, selection, control and data transfer; data alone could use eight pins, with another eight for addressing. By contrast, the I²C bus provides chip-to-chip communication over just two serial wires. These are Clock (SCL) and Data (SDA), which carry addressing, selection, control and data one bit at a time.

8C3C.jpg
A 24C04 memory used in a TV

SDA carries data, while SCL synchronizes the transmitter and receiver as data moves from one IC to another. Devices on an I²C communication bus are identified as Master and Slave. The Master initiates contact and opens the bus; the Slave receives and answers. Depending on the system, a device can act only as Master, only as Slave, or take either role. The EEPROMs discussed here can participate in this arrangement.

The bus can connect up to 255 ICs on the same SDA and SCL wires. Every slave device has a unique address. When the Master sends a call, all connected ICs hear it, but only the one whose address matches responds. Data is then exchanged with that Slave until the Master closes the session.

Starting and ending a transfer

A START condition occurs when SDA changes from HIGH to LOW while SCL is HIGH. A STOP condition is the opposite transition—SDA changes from LOW to HIGH while SCL remains HIGH. The device acting as Master always generates START and STOP.

The bus is considered busy after START. It becomes free again some time after STOP; the Master determines that interval, which is specified in its data sheet.

8C3D.jpg
SDA transition from H to L and back to generate START and STOP conditions

Setting up I²C in Proton

In Proton, the program or microcontroller must be told which pins will serve as SDA and SCL. Some designs use a device without a native SSP module (Synchronous Serial Port), so the microcontroller has no hardware-dedicated pins for the bus. In that case, declare the chosen digital I/O pins in the program, before declaring variables:

DECLARE SDA_PIN PORTB.0
DECLARE SCL_PIN PORTB.1

Here SDA is port B, pin 0, and SCL is port B, pin 1. Any microcontroller port can be used if configured for digital operation. Without these declarations, the defaults are PORTA.0 for SDA and PORTA.1 for SCL.

The bus speed must also be selected. Most current devices support 400 kHz or 100 kHz. Some ICs can operate at 1 MHz, while some EEPROMs cannot exchange data above 100 kHz. To use the lower rate, add:

DECLARE SLOW_BUS=ON

Open the bus with BSTART. The microcontroller becomes the Master, ready to send the address of the Slave it wants to reach. To read a value from a memory, use:

BUSIN Control, Address, [Variable]

The Master receives a value with BUSIN and places it in a variable. The selected device receives the Control word, which identifies the device and specifies the operation; Address identifies the location to read.

To write a value to a selected Slave, use:

BUSOUT Control, Address, [Variable]

The Master sends the value held in the variable to the selected device and address. Proton and most memory manufacturers recommend waiting after a BUSOUT write to ensure the data has been stored:

DELAYMS 10

This pauses execution for 10 milliseconds. Close the bus with BUSTOP. That covers opening the bus, reading or writing a Slave, and closing the connection. The next step is to understand the Control word and the read/write Address.

Internal organization and the Control byte

I²C EEPROMs do not all use identical addressing and control schemes. The Control byte indicates the memory's bus address and whether the operation is a read or a write.

24C00

24C00 = 1|0|1|0|X|X|X|R/W

R/W means Read or Write. For a read, this bit is 1; for a write, it is 0. The X positions do not affect the result and can be either zero or one. A 24C00 stores 128 bits, arranged as one page of sixteen 8-bit bytes.

24C01, 24C02 and 24C04

24C01/24C02/24C04 = 1|0|1|0|A2|A1|A0|R/W

The first four bits remain 1010 and the last is R/W. A0, A1 and A2 correspond to pins 1, 2 and 3 in the memory data sheets.

8C3E.jpg
Pins A2, A1 and A0 select the chip addressed for the data transfer

Those three bits can address up to eight memories on one bus. The 24C04 is the exception: A0 must be tied to GND or VCC, so A1 and A2 can select only four units. In this model A0 instead directs the read/write pointer to the first or second 256-byte page. This illustrates the page-based internal layout. Memories in this group can also be combined to increase storage capacity.

The 24C02 has one page of 256 bytes; the 24C04 has two such pages, for 512 bytes total. The 24C01 and 24C02 have one page, so their Control word is 1010000X, with X determining read or write. The 24C04 has two Control words, depending on the page: 1010000X for page one and 1010001X for page two.

24C08 and 24C16

24C08/24C16 = 1|0|1|0|B2|B1|B0|R/W

These models retain the 1010 prefix and the final R/W bit, but use B2, B1 and B0 in the Control word.

8C3F.jpg
Table showing how to select pages in 24C08 and 24C16 memories

Unlike the models described above, only one 24C08 or 24C16 can be placed on a bus. Although their pin arrangement matches the earlier models, A2, A1 and A0 are not internally connected and are generally tied to GND. B2, B1 and B0 identify the page, or block, in software.

A 24C08 has four pages of 256 bytes: 4 × 256 × 8 bits = 1 KByte = 8 Kbit. A 24C16 has eight pages of 256 8-bit bytes, for 2 KByte, or 16 Kbit. As a result, the 24C08 has four Control words and the 24C16 has eight—one for each 256-byte page.

8C40.jpg
24C08 and 24C16 memories are widely used in TVs and DVD equipment to store information

The Address word

The Address word selects the location within the page chosen earlier. A 24C00 has a page of just 16 bytes, or 16 rows for storing bytes. Proton Lite accepts Control and Address values in binary, decimal or hexadecimal notation. For example:

BUSIN 161, 8, [Dato]

The Control value 161 is binary 10100001: it selects page zero and requests a read from a compatible memory. The next value, 8, identifies the byte location to read. The extracted value is placed in a BYTE variable named Dato.

To write to that location:

BUSOUT 160, 8, [Otro_Dato]

The last bit of the Control word is now zero: 160 instead of 161. The value stored in the BYTE variable Otro_Dato, declared earlier in the program, is written to the memory.

8C41.jpg
Example program for reading an I²C EEPROM with Proton

This example reads the first page of an I²C EEPROM and displays each value on an LCD for half a second before moving to the next, until all values have been read. To use another page, change the Control word as needed. The example uses a PIC16F84A, declares the SDA and SCL pins, and selects a low bus speed. The LCD is connected to the default port for this purpose, port B.

An I²C EEPROM can keep many values ready for use by a program. Additional microcontroller code can detect the loss of supply voltage and, using a backup battery, immediately save all variables to memory before shutting the system down automatically until power returns.

One example is the token counters in arcade video games, pinball machines and other coin- or token-operated amusement machines. During normal operation, the backup battery stays charged. If the power fails or the machine is disconnected for transport, the information is saved automatically for later checking. Did you know?