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.
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:
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.
|Device||Type||I2C address (hex)|
|Ricoh 5C386||Real Time Clock||32|
|Philips PCA9532||LED dimmer||Cx
|Philips TDA8754||Triple 8-bit video ADC||4C and 4D|
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:
|Hitachi / Renesas||H8S/2148|
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.
Philips I2C protocol manual
ePanorama has a section on the I2C protocol:
Fairchild have produced a document describing using the I2C protocol for serial EEPROMs.
Embedded Systems Academy present some code for Philips microcontrollers
Code for PIC microcontrollers