Mbed Course Notes - Serial I2c
Mbed Course Notes - Serial I2c
These course notes are written by R.Toulson (Anglia Ruskin University) and T.Wilmshurst
(University of Derby). (c) ARM 2012
These course notes accompany the textbook “Fast and effective embedded system design :
Applying the ARM mbed”
1
Serial communications with I2C
• Introducing I2C
• Evaluating simple I2C communications
• I2C on the mbed
• Working with the TMP102 I2C temperature sensor
• Working with the SRF08 ultrasonic rangefinder
• Interfacing multiple devices on a single I2C bus
• Extended exercises
2
Introducing I2C
• I2C only uses two physical wires, this means that data only
travels in one direction at a time.
3
Introducing I2C
• The I2C protocol is a two-wire serial bus:
• The I2C communication signals are serial data (SDA) and serial clock (SCL)
– These two signals make it possible to support serial communication of
8-bit data bytes, 7-bit device addresses as well as control bits
– Using just two wires makes it cheap and simple to implement in
hardware
4
Evaluating simple I2C communications
– Each I2C-compatible slave device has a predefined device address. The slaves
are therefore responsible for monitoring the bus and responding only to data
and commands associate with their own address.
– This addressing method, however, limits the number of identical slave devices
that can exist on a single I2C bus, as each device must have a unique address.
For some devices, only a few of the address bits are user configurable.
5
Evaluating simple I2C communications
• A data transfer is made up of the Master
signalling a Start Condition, followed by
one or two bytes containing address and
control information.
6
Evaluating simple I2C communications
• The byte following the Start condition is made up of seven address bits, and one
data direction bit (Read/Write)
• All data transferred is in units of one byte, with no limit on the number of bytes
transferred in one message.
• Each byte must be followed by a 1-bit acknowledge from the receiver, during
which time the transmitter relinquishes SDA control.
7
I2C on the mbed
The mbed I2C library functions are shown in the table below:
I2C An I2C Master, used for communicating with I2C slave devices
Functions Usage
I2C Create an I2C Master interface, connected to the specified pins
frequency Set the frequency of the I2C interface
read Read from an I2C slave
read Read a single byte from the I2C bus
write Write to an I2C slave
write Write single byte out on the I2CC bus
start Creates a start condition on the I2C bus
stop Creates a stop condition on the I2C bus
8
I2C on the mbed
9
Evaluating the TMP102 I2C temperature sensor
• Configuration and data register details are given in the TMP102 data sheet
http://focus.ti.com/lit/ds/sbos397b/sbos397b.pdf
10
Interfacing the TMP102 with the mbed
TMP102 Mbed
Signal Notes
Pin Pin
Vcc (3.3V) 1 40
Gnd (0V) 4 1
2.2kΩ pull-up
SDA 2 9
to 3.3V
2.2kΩ pull-up
SCL 3 10
to 3.3V
11
Working with the TMP102 I2C
temperature sensor
12
Working with the TMP102 I2C
temperature sensor
The following program will configure the TMP102 sensor, read data, convert data
to degrees Celsius and then display values to the screen every second:
#include "mbed.h“
I2C tempsensor(p9, p10); //sda, sc1
Serial pc(USBTX, USBRX); //tx, rx
const int addr = 0x90;
char config_t[3];
char temp_read[2];
float temp;
int main() {
config_t[0] = 0x01; //set pointer reg to 'config register'
config_t[1] = 0x60; // config data byte1
config_t[2] = 0xA0; // config data byte2
tempsensor.write(addr, config_t, 3);
config_t[0] = 0x00; //set pointer reg to 'data register'
tempsensor.write(addr, config_t, 1); //send to pointer 'read temp'
while(1) {
wait(1);
tempsensor.read(addr, temp_read, 2); //read the two-byte temp data
temp = 0.0625 * (((temp_read[0] << 8) + temp_read[1]) >> 4); //convert data
pc.printf("Temp = %.2f degC\n\r", temp);
}
}
13
Evaluating the SRF08 ultrasonic rangefinder
• Configuration and data register details are given in the SRF08 data sheet:
http://www.rapidonline.com/netalogue/specs/78-1086.pdf
14
Interfacing the SRF08 with the mbed
• The SRF08 ultrasonic range finder is an I2C device which
measures distance and proximity of items
• The SRF08 can be connected to the mbed as shown:
15
Working with the SRF08 ultrasonic
rangefinder
• Exercise 2: Configure the SRF08 ultrasonic rangefinder to update distance
data to the screen. The following code will perform this operation:
#include "mbed.h"
I2C rangefinder(p9, p10); //sda, sc1
Serial pc(USBTX, USBRX); //tx, rx
const int addr = 0xE0;
char config_r[2];
char range_read[2];
float range;
int main() {
while (1) {
config_r[0] = 0x00; //set pointer reg to ‘cmd register'
config_r[1] = 0x51; // config data byte1
rangefinder.write(addr, config_r, 2);
wait(0.07);
config_r[0] = 0x02; //set pointer reg to 'data register'
rangefinder.write(addr, config_r, 1); //send to pointer 'read range'
rangefinder.read(addr, range_read, 2); //read the two-byte range data
range = ((range_read[0] << 8) + range_read[1]);
pc.printf("Range = %.2f cm\n\r", range); //print range on screen
wait(0.05);
}
}
16
Working with the SRF08 ultrasonic
rangefinder
• Exercise 3:
Evaluate the SRF08 datasheet to understand the different control
setup and data conversion options.
Now configure the SRF08 to accurately return data in inches
measured to a maximum range of 10 feet.
Note:
1 inch = 25.4 mm
1 foot = 12 inches
17
Interfacing multiple devices on an I2C bus
• Exercise 4: Connect both the temperature sensor and the range finder to
the same I2C bus and verify that the correct data can be read by
addressing commands appropriately. Update your screen readout to
continuously display updates of temperature and detected range.
18
Extended Exercises
19
Summary
• Introducing I2C
• Evaluating simple I2C communications
• I2C on the mbed
• Working with the TMP102 I2C temperature sensor
• Working with the SRF08 ultrasonic rangefinder
• Interfacing multiple devices on a single I2C bus
• Extended exercises
20