Lecture 13
Lecture 13
TM4C123GH6PM Launchpad
What is I2C
Inter-integrated Circuit
Bidirectional Data Transfer
Half Duplex (have only one data-line)
Synchronous bus so data is clocked with clock signal
Clock is controlled when data line is changed
Speed of I2C
Low (under 100 kbps)
Fast (400 Kbps)
High speed (3.4 mbps) (I2C V2.0)
2 Wire communication:
SDA and SCL
The Serial Clock Line (SCL) and the Serial Data line (SDA)
are both bi-directional.
Each line is open-drain, meaning a device may drive it low or
let it float.
A logic high occurs if all devices let the output float, and a
logic low occurs when at least one device drives it low.
The value of the pull-up resistor depends on the speed of the
bus.
4k7 is recommended for baud rates below 100 kbps
2k2 is recommended for standard mode
and 1k is recommended for fast mode.
In I2C protocol, more than 100 devices can share an I2C bus.
Each node can operate as either master or slave.
Master is a device that generate the Clock for the system, it
also initiate and terminate a transmission.
Slave is node that receives the clock and is addressed by the
master.
In I2C, both master and slave can receive or transmit data.
Master Transmitter
Master Receiver
Slave Transmitter
Slave Receiver
All address bytes transmitted on the I2C bus are nine bits long.
It consists of seven address bits, one READ/WRITE control
bit and an acknowledge bit.
Slave address bits are used to address a specific slave device
on the bus
7 bit address let the master to address maximum of 128 slaves
on the bus.
Although address 0000 000 is reserved for general call and all
address of the format 1111 xxx are reserved in many devices.
That means 119 devices can share an I2C bus.
The I2C Module inside the ARM chip can be Master or Slave.
We use I2CMCR (I2C Master Configuration register) to
designate the ARM chip as master or slave.
Setting bit D4 to 1 makes the I2C of ARM chip as Master.
After placing a byte of data in I2C Data register and the slave
address in I2C Master Slave address register
We may write a value of 0x07 to I2CMCS register for the I2C
to start a single byte data transmission from Master
(microcontroller) to slave device.
Notice, writing 0x07 to this register has all the three of STOP
= 1, RUN = 1, and START = 1 in it.
generate a START condition,
send the slave address with R/W bit,
check the acknowledge bit,
send the data byte,
check the acknowledge bit and
generate the STOP condition to terminate the transmission.
For a single byte write, after the START, the bus will go busy
until the data byte is written and the STOP is sent.
To monitor the progress of the transmission, we need to check
the BUSBSY bit (bit 6) of the I2CMCS register. When this
bit goes low, the transmission is complete.
When the transmission is complete, the program should check
the ERROR bit (bit 1) to make sure there was no error in the
transmission.
Transmission error may be caused by either slave address was
not acknowledged or the data write was not acknowledged.
In either case, the ADRACK (bit 2) or the DATACK (bit 3)
will be set.
#include<Wire.h>
void setup()
{
Wire.begin(0x20);
Wire.onReceive(receiveEvent);
Serial.begin(9600);
}
void loop()
{
delay(100);
}