Chapter 11

I2C Bus Projects

Abstract

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.

Keywords

I2C; Mbed I2C functions; Nucleo-F411RE I2C ports; TMP102; SDA; SCL; Temperature sensor; Temperature measurement

11.1 Overview

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.

Fig. 11.1
Fig. 11.1 I2C bus with one master and three slaves.

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.

Table 11.1

Mbed I2C Master Node Functions
FunctionDescription
frequencySet the frequency of communication on the I2C bus
readRead from a slave
writeWrite to a slave
startCreate a start condition on the I2C bus
stopCreate a stop condition on the I2C bus

11.2 Nucleo-F411RE I2C GPIO Pins

There are three I2C modules on the Nucleo-F411RE development board. The following are the GPIO pins for these modules:

I2C ModuleSignalGPIO Pin
I2C1SDAPB_7, PB_9
I2C1SCLPB_6, PB_8
I2C2SDAPB_3
I2C2SCLPB_10
I2C3SDAPB_4, PC_9
I2C3SCLPA_8

11.3 Project 1—TMP102 Temperature Sensor

11.3.1 Description

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.

11.3.2 Aim

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.

11.3.3 Block Diagram

The block diagram of the project is shown in Fig. 11.2.

Fig. 11.2
Fig. 11.2 Block diagram of the project.

11.3.4 Circuit Diagram

TMP102 module (see Fig. 11.3) has the following pins:

  • GND: Power supply ground
  • Vcc: Power supply (1.4–3.6 V)
  • SDA: I2C data line
  • SCL: I2C clock line
  • ALT: Alert
  • ADD0: I2C address select
Fig. 11.3
Fig. 11.3 TMP102 module.

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:

ADD0 ConnectionI2C Address
No connection0x90 (default)
GND0x90
Vcc0x91
SDA0x92
SCL0x93

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.4
Fig. 11.4 Circuit diagram of the project.

11.3.5 The Construction

Fig. 11.5 shows the circuit built on a breadboard.

Fig. 11.5
Fig. 11.5 Circuit built on a breadboard.

11.3.6 The PDL

Fig. 11.6 shows the program PDL.

Fig. 11.6
Fig. 11.6 Program PDL.

11.3.7 Program Listing

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:

Fig. 11.7
Fig. 11.7 TMP102 registers.
00Select the Temperature Register (read only)
01Select the Configuration Register
10Select the TLOW Register
11Select 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):

  •  shutdown mode
  •  thermostat mode
  •  polarity
  •  fault queue
  •  conversion resolution
  •  one-shot mode
  •  extended conversion mode
  •  alert bit
  •  conversion rate

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.

Fig. 11.8
Fig. 11.8 The Temperature Register.

The temperature can be extracted from these two bytes by the following steps:

For positive temperatures:

  •  Shift the HIGH byte by 4 bits to the left into a 16-bit word called say, M.
  •  Shift the LOW byte by 4 bits to the right into a byte called say, L.
  •  Add M and L to find the 16-bit result.
  •  Multiply the result with 0.0625 (resolution) to find the temperature.

For negative temperatures:

  •  Shift the HIGH byte by 4 bits to the left into a 16-bit word called say, M.
  •  Shift the LOW byte by 4 bits to the right into a byte called say, L.
  •  Add M and L to find the 16-bit result.
  •  Generate 2's complement of the result (complement and add 1).
  •  Multiply the result with 0.0625 (resolution) to find the temperature.

For example, if the returned two bytes are: 0011 0010 1000 0000

  • M = 0011 0010 0000
  • L = 1000
  • M + L = 0011 0010 1000 which is equivalent to 808 in decimal. Therefore, 801 × 0.0625 = 50.5°C.

Similarly, for a negative temperature, if the returned bytes are: 1110 0111 1000 0000

  • M = 1110 0111 0000
  • L = 1000
  • M + L = 1110 0111 1000

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.

Fig. 11.9
Fig. 11.9 Program listing.

A typical display of the temperature is shown in Fig. 11.10.

Fig. 11.10
Fig. 11.10 Typical display of the temperature.

11.3.8 Suggestions for Additional Work

Modify the program given in Fig. 11.9 so that the negative temperatures can also be displayed.

11.4 Summary

In this chapter we have learned about the following:

  •  I2C bus
  •  Mbed I2C bus functions
  •  A project using an I2C compatible device

11.5 Exercises

  1. 1. Explain how the I2C bus operates.
  2. 2. Which pins can be used for the I2C interface on a Nucleo-F411RE development board.
  3. 3. A TMP102 temperature sensor chip is connected to the Nucleo-F411RE development board. Additionally an LCD is connected to the development board. Write a program to display the ambient temperature every minute.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset