Chip communications using I2C protocol

V0.01 24-Feb-03

1. Introduction

The I2C protocol was invented by Philips Electronics as a simple means for a master processor to talk to a variety of other chips over a two wire interface. Each chip on the bus has a unique number (address), and when the master device sends a message, it incorporates this number. The appropriate chip will send an acknowledge when it sees its own address in the message.

The two lines are clock (SCL) and data (SDA). These are pulled up to the supply voltage (typically +5v or +3.3v) by resistors. Any of the devices on the bus can pull either of the lines low. The advantage of this system is that each device can see if any other device is talking on the line (it will be pulled low), and any collisions (two devices talking at the same time) do not cause any damage.

The chip addresses are allocated by function, for example, all real time clock chips will have the address 0x32, and all EEPROM devices 0x50.

2. I2C commands

The I2C command set consists of five basic commands:

2.1. Line conditions

The read operation and write operations are made up of the four conditions. Each condition is simply a level transition of the two lines. They are shown below:

After a start condition has been sent, the address of the device to talk to is sent. This is the 7-bit address of the device to talk to with a read (1) or write (0) bit following. For a data bit condition, the SDA line is set to the dat bit value (1 or 0), and the SCL line is raised high then low again. After the 8 bits of data, the addressed device will hold the data line low for one more clock cycle of SCL. This is to indicate that it has seen its address, and is ready to talk:

2.2. Read and write operations

A write operation is performed by sending the address as discussed above (section 2.1), then sending the commands or data, followed by a stop condition. In the diagram below, an S represents a Start condition, A is an acknowledge condition, and P is a Stop condition:

A Read operation is performed by sending the address as discussed above (section 2.1), then by the master cycling the SCL line and reading in the data. When the device being read sees the SCL line rise, it places the next bit of data on the SDA line. When the master lower the SCL line it clocks in that bit of data. When all bytes have been read, the device deliberatley does not acknowledge the last byte to inform the master of this.

The data to write and read depends on what device is being addressed. Many devices will have a set of control registers that can be written to or read from. The datasheet for each device will include details of its command set and registers.

3. Example devices

The table below shows several I2C devices. Click on the device name to open the datasheet in PDF format.

Device Type I2C address (hex)
Ricoh 5C386 Real Time Clock 32
Microchip 24LC04a EEPROM 50
Philips PCA9532 LED dimmer Cx
x=channel
Philips TDA8754 Triple 8-bit video ADC 4C and 4D

3.1. I2C enable microcontrollers

Although the I2C protocol can be fairly easily implemented using standard I/O ports, some microcontrollers incorporate I2C peripherals which make the communication process simpler in software. Some are presentd in the table below:

Manufacturer processor
Philips P87C660
SGS Thomson ST92F
Microchip PIC14000
Hitachi / Renesas H8S/2148

4. I/O port implementation

Presented here will be code that will run on most processors using only two I/O ports. The ports must be capable of being inputs or outputs. This code was originally running on a Hitachi H8/38024 processor.

5. Links

Philips I2C protocol manual
http://www.semiconductors.philips.com/acrobat/applicationnotes/AN10216_1.pdf

ePanorama has a section on the I2C protocol:
http://www.epanorama.net/links/serialbus.html#i2c

Fairchild have produced a document describing using the I2C protocol for serial EEPROMs.
http://www.fairchildsemi.com/an/AN/AN-794.pdf

Embedded Systems Academy present some code for Philips microcontrollers
http://www.esacademy.com/faq/progs/i2c.htm

Code for PIC microcontrollers
http://www.phanderson.com/PIC/PICC/CCS_PCM/24_256_1.html