GETTING STARTED WITHPICs
THE LATEST IN PROGRAMMING MICROCONTROLLERS
■ BY CHUCK HELLEBUYCK
I2C Communication
It's a new year and, unfortunately, economic times are looking quite dismal.
Our industry is also going through a recession, with
people losing their jobs as companies downsize.
Acquiring skills outside your normal job requirements
provides a great advantage to you. If you have experience
designing electronics, you should have different options
even in the worst economic conditions, because electronics
control most of the things we depend on every day.
Luckily, you don't have to spend a fortune on a college
education to gain electronic design experience. Just reading
through and experimenting with the projects in Nuts &
Volts may help set you on a new path toward success.
This time around, I want to do my part in educating
those getting started with Microchip PIC microcontrollers
(MCUs) by demonstrating how to use an I C (pronounced
2
"I-squared-C") Electrically Erasable Programmable Read
Only Memory (EEPROM) chip. This may be helpful for
other purposes, because Microchip offers many I C-based
2
products.
from the master. To understand what the slave requires,
review the component specifications which detail any
specific communications. The main thing to understand is
that I C is a master/slave relationship, and typically, an
2
MCU will handle the master role.
WHAT IS I2C?
I C is a two-wire communication standard that only
2
uses two input/output (I/O) pins on an MCU to control
multiple devices. Each device on the bus connects to
those two lines via open-drain pins, meaning they can pull
the bus line low but require an external voltage source to
pull it high. The key thing to understand about I C is that
2
each device on the bus has a unique control byte because
all devices receive messages from the master device.
However, they cannot respond or react to the message
unless the control byte sent by the master matches their
own. Figure 1 shows an I C bus setup. The I C bus has a
22
clock line and a data line, and sends out a series of byte
values serially by putting a data bit on the data line and
then loading it into the slaves via the clock line. Each slave
device will follow a standard type of communication, but
also requires a unique set of byte-sized information sent
■ FIGURE 1. I2CBus Setup.
68 January 2009
MASTER AND SLAVE
The I C protocol defines what a master is and what a
2
slave is. The master has control of the bus. It generates the
clock signal and sends the start and stop signals that frame
the complete I C message. The slave must listen to every
2
message sent, and respond if its control byte was received.
The slave then responds to the master with data. There are
systems with multiple masters but, for this article, I will
only focus on a single master setup.
As mentioned, the I C bus has two lines: a data line
2
and a clock line. If you are familiar with how a shift
register works, then this should be easy to understand. The
master puts a bit on the data line by actively pulling the
line low or passively letting the external resistor pull the
line high. Then, the master pulses the clock line by pulling
it low and then releasing it high again. The circuitry at the
slave receives that bit of data the same way a shift register
clocks in a bit. This pulsing of data and clock lines continues
until the first part of the I C message is transmitted.
2
Figure 2 shows the format of the data line I C message.
2
The signal in Figure 2 shows a write and read
sequence for communicating with an external EEPROM
device. Microchip offers many different types of memory
chips. I will use the 24LC01B to demonstrate how to store
and read back data in an external EEPROM, using I C as
2
the communication protocol. The control byte is a seven
bit wide value, with the eighth bit being either zero for a
write or one for a read operation. As you can see, the
read sequence requires two control bytes, while a write
only requires one. This is because a read operation
requires the
master to first
write the memory-address byte to
the EEPROM so
it can locate the
data and then
read the data, from
the EEPROM.
■ FIGURE 2. I2CData Line Message Format.