I2C Protocol
I2C Protocol
Objectives
1. Understand the Inter-Integrated Circuit (I2C) protocol
2. Explain how the I2C read and write operations work
3. Examine the I2C pins SCK and SCL
4. Explain the function of I2C (TWI) registers in AVR
5. Code programs in Assembly and C for I2C (TWI)
6. Explain how the real-time clock (RTC) chip works
7. Explain the function of the DS1307 RTC pins
8. Explain the function of the DS1307 RTC registers
9. Understand the interfacing of the DS1307 RTC to the AVR
10. Code programs to display time and date in Assembly and C
The IIC (Inter-Integrated Circuit) is a bus interface connection incorporated into many devices
such as sensors, RTC, and EEPROM. The IIC is also referred to as I2C (I2C) or I square C in many
technical literatures. In this section we examine the pins of the I2C bus and focus on I2C
terminology and protocols.
I2C bus
The I2C bus was originally started by Philips, but in recent years has become a widely used
standard adapted by many semiconductor chip companies. I2C is ideal for attaching low-speed
peripherals to a motherboard or embedded system or anywhere that a reliable communication
over a short distance is required. As we will see in this chapter, I2C provides a connection-
oriented communication with acknowledge. I2C devices use only 2 pins for data transfer, instead
of the 8 or more pins used in traditional buses. They are called SCL (Serial Clock), which
synchronize the data transfer between two chips, and SDA (Serial Data). This reduction of
communication pins reduces the package size and power consumption drastically, making them
ideal for many applications in which space is a major concern. These two pins, SDA and SCK,
make the I2C a 2-wire interface. In many application notes, including AVR datasheets, I2C is
referred to as Two-Wire Serial Interface (TWI). In this chapter we use I2C and TWI interchangeably.
Example 1
Give an example to show how a device (node) can have more than one mode of operation.
Solution:
If you connect the AVR to an EEPROM with I2C, the AVR does a master transmit operation to write
to EEPROM. The AVR also does master receive operations to read from EEPROM. In the following
sections, you will see that a node can do the operations of master and slave at different times.
Bit format
I2C is a synchronous serial protocol; each data bit transferred on the SDA line is synchronized by
a high-to-low pulse of clock on the SCL line. According to I2C protocols the data line cannot
change when the clock line is high; it can change only when the clock line is low. See Figure 2.
The STOP and START conditions are the only exceptions to this rule.
The bus is considered busy between each pair of START and STOP conditions, and no other
master tries to take control of the bus when it is busy. If a master, which has the control of the
bus, wishes to initiate a new transfer and does not want to release the bus before starting the new
transfer, it issues a new START condition between a pair of START and STOP conditions. It is called
the REPEATED START condition. See Figure 4.
Example 2
Give an example to show when a master must use the REPEATED START condition. What will
happen if the master does not use it?
Solution:
If you connect two AVRs (AVR A and AVR B) and an EEPROM with I2C, and AVR A wants to display
the addition of the contents of addresses 0x34 and 0x35 of EEPROM, it has to use the REPEATED
START condition. Let’s see what may happen if AVR A does not use the REPEATED START
condition. AVR A transmits a START condition, reads the content of address 0x34 of EEPROM into
R16, and transmits a STOP condition to release the bus. Before AVR A reads the contents of
address 0x35 into R17, AVR B seizes the bus and changes the contents of addresses 0x34 and
0x35 of EEPROM. Then AVR A reads the content of address 0x35 into R17, adds it to R16, and
displays the result on the LCD. The result on the LCD is neither the sum of the old values of
addresses 0x34 and 0x35 nor the sum of the new values of addresses 0x34 and 0x35 of EEPROM!
Packet format in I2C
In I2C, each address or data to be transmitted must be framed in a packet. Each packet is 9 bits
long. The first 8 bits are put on the SDA line by the transmitter, and the 9th bit is an acknowledge
by the receiver or it may be NACK (not acknowledge). The clock is generated by the master,
regardless of whether it is the transmitter or receiver. To get an acknowledge, the transmitter
releases the SDA line during the ninth clock so that the receiver can pull the SDA line low to
indicate an ACK. If the receiver doesn’t pull the SDA line low, it is considered as NACK. See Figure
5.
In I2C, each packet may contain either address or data. Also notice that START condition +
address packet + one or more data packet + STOP condition together form a complete data
transfer. Next we will study address and data packet formats and how to combine them to make
a complete transmission.
Address bits are used to address a specific slave device on the bus. The 7-bit address lets the
master address a maximum of 128 slaves on the bus, although the address 0000 000 is reserved
for general call and all addresses of the format 1111 xxx are reserved. That means 119 (128 – 1 –
8) devices can share an I2C bus. In the I2C bus the MSB of the address is transmitted first.
The eighth bit in the packet is the READ/WRITE control bit. If this bit is set, the master will read
the next frame (Data) from the slave, otherwise, the master will write the next frame (Data) on the
bus to the slave. When a slave detects its address on the bus, it knows that it is being addressed
and it should acknowledge in the ninth SCL (ACK) cycle by changing SDA to zero. If the addressed
slave is not ready or for any reason does not want to service the master, it should leave the SDA
line high in the ninth clock cycle. This is considered to be NACK. In case of NACK, the master can
transmit a STOP condition to terminate the transmission, or a REPEATED START condition to
initiate a new transmission.
An address packet consisting of a slave address and a READ is called SLA+R, while an address
packet consisting of a slave address and a WRITE is called SLA+W.
As we mentioned before, address 0000 000 is reserved for general call. This means that when a
master transmits address 0000 000, all slaves respond by changing the SDA line to zero and wait
to receive the data byte. This is useful when a master wants to transmit the same data byte to all
slaves in the system. Notice that the general call address cannot be used to read data from slaves
because no more than one slave is able to write to the bus at a given time.
Clock stretching
One of the features of the I²C protocol is clock stretching. It is a kind of flow control. If an
addressed slave device is not ready to process more data it will stretch the clock by holding the
clock line (SCL) low after receiving (or sending) a bit of data. Thus the master will not be able to
raise the clock line (because devices are wire-ANDed) and will wait until the slave releases the
SCL line to show it is ready to transfer the next bit. See Figure 8.
Arbitration
I2C protocol supports a multimaster bus system. This doesn’t mean that more than one master
can use the bus at the same time. Rather, each master waits for the current transmission to finish
and then starts to use the bus. But it is possible that two or more masters initiate a transmission
at about the same time. In this case the arbitration happens.
Each transmitter has to check the level of the bus and compare it with the level it expects; if it
doesn't match, that transmitter has lost the arbitration, and will switch to slave mode. In the case
of arbitration, the winning master will continue its job. Notice that neither the bus is corrupted
nor the data is lost. See Example 5.
Example 5
Two masters, A and B, start at about the same time. What happens if master A wants to write to
slave 0010 000 and master B wants to write to slave 0001 111?
Solution:
Master A will lose the arbitration in the third clock because the SDA line is different from the
output of master A at the third clock. Master A switches to slave mode and leaves the bus after
losing the arbitration.
Figure 9 shows how to write 0x01, 0x02, and 0x03 to three consecutive locations starting from
location 00001111 of slave 1111000.
Figure 10 shows how to read three consecutive locations starting from location 00001111 of slave
number 1111000.
SECTION 2: TWI (I2C) IN THE AVR
In many applications, including AVR datasheet, I2C is referred to as Two-wire Serial Interface
(TWI). From now on, in this chapter we use TWI to conform with the AVR data sheets. In this
section we discuss the TWI module and registers of the AVR. Then we show how to program the
AVR to address a slave device and send or receive data using TWI. The TWI module in the AVR is
composed of four submodules: bit rate generation unit, bus interface unit, address match unit,
and control unit. Figure 11 shows the TWI module. All registers drawn with a thick line are
accessible through the AVR data bus.
The bit rate generation unit controls the frequency of the system clock (SCL) when operating in a
master mode. The bus interface unit detects and generates START, REPEATED START and STOP
conditions. It also detects arbitration, controls sending or receiving ACK, and also transfers
packets of data or address. The address match unit compares the received address byte with the
7-bit address in TWI address register and informs the control unit upon an address match. The
control unit controls the TWI module and generates responses according to settings in the TWI
control register. It also sets the contents of the status register according to current state.
In the AVR microcontroller, five major registers are associated with the TWI. They are TWBR (TWI
Bit rate Register), TWCR (TWI Control Register), TWSR (TWI Status Register), TWAR (TWI Address
Register), and TWDR (TWI Data Register). Next, we will focus on registers related to TWI and study
each bit of them in detail.
TWBR selects the division factor to control the SCL clock frequency in master mode. The SCL
frequency is controlled by settings in the TWBR and the prescaler bits in the TWSR (TWI status
register is discussed next). The following equation demonstrates the relation between SCL
frequency, TWBR, and TWPS bits in TWI status register:
Notice that the value of TWBR should be 10 or higher if the TWI operates in master mode. Example
6 shows how the frequency of SCL is calculated.
Example 6
Calculate the SCL frequency if the value of TWPS bits in TWSR is 01 (1 Dec) and the value of TWBR
is 00100110 (38 Dec). Assume that CPU clock frequency is 8 MHz.
Solution:
The SCL frequency will be: 8 MHz / ((16 + 2 (38) × 4) = 25 kHz
TWI Start bit and TWI Stop bit (TWSTA and TWSTO)
To generate START or STOP conditions, you have to set the TWSTA or TWSTO bit to one respectively
and then clear the TWINT flag to zero by writing a one to it.
REVIEW QUESTIONS
1. True or false. I2C protocol is ideal for short distances.
2. How many bits are there in a frame? Which bit is for acknowledge?
3. True or false. START and STOP conditions are generated when the SDA is high.
4. What is the name of the flow control method in the I2C protocol?
5. What is the recommended value for the pull-up resistors in the I2C protocol?
6. True or false. After the arbitration of two masters, both of them must start transmission
from the beginning.
7. True or false. The AVR has an internal TWI module.
8. What are the TWI registers in AVR?
9. How do we generate START or STOP conditions in the AVR?
10. True or false. After reading status register we should mask the 2 MSB bits.
11. Which bit is polled to know if the TWI is ready now?
12. True or false. We can write to TWDR when the TWI module is busy.
13. Which bit controls the generation of ACK?
ANSWERS TO REVIEW QUESTIONS
1. True
2. 9 bits. The ninth bit
3. True
4. Clock stretching
5. 4.7 kilohms
6. False
7. True
8. TWDR, TWAR, TWBR, TWCR, and TWSR
9. By writing 1 to the TWSTA and TWSTO bits, respectively
10. False
11. TWINT
12. False
13. TWEA