User manual

LPCXpresso Experiment Kit - User’s Guide
Page 94
Copyright 2013 © Embedded Artists AB
7.14 Work with a Serial Bus I2C
Note that the breadboard cannot be used in these experiments. The chips used are surface
mounted and these must be soldered to the pcb before starting.
In this experiment you will learn how to work with the Inter-Integrated Circuit Bus, or I
2
C bus for short. It
is a multi-master bus for (relatively) low-speed peripherals. The basic clock frequency is 100 kHz but
there are newer specifications that support higher speeds, for example 400 kHz that is often
supported, called Fast-mode (Fm). Higher frequencies of 1 MHz (Fm+), 3.4 MHz (High-speed mode,
Hs) and 5 MHz (Ultra Fast-mode, UFm) also exist but are less widespread.
The I
2
C bus is a synchronous bus meaning that there is an explicit clock signal. It builds on the master-
slave concept where one unit is a master and controls the communication. One slave is addressed on
the bus and is the other end of the master-slave communication. There can be many masters on the
bus, but only one active at a time.
The I
2
C bus uses two bidirectional open-drain lines pulled up by resistors:
SCK: serial clock, the master always generates the clock
SDA: serial data, the master generates the data when transmitting to the slave. The slave
generates the data when transmitting to the master
The picture below illustrates how many masters and slaves can share one I
2
C bus.
Figure 55 I2C Bus
For more information about I
2
C, see http://en.wikipedia.org/wiki/I%C2%B2C
There is a lot of details about the I
2
C bus that have not been covered in this short overview, like how
addressing works, how bus arbitration works, how read and write operations work, how acknowledge
of data works, etc.
Have a look in chapter 15 - LPC111x/LPC11Cxx I2C-bus controller in the LPC111x user’s manual for a
description of the how the I
2
C block works. It is more complicated interface than for the timers and SSP
peripherals. The basic principle is to send commands to the I
2
C peripheral block. These commands are
carried out in the (external) I
2
C bus and a status is presented as result. Based on the status the I
2
C
driver gives the next command.
It is not recommended to start from scratch and create an I
2
C driver. Instead the driver supplied from
NXP will be used, see files i2c.c/i2c.h. Let’s investigate the application program interface (API)
for this driver. The file i2c.h contains (amongst other declarations) the following function
declarations:
I2CInit() this function must be called before the I2C driver is used and any I2C
communication can take place. The function initializes the pins (PIO0_4, PIO0_5) to be I2C
pins and other necessary initialization. The function has two parameters. The first parameter
tells if the I2C interface shall be a master or slave interface. In this case it is a master
interface and no further parameter is needed. In case it is a slave interface, the second
parameter is the slave address of this interface.