Professional Documents
Culture Documents
Inter Integrated Circuit Bus Protocol (I C) : By: Dr. Mitesh Limachia
Inter Integrated Circuit Bus Protocol (I C) : By: Dr. Mitesh Limachia
(I2C)
• To reduce the pins and wiring => cost => Serial bus
protocol => I2C (2-wire) & SPI (4-wire)
Term Description
LCD
MCU
DATA Transfer on the I2C Bus
SDA
SCL
Data line stable; Change
Data valid of data
allowed
Start and Stop Conditions
A transition on the data line while the clock line is high is
defined as either a start or a stop condition.
SDA SDA
SCL SCL
Start Stop
Condition Condition
Addressing Scheme
R / Wr
By Slave
R/W’
0 – Slave written by Master
1 – Slave read by Master
SLAVE
Acknowledge
• The acknowledge takes place after every byte. The
acknowledge bit allows the receiver to signal the
transmitter that the byte was successfully received and
another byte may be sent.
• The Acknowledge signal is defined as follows: the
transmitter releases the SDA line during the acknowledge
clock pulse so the receiver can pull the SDA line LOW and
it remains stable LOW during the HIGH period of this clock
pulse.
• When SDA remains HIGH during 9th clock pulse, this is
defined as the Not Acknowledge signal. The master can then
generate either a STOP condition to abort the transfer, or a
repeated START condition to start a new transfer.
Data Transfer on the I2C Bus
• Start Condition
• Slave address + R/W
– Slave acknowledges with ACK (9th SCK)
• All data bytes
– Each followed by ACK
• Stop Condition
SDA
SCL
Remember : Clock is produced by Master
Start Stop
Multi-Master Capability
22 of 40
Contd.
• The master tries to send a HIGH, but detects that the SDA
level is LOW, the master knows that it has lost the arbitration
and will turn off its SDA output driver. The other master goes
on to complete its transaction.
• No information is lost during the arbitration process.
A master that loses the arbitration can generate clock
pulses until the end of the byte in which it loses the
arbitration and must restart its transaction when the bus is
idle.
• Two masters can actually complete an entire transaction
without error, as long as their data transmissions are
identical.
Example-2
• Two or more masters may generate a START condition at the same time
• Arbitration is done on SDA while SCL is HIGH - Slaves are not involved
DATA1 “0”
0
“1”
DATA2
0
“0”
SDA
0
SCL
0
START “1” “0” “0” “1” “1” “0”
24
Clock Synchronization
• The I2C bus protocol has the clock
synchronization mechanism between the
slower and faster devices. There will be a
handshake mechanism between slower and
faster devices.
Clock Synchronization
• CLK Synchronization => Wired-AND function on SCL line.
• SCL line => held Low by Master with longest LOW period,
Master with shorter LOW periods enter HIGH wait-states
during time.
Contd.
• In this way, a synchronized SCL clock is generated.
• LOW period of SCL is determined by the master with the
longest clock LOW period, and its HIGH period
determined by the one with the shortest clock HIGH
period.
• Assume the followings in sequence in context of I2C bus
protocol:
(i) Master1 & Master2 having a clock of 100 KHz and
50 KHz respectively.
(ii) Master1 & Master2 generates the START conditions
simultaneously on the bus.
(iii) Master1 wants to send first data byte (address + R/W)
0x65H on the I2C SDA line while Master2 wants to send first
data byte 0x95H.
• Answer followings:
(a) Which Master will send its first data byte successfully?
Discuss the same using arbitration procedure.
(b) What is the frequency of SCLK set by I2C interface?
Discuss the same using clock synchronization procedure.
I2C Bus
Programming
using
STM32F103C8 Kit
I2C Data Register (I2C_DR)
Transmitter Byte transmission starts automatically when a byte is written in the DR register.
mode: A continuous transmit stream can be maintained if the next data is put in DR ,
SMBALERT
Bit 15 SMBus alert
:
Bit 14 TIMEOUT: Timeout or Tlow error
Bit 12 PECERR: PEC Error in reception
Bit 11 OVR: Overrun/Underrun
Bit 10 AF: Acknowledge failure
Bit 9 ARLO: Arbitration lost (master mode)
Bit 8 BERR: Bus error
Bit 7 TxE: Data register empty (transmitters)
Bit 6 RxNE: Data register not empty (receivers)
Bit 4 STOPF: Stop detection (slave mode)
Bit 3 ADD10: 10-bit header sent (Master mode)
Bit 2 BTF: Byte transfer finished
Bit 1 ADDR: Address sent (master mode)/matched (slave mode)
Bit 0 SB: Start bit (Master mode)
Contd.
• Bit 0 SB: Start bit (Master mode)
0: No Start condition 1: Start condition generated.
– Set by hardware when a Start condition generated.
– Cleared by software by reading the SR1 register followed by
writing the DR register.
…...
0b110010: 50 MHz
I2C Clock Control Register I2C_CCR
Address=0x38,
DAC
Program Code
#include<stm32f10x.h>
char res;
void I2C_init()
{
RCC->APB1ENR |= (1<<21); I2C1 clock enable.
I2C1->CR2 = 0x0008; To set the clock frequency of
I2C1->CCR = 0x0028; SCL = 100 KHz
Data = 0x41(A)