y. Using I2C bus

Basic architecture

Most of PIC18, PIC24 and PIC32 microcontrollers have an inbuilt I2C (integrated inter circuit) bus controller. I2C is 1-bit data bus with a bus master driven clock signal. The bus uses 4 lines to interconnect integrated devices. PIC microcontrollers can act as either bus masters or slaves. They can also change their roles as needed. However, there can be only one bus master at a time. I2C bus does not include a protocol to govern, which device would take the role of a bus master, but can detect a collision. Of course, the simplest bus configuration is the one with a single master and a number of slave devices. A self-powered peripheral device is connected to the bus with three lines: SDA (serial data), SCL (serial clock) and GND (ground). Other devices may get power from the bus power supply line, which usually has from +3.3 V to +5 V. All devices that are connected to the bus must be compatible with the selected bus power supply voltage. PIC microcontrollers have at least one SCL and SDA pair that is +5.5 V tolerant, so no voltage adaptors are needed, it the bus power supply voltage does not exceed this level. All devices SDA and SCL pins must operate as open collector outputs, so a pull-up resistor is needed on each line to hold the voltage level at bus power supply level, when all of the devices’ outputs are set logical “1”, or to tri-state. Each device must have its own pull-up resistors from SCL and SDA lines to the bus power supply line. If any of the devices sets a logical “0”, SDA line is pulled to +0 V. The bus master is the only device that may initiate a data transfer, by pulling SDA line low. A slave device may only send data, if requested by the bus master.

Operation

The bus master initiates the data transfer by pulling SDA line low, while SCL line is high. Then it sends 7-bit or 10-bit device address. Maximum number of slave device on an I2C bus is therefore limited to less than 128 or 1024, because a small part of the addressing range holds the reserved addresses. 7-bit or 10-bit addressing mode is selected by the bus master and must be supported by all the attached slave devices. All the slave devices must read the address and determine, which device was requested by the bus master. Only one device may recede on a particular address, but a device may use a number of addresses from 7-bit or 10-bit address range. The selected device responds by sending ACK (acknowledge) signal by pulling SDA line low. The bus master then transfers the first data byte followed by a series of data bytes. The end of data transfer is marked by NACK (negative acknowledge), when the master skips pulling SDA to logical zero. It is also possible for the master to set a repeated start condition after sending the initial data to the addressed slave device. This condition instructs the slave device to transmit data to the master.

Most slave devices contain some data and some control registers. Therefore data byte(s) immediately following the address are usually used as a slave device internal register address. The following data bytes are the new contents to be transferred to a chosen slave device internal register. If a slave device internal register is read by the master device, then address and internal slave device registers are sent followed by a repeated start condition and preparation of the master device to receive data from the slave device.

Practical considerations

The number of devices attached to the I2C bus is not only limited by the addressing mode, but also depends on the maximum capacitance of 400 pF on SDA and SCL lines. The latter limits the maximum length of the bus to a few meters. However, it is possible to lengthen the bus by lowering the data rate. I2C standard defines four clock rates: 100 kHz, 400 kHz, 1 MHz and 3.4 MHz, but it is also possible to use an arbitrary clock speed, if the all the attached devices permit it. A very low communication speed is favorable for reading remote (temperature) sensors, where the amount of transferred data per measurement is only a few bytes.

One of such sensors is ADT7410, which enables 16-bit precision on temperature range from -50 °C to +150 °C. 16-bit conversion requires 240 ms to complete, but the host computer needs only to read two bytes of information to get the temperature reading. Despite the communication overhead for sending the device address and selecting the appropriate ADT7410 registers, the number of transferred bits is about 60 per reading. This spells a minimum data transfer rate of 60 bits per 240 ms, which is 250 bits per second. It is obvious that even a 1 kbps transfer rate would be 4 times faster. Therefore the minimum clock rate for this application should around 500 Hz, even if we decide for a considerably large margin…

Examples

There are 4 programing examples on I2C bus.