The I2C (or interintegrated circuit) bus was invented by Philips Semiconductor in 1982 for connecting peripheral devices and microcontrollers over short distances. The bus uses two open collector (or open drain) bidirectional lines pulled up with resistors. The SDA is the serial data line and SCL is the serial clock line. Although the bus is bidirectional, data can travel only in one direction at any time. I2C is a bus with 7-bit address space and achieves bus speeds of 100 kbits/s in standard mode and 400 kbits/s in fast mode (faster bus speeds are also available with Version 2.0 of the bus protocol). This chapter describes the basic features of the I2C bus and presents the Nucleo-F411RE GPIO pins that are compatible with the I2C bus. An example I2C based project is given using the TMP102 temperature sensor chip.
I2C; Mbed I2C functions; Nucleo-F411RE I2C ports; TMP102; SDA; SCL; Temperature sensor; Temperature measurement
In this chapter we shall be developing projects using the I2C (interintegrated circuit) bus with the Nucleo-F411RE development board.
The I2C bus was invented by Philips Semiconductor in 1982 for connecting peripheral devices and microcontrollers over short distances. The bus uses two open collector (or open drain) bidirectional lines pulled up with resistors. The SDA is the serial data line and SCL is the serial clock line. Although the bus is bidirectional, data can travel only in one direction at any time. I2C is a bus with 7-bit address space and achieves bus speeds of 100 kbits/s in standard mode and 400 kbits/s in fast mode (faster bus speeds are also available with Version 2.0 of the bus protocol). Devices on the bus can be one or more master nodes and one or more slave nodes. The master nodes initiate the communication and generate the clock signals on the bus. Slave nodes receive the clock signals and respond when addressed by a master.
Fig. 11.1 shows an example I2C system with one master and three slaves. In a typical application the master initiates the communication on the bus by signaling a start condition. This is followed by 7 bits of address information (10-bit addressing mode is also available), and one data direction bit, where a LOW means that the master is writing to the slave, and a HIGH means that the master is reading from the slave. With 7 bits of address up to 128 devices can be connected to the bus. When reading and writing to the bus we have to specify the device address, register address, and the number of bytes.
Mbed supports a number of functions for both master and slave I2C bus communication. In the I2C projects in this book the Nucleo-F411RE development board will be the master and one or more slaves will be connected to the bus. Since we will be programming the master only, a list of the functions available for the master nodes is presented in Table 11.1. When creating an I2C variable we have to specify the GPIO pins for SDA and SCL.
There are three I2C modules on the Nucleo-F411RE development board. The following are the GPIO pins for these modules:
In this project the TMP102 temperature sensor module is used to read and display the ambient temperature on the PC screen. This sensor is I2C bus compatible.
The aim of this project is to show how an I2C bus compatible device can be connected to the Nucleo-F411RE development board and also how it can be programmed using Mbed.
The block diagram of the project is shown in Fig. 11.2.
TMP102 module (see Fig. 11.3) has the following pins:
The ADD0 pin is used to select the device address. By default the device address is 0x48. Four different addresses can be selected depending on where the ADD0 pin is connected to:
Fig. 11.4 shows the circuit diagram of the project. Here, the default device address of 0x90 is selected by leaving the ADD0 pin unconnected. The alert pin is used to generate a signal when the temperature is above or below a limit and this pin is not used in this project. The SDA and SCL pins of the module are connected to I2C1 pins (PB_7, I2C1 SDA, and PB_6, I2C1 SCL) of the development board. Pins Vcc and GND of the module are connected to pins + 3.3 V (pin 16, connector CN7) and GND (pin 8, connector CN7) of the development board, respectively. The sensor module has built-in 4.7 K pull up resistors on the SDA and SCL lines as required by the I2C specifications.
Fig. 11.5 shows the circuit built on a breadboard.
Fig. 11.6 shows the program PDL.
TMP102 is a very low-current (10 μA) temperature sensor chip that can be used to measure ambient temperature in the range − 40°C to + 125°C with a resolution of 0.0625°C and an accuracy of ± 0.5°C. The device has a 12-bit resolution and is I2C, Two-Wire, and SMBus compatible. TMP102 can have four different device addresses and hence up to four such devices can be connected to the bus. The programming model of the device is shown in Fig. 11.7. The Pointer Register is used to select the register to be configured and it selects the other registers as follows:
00 | Select the Temperature Register (read only) |
01 | Select the Configuration Register |
10 | Select the TLOW Register |
11 | Select the THIGH Register |
The Configuration Register is a 16-bit register and after power up or reset it is necessary to configure this register. The register is configured by sending two consecutive bytes to it. The following options can be configured (see the TMP102 data sheet for details: http://www.ti.com/lit/ds/symlink/tmp102.pdf):
In this example we will configure this register with the normal operating values by sending 0x60 followed by 0xA0. These are also the default register values after power up or reset. 0x60 sets the conversion resolution to 12 bits, continuous conversion mode. 0xA0 sets nonextended mode of operation and 4 Hz conversion rate.
The Temperature Register returns the temperature in two bytes as shown in Fig. 11.8, where 4 bits of the lower byte are cleared to 0.
The temperature can be extracted from these two bytes by the following steps:
For positive temperatures:
For negative temperatures:
For example, if the returned two bytes are: 0011 0010 1000 0000
Similarly, for a negative temperature, if the returned bytes are: 1110 0111 1000 0000
Taking the complement and adding 1 (2's complement) we get: 0001 1000 0111 + 1 = 0001 1000 1000 which is equivalent to 392. Therefore 392 × 0.0625 = 24.5°C, or − 24.5°C.
Fig. 11.9 shows the program listing (program: TMP102). At the beginning of the program GPIO pins PB_7 and PB_6 are assigned to I2C1 with the name TMP102, the device address is set to 0x90, and the registers used in the program are defined. Inside the main program function ConfigureTMP102 is called to configure the sensor to normal mode of operation. The remainder of the program runs in an endless loop. Inside this loop the temperature is read as a 12-bit data and is converted into a 16-bit data in variable M. The actual temperature in °C is then calculated and displayed on the PC screen in the format: nn.nn. Notice that only positive temperatures are displayed by this program. The loop is repeated every second.
A typical display of the temperature is shown in Fig. 11.10.
Modify the program given in Fig. 11.9 so that the negative temperatures can also be displayed.
In this chapter we have learned about the following: