Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
SlideShare a Scribd company logo

1

I2C Protocol
Lecture Notes: 17th April 2018
Sudhanshu Janwadkar, SVNIT Surat

2

Introduction
I²C (Inter-Integrated Circuit) is a
• Synchronous
• multi-master
• multi-slave
• packet switched
• single-ended
serial computer bus invented in 1982 by Philips Semiconductor
It is widely used for attaching lower-speed peripheral ICs to
processors and microcontrollers in short-distance, intra-board
communication.

3

A particular strength of I²C is the capability of a
microcontroller to control a network of device chips with just
two general-purpose I/O pins and software.
Many other bus technologies used in similar applications, such
as Serial Peripheral Interface Bus, require more pins and
signals to connect multiple devices.
Introduction

4

Design (H/W)
I²C uses only two bidirectional open-drain lines, Serial Data
Line (SDA) and Serial Clock Line (SCL), pulled up with
resistors.
Typical voltages used are +5 V or +3.3 V, although systems
with other voltages are permitted.

5

Design (H/W)

6

The I2C bus drivers are “open drain”, meaning that they can
pull the corresponding signal line low, but cannot drive it high.
Thus, there can be no bus contention where one device is
trying to drive the line high while another tries to pull it low,
eliminating the potential for damage to the drivers or excessive
power dissipation in the system.
Each signal line has a pull-up resistor on it, to restore the
signal to high when no device is asserting it low.
Design (H/W)

7

The I²C reference design has a 7-bit address space.
Common I²C bus speeds are the 100 kbit/s standard mode and
the 400 kbit/s Fast mode.
The maximal number of nodes is limited by the address space
and also by the total bus capacitance of 400 pF, which restricts
practical communication distances to a few meters.
The relatively high impedance and low noise immunity
requires a common ground potential, which again restricts
practical use to communication within the same PC board or
small system of boards.
Design (H/W)

8

Design (Architecture)
The bus has two roles for nodes: master and slave:
• Master node – node that generates the clock and initiates
communication with slaves.
• Slave node – node that receives the clock and responds
when addressed by the master.
The bus is a multi-master bus, which means that any number
of master nodes can be present.
Additionally, master and slave roles may be changed between
messages (after a STOP is sent).

9

There may be four potential modes of operation for a given
bus device, although most devices only use a single role and
its two modes:
• master transmit – master node is sending data to a slave,
• master receive – master node is receiving data from a slave
• slave transmit – slave node is sending data to the master
• slave receive – slave node is receiving data from the
master.
Design (Architecture)

10

I2C Protocol (Basics)
Frame Format

11

I2C Protocol (Basics)
Messages are broken up into two types of frame:
• Address frame: The master indicates the slave to which the
message is being sent
• Data frame: These are 8-bit data messages passed from
master to slave or vice versa.
Data is placed on the SDA line after SCL goes low, and is
sampled after the SCL line goes high.

12

I2C Protocol (Basics)
Start Condition
• To initiate the address frame, the master device leaves SCL
high and pulls SDA low.
• This puts all slave devices on notice that a transmission is
about to start.
• If two master devices wish to take ownership of the bus at one
time, whichever device pulls SDA low first wins the race and
gains control of the bus.
• It is possible to issue repeated starts, initiating a new
communication sequence without relinquishing control of the
bus to other masters

13

I2C Protocol (Basics)
Address Frame
• The address frame is always first in any new communication
sequence.
• For a 7-bit address, the address is clocked out most significant
bit (MSB) first, followed by a R/W bit indicating whether this
is a read (1) or write (0) operation.

14

I2C Protocol (Basics)
NACK/ACK bit
• It is the 9th bit of the frame
• This is the case for all frames (data or address).
• Once the first 8 bits of the frame are sent, the receiving device
is given control over SDA.
• If the receiving device does not pull the SDA line low before
the 9th clock pulse, it can be inferred that the receiving device
either did not receive the data or did not know how to parse the
message.
• In that case, the exchange halts, and it’s up to the master of the
system to decide how to proceed.

15

I2C Protocol (Basics)
Data Frames
• After the address frame has been sent, data can begin being
transmitted.
• The master will simply continue generating clock pulses at a
regular interval, and the data will be placed on SDA by either
the master or the slave, depending on whether the R/W bit
indicated a read or write operation.

16

I2C Protocol (Basics)
Stop condition
• Once all the data frames have been sent, the master will
generate a stop condition.
• Stop conditions are defined by a 0->1 (low to high) transition
on SDA after a 0->1 transition on SCL, with SCL remaining
high.
• During normal data writing operation, the value on SDA
should not change when SCL is high, to avoid false stop
conditions.

17

I2C Protocol
1. The master is initially in master transmit mode by sending a
START followed by the 7-bit address of the slave it wishes to
communicate with
2. This is finally followed by a single bit representing whether it
wishes to write (0) to or read (1) from the slave.
3. If the slave exists on the bus then it will respond with an ACK
bit (active low for acknowledged) for that address.
4. The master then continues in either transmit or receive mode
(according to the read/write bit it sent), and the slave
continues in the complementary mode (receive or transmit,
respectively).

18

I2C Protocol
5. The address and the data bytes are sent most significant bit
first.
6. The master terminates a message with a STOP condition if
this is the end of the transaction or it may send another
START condition to retain control of the bus for another
message (a "combined format" transaction).

19

I2C Protocol
If the master wishes to write to the slave, then it repeatedly
sends a byte with the slave sending an ACK bit. (In this
situation, the master is in master transmit mode, and the slave
is in slave receive mode.)
If the master wishes to read from the slave, then it repeatedly
receives a byte from the slave, the master sending an ACK bit
after every byte except the last one. (In this situation, the
master is in master receive mode, and the slave is in slave
transmit mode.)
An I²C transaction may consist of multiple messages.

20

Types of Transactions in I2C
• I²C defines basic three types of transactions, each of which
begins with a START and ends with a STOP:
• Single message where a master writes data to a slave.
• Single message where a master reads data from a slave.
• Combined format, where a master issues at least two
reads or writes to one or more slaves.
• In a combined transaction, each read or write begins with a
START and the slave address. The START conditions after the
first are also called repeated START bits. Repeated STARTs
are not preceded by STOP conditions, which is how slaves
know that the next message is part of the same transaction.

21

Timing Diagram
1. Data transfer is initiated with a start bit (S) signaled by SDA being pulled
low while SCL stays high.
2. SCL is pulled low, and SDA sets the first data bit level while keeping SCL
low
3. The data are sampled (received) when SCL rises for the first bit (B1). For a
bit to be valid, SDA must not change between a rising edge of SCL and the
subsequent falling edge
4. This process repeats, SDA transitioning while SCL is low, and the data
being read while SCL is high (B2, ...Bn).
5. The final bit is followed by a clock pulse, during which SDA is pulled low
in preparation for the stop bit.
6. A stop bit (P) is signaled when SCL rises, followed by SDA rising.

22

Interfacing of 8051 with EEPROM
(I2C Protocol)

23

#include<reg51.h>
#define ACK 1
#define NO_ACK 0
unsigned char i;
unsigned char EData[5];
unsigned char Data;
void DelayMs(unsigned int);
void WriteI2C(unsigned char);
void Start(void);
void Stop(void);
void ReadBYTE(unsigned int);
void WriteBYTE(unsigned int);
unsigned char ReadI2C(bit);
sbit SCL = P2^0; // connect to SCL pin (Clock)
sbit SDA = P2^1; // connect to SDA pin (Data)

24

void main(void) //Main Program
{
WriteBYTE(0x0000);
WriteI2C(‘A’); //Write Data’s Here
WriteI2C(‘B’);
WriteI2C(‘C’);
Stop();
DelayMs(10);
ReadBYTE(0x0000);
EData[0] = ReadI2C(NO_ACK);
EData[1] = ReadI2C(NO_ACK);
EData[2] = ReadI2C(NO_ACK);
while(1);
}

25

void Start(void) ///;Start I2C Protocol
{
SDA = 1;
SCL = 1;
_nop_();_nop_();
SDA = 0;
_nop_();_nop_();
SCL = 0;
_nop_();_nop_();
}
void Stop(void) // Stop I2C protocol
{
SDA = 0;
_nop_();_nop_();
SCL = 1;
_nop_();_nop_();
SDA = 1;
}

26

void WriteI2C(unsigned char Data) //;WriteI2C
{
for (i=0;i<8;i++)
{
SDA = (Data & 0x80) ? 1:0;
SCL=1;SCL=0;
Data<<=1;
}
SCL = 1;
_nop_();_nop_();
SCL = 0;
}

27

unsigned char ReadI2C(bit ACK_Bit)
{
Start();
WriteI2C(0xA1);
SDA = 1;
for (i=0;i<8;i++)
{
SCL = 1;
Data<<= 1;
Data = (Data | SDA);
SCL = 0;
_nop_();
}
if (ACK_Bit == 1)
SDA = 0; // Send ACK
else
SDA = 1; // Send NO ACK
_nop_(); _nop_();
SCL = 1;
_nop_();_nop_();
SCL = 0;
Stop();
return Data;

28

void ReadBYTE(unsigned int Addr) // Read 1 byte from I2C
{
Start();
WriteI2C(0xA0);
WriteI2C((unsigned char)(Addr>>8)&0xFF);
WriteI2C((unsigned char)Addr&0xFF);
}
void WriteBYTE(unsigned int Addr) //Write 1 byte to I2C
{
Start();
WriteI2C(0xA0);
WriteI2C((unsigned char)(Addr>>8)&0xFF); // send address high
WriteI2C((unsigned char)Addr&0xFF); // send address low
}

29

void DelayMs(unsigned int count)
{
unsigned int i;
while(count)
{
i = 115;
while(i>0) i- -;
Count- -;
}
}

30

Common applications of the I²C bus are:
• Describing connectable devices via small ROM configuration
tables to enable "plug and play" operation, such as
• Accessing real-time clocks and NVRAM chips that keep user
settings.
• Accessing low-speed DACs and ADCs.
• Controlling LCD displays.
Applications of I2C

More Related Content

I2C Protocol

  • 1. I2C Protocol Lecture Notes: 17th April 2018 Sudhanshu Janwadkar, SVNIT Surat
  • 2. Introduction I²C (Inter-Integrated Circuit) is a • Synchronous • multi-master • multi-slave • packet switched • single-ended serial computer bus invented in 1982 by Philips Semiconductor It is widely used for attaching lower-speed peripheral ICs to processors and microcontrollers in short-distance, intra-board communication.
  • 3. A particular strength of I²C is the capability of a microcontroller to control a network of device chips with just two general-purpose I/O pins and software. Many other bus technologies used in similar applications, such as Serial Peripheral Interface Bus, require more pins and signals to connect multiple devices. Introduction
  • 4. Design (H/W) I²C uses only two bidirectional open-drain lines, Serial Data Line (SDA) and Serial Clock Line (SCL), pulled up with resistors. Typical voltages used are +5 V or +3.3 V, although systems with other voltages are permitted.
  • 6. The I2C bus drivers are “open drain”, meaning that they can pull the corresponding signal line low, but cannot drive it high. Thus, there can be no bus contention where one device is trying to drive the line high while another tries to pull it low, eliminating the potential for damage to the drivers or excessive power dissipation in the system. Each signal line has a pull-up resistor on it, to restore the signal to high when no device is asserting it low. Design (H/W)
  • 7. The I²C reference design has a 7-bit address space. Common I²C bus speeds are the 100 kbit/s standard mode and the 400 kbit/s Fast mode. The maximal number of nodes is limited by the address space and also by the total bus capacitance of 400 pF, which restricts practical communication distances to a few meters. The relatively high impedance and low noise immunity requires a common ground potential, which again restricts practical use to communication within the same PC board or small system of boards. Design (H/W)
  • 8. Design (Architecture) The bus has two roles for nodes: master and slave: • Master node – node that generates the clock and initiates communication with slaves. • Slave node – node that receives the clock and responds when addressed by the master. The bus is a multi-master bus, which means that any number of master nodes can be present. Additionally, master and slave roles may be changed between messages (after a STOP is sent).
  • 9. There may be four potential modes of operation for a given bus device, although most devices only use a single role and its two modes: • master transmit – master node is sending data to a slave, • master receive – master node is receiving data from a slave • slave transmit – slave node is sending data to the master • slave receive – slave node is receiving data from the master. Design (Architecture)
  • 11. I2C Protocol (Basics) Messages are broken up into two types of frame: • Address frame: The master indicates the slave to which the message is being sent • Data frame: These are 8-bit data messages passed from master to slave or vice versa. Data is placed on the SDA line after SCL goes low, and is sampled after the SCL line goes high.
  • 12. I2C Protocol (Basics) Start Condition • To initiate the address frame, the master device leaves SCL high and pulls SDA low. • This puts all slave devices on notice that a transmission is about to start. • If two master devices wish to take ownership of the bus at one time, whichever device pulls SDA low first wins the race and gains control of the bus. • It is possible to issue repeated starts, initiating a new communication sequence without relinquishing control of the bus to other masters
  • 13. I2C Protocol (Basics) Address Frame • The address frame is always first in any new communication sequence. • For a 7-bit address, the address is clocked out most significant bit (MSB) first, followed by a R/W bit indicating whether this is a read (1) or write (0) operation.
  • 14. I2C Protocol (Basics) NACK/ACK bit • It is the 9th bit of the frame • This is the case for all frames (data or address). • Once the first 8 bits of the frame are sent, the receiving device is given control over SDA. • If the receiving device does not pull the SDA line low before the 9th clock pulse, it can be inferred that the receiving device either did not receive the data or did not know how to parse the message. • In that case, the exchange halts, and it’s up to the master of the system to decide how to proceed.
  • 15. I2C Protocol (Basics) Data Frames • After the address frame has been sent, data can begin being transmitted. • The master will simply continue generating clock pulses at a regular interval, and the data will be placed on SDA by either the master or the slave, depending on whether the R/W bit indicated a read or write operation.
  • 16. I2C Protocol (Basics) Stop condition • Once all the data frames have been sent, the master will generate a stop condition. • Stop conditions are defined by a 0->1 (low to high) transition on SDA after a 0->1 transition on SCL, with SCL remaining high. • During normal data writing operation, the value on SDA should not change when SCL is high, to avoid false stop conditions.
  • 17. I2C Protocol 1. The master is initially in master transmit mode by sending a START followed by the 7-bit address of the slave it wishes to communicate with 2. This is finally followed by a single bit representing whether it wishes to write (0) to or read (1) from the slave. 3. If the slave exists on the bus then it will respond with an ACK bit (active low for acknowledged) for that address. 4. The master then continues in either transmit or receive mode (according to the read/write bit it sent), and the slave continues in the complementary mode (receive or transmit, respectively).
  • 18. I2C Protocol 5. The address and the data bytes are sent most significant bit first. 6. The master terminates a message with a STOP condition if this is the end of the transaction or it may send another START condition to retain control of the bus for another message (a "combined format" transaction).
  • 19. I2C Protocol If the master wishes to write to the slave, then it repeatedly sends a byte with the slave sending an ACK bit. (In this situation, the master is in master transmit mode, and the slave is in slave receive mode.) If the master wishes to read from the slave, then it repeatedly receives a byte from the slave, the master sending an ACK bit after every byte except the last one. (In this situation, the master is in master receive mode, and the slave is in slave transmit mode.) An I²C transaction may consist of multiple messages.
  • 20. Types of Transactions in I2C • I²C defines basic three types of transactions, each of which begins with a START and ends with a STOP: • Single message where a master writes data to a slave. • Single message where a master reads data from a slave. • Combined format, where a master issues at least two reads or writes to one or more slaves. • In a combined transaction, each read or write begins with a START and the slave address. The START conditions after the first are also called repeated START bits. Repeated STARTs are not preceded by STOP conditions, which is how slaves know that the next message is part of the same transaction.
  • 21. Timing Diagram 1. Data transfer is initiated with a start bit (S) signaled by SDA being pulled low while SCL stays high. 2. SCL is pulled low, and SDA sets the first data bit level while keeping SCL low 3. The data are sampled (received) when SCL rises for the first bit (B1). For a bit to be valid, SDA must not change between a rising edge of SCL and the subsequent falling edge 4. This process repeats, SDA transitioning while SCL is low, and the data being read while SCL is high (B2, ...Bn). 5. The final bit is followed by a clock pulse, during which SDA is pulled low in preparation for the stop bit. 6. A stop bit (P) is signaled when SCL rises, followed by SDA rising.
  • 22. Interfacing of 8051 with EEPROM (I2C Protocol)
  • 23. #include<reg51.h> #define ACK 1 #define NO_ACK 0 unsigned char i; unsigned char EData[5]; unsigned char Data; void DelayMs(unsigned int); void WriteI2C(unsigned char); void Start(void); void Stop(void); void ReadBYTE(unsigned int); void WriteBYTE(unsigned int); unsigned char ReadI2C(bit); sbit SCL = P2^0; // connect to SCL pin (Clock) sbit SDA = P2^1; // connect to SDA pin (Data)
  • 24. void main(void) //Main Program { WriteBYTE(0x0000); WriteI2C(‘A’); //Write Data’s Here WriteI2C(‘B’); WriteI2C(‘C’); Stop(); DelayMs(10); ReadBYTE(0x0000); EData[0] = ReadI2C(NO_ACK); EData[1] = ReadI2C(NO_ACK); EData[2] = ReadI2C(NO_ACK); while(1); }
  • 25. void Start(void) ///;Start I2C Protocol { SDA = 1; SCL = 1; _nop_();_nop_(); SDA = 0; _nop_();_nop_(); SCL = 0; _nop_();_nop_(); } void Stop(void) // Stop I2C protocol { SDA = 0; _nop_();_nop_(); SCL = 1; _nop_();_nop_(); SDA = 1; }
  • 26. void WriteI2C(unsigned char Data) //;WriteI2C { for (i=0;i<8;i++) { SDA = (Data & 0x80) ? 1:0; SCL=1;SCL=0; Data<<=1; } SCL = 1; _nop_();_nop_(); SCL = 0; }
  • 27. unsigned char ReadI2C(bit ACK_Bit) { Start(); WriteI2C(0xA1); SDA = 1; for (i=0;i<8;i++) { SCL = 1; Data<<= 1; Data = (Data | SDA); SCL = 0; _nop_(); } if (ACK_Bit == 1) SDA = 0; // Send ACK else SDA = 1; // Send NO ACK _nop_(); _nop_(); SCL = 1; _nop_();_nop_(); SCL = 0; Stop(); return Data;
  • 28. void ReadBYTE(unsigned int Addr) // Read 1 byte from I2C { Start(); WriteI2C(0xA0); WriteI2C((unsigned char)(Addr>>8)&0xFF); WriteI2C((unsigned char)Addr&0xFF); } void WriteBYTE(unsigned int Addr) //Write 1 byte to I2C { Start(); WriteI2C(0xA0); WriteI2C((unsigned char)(Addr>>8)&0xFF); // send address high WriteI2C((unsigned char)Addr&0xFF); // send address low }
  • 29. void DelayMs(unsigned int count) { unsigned int i; while(count) { i = 115; while(i>0) i- -; Count- -; } }
  • 30. Common applications of the I²C bus are: • Describing connectable devices via small ROM configuration tables to enable "plug and play" operation, such as • Accessing real-time clocks and NVRAM chips that keep user settings. • Accessing low-speed DACs and ADCs. • Controlling LCD displays. Applications of I2C