Wednesday 2 July 2014

I2C COMMUNICATION (Explanation With Diagrams)

I2C is primarily used for communication between two Integrated Chips. I2C protocol was designed by Philips. This post will help you to easily interface two IC’s using I2C protocol.
I2C is a two wire communication system where master controls the slave. The two wires are SDA and SCL. These two wires are pulled up using resistors, which forms wired AND connection between Master and Slaves.
IMPORTANT TERMS: 
SDA- Serial Data Line.
SCL- Serial Clock Line.
Master- Master is a device which take care of data transfer between IC’s by providing proper clock signal
Slave- Slave is a device where the data can be written and read.
Clock- Clock is a Periodic signal generated by the Master, the state of the clock plays a major role in STARTING, TERMINATING and SAMPLING the data.
Data- Data is also a signal where the data, either 1 or 0 is represented.
COMMUNICATION: 
What is I2C Connection?
i2c-diagram
In-order to start a communication between two IC’s they should be connected as per the I2C Protocol.
What Determines the number of Slaves in a line? 
ADDRESS LINES, based on the number of address lines number of devices can be calculated, using the formula 2 ^address lines. In this post I will explain about 7 bit addressing.
How to Initiate a Communication? 
Communication is initiated by first addressing the desired IC. The desired IC is addressed by using the device address, normally the device address will be mentioned by the manufacturer, for example Device Address of a RTC (DS1307) will be 1101000. Once identifying the device address the following procedure has to be followed.
STEPS: 
  1. Transmit the Start Bit
  2. Transmit the Device address(specifying the read or write mode).
  3. Receive the acknowledgement.
  4. Transmit the Register Address(optional).
  5. Receive the acknowledgement.
  6. Transmit the Data as per your wish.(for each byte receive an acknowledgement)
  7. Transmit the Stop Bit.
1) Transmitting the Start Bits and Stop Bits: 
Start Bit: A State know as Start Bit is generated when the data line is pulled from HIGH to LOW when the Clock is HIGH
Stop Bit: A State know as Stop Bit is generated when the data line is pulled from LOW to HIGH when the Clock is HIGH
i2c-tutorial-star-stop                                        .
note: From the above Diagram it can be inferred that it is not advisable to change the state of SDA when the clock is HIGH as it either starts or ends the connection. 
2)Transmitting the Device Address: 
Once the Start Bit is transmitted it must be followed by the device address along with the read/write bit. well, here we shall take RTC as an example. RTC mentioned above has 7bit address this seven bit address has to be transmitted serially bit by bit through SDA line.
IMPORTANT WHILE TRANSMITTING THE DATA:( The data can be device address, register address or the byte which has to be stored in the register)
  • The MSB of the byte must be sent first.
  • The state of SDA line must not be changed when the Clock signal is HIGH.
  • The State of SDA should be changed only if the state of the Clock is LOW
Once the 7 bit of address is transmitted then READ/WRITE bit has to be send. This bit determines whether the data is read from the slave or written from the slave.
  • READ - 
  • WRITE - 0 
ACKNOWLEDGEMENT: 
Once the device address is transmitted the slave has to acknowledge the user by sending the acknowledgement bit. Transmitting the device address will take 8 clock pulse , therefore in the 9th clock pulse acknowledgement must be received by the master. The Slave device will pull the SDA line to LOW to Acknowledge the data transmitted.
  • POSITIVE ACKNOWLEDGEMENT: Bit 0 is sent.
  • NEGATIVE ACKNOWLEDGEMENT: Bit 1 is sent.
3)Transmitting the Register Address: 
It is similar to the transmission of device address but it doesn`t include the READ/WRITE bit. Here the eight bit address has to be transmitted serially bit by bit through SDA line.
After transmitting it acknowledgement is received by the master.
4) Transmitting the Data as per your wish: 
The data is sent similar to the address and an acknowledgement is received after 8 bits. We can transmit N number of data and  finally conclude it with a stop bit . The register address need not be mentioned every time, the slave device can increment the address automatically. But if you need to write in a specific address or if the slave device does not increment the address you have to mention the register address.
Thus the  data is written to the slave device. then Stop Bit has to be sent as mentioned above.
READING THE DATA FROM THE IC 
  1. Transmit the Start Bit
  2. Transmit the Device address(specifying the write mode).
  3. Receive the acknowledgement.
  4. Transmit the Register Address.
  5. Receive the acknowledgement.
  6. Transmit the Start Bit again. (Repeated Start)
  7. Transmit the Device address.(specifying the read mode).
  8. Read the Data as per your wish.(Master should send acknowledgement for each byte it read)
  9. Perform the steps 1 to 6 to read the desired registers.
  10. once the reading is done master should send negative acknowledgement to the IC.
  11. Transmit the Stop Bit.
NOTE: Transmitting the start bits, stop bits, device address, register address  explained above can be used for reading the data from the IC. 
Reading the data from the IC: 
The data is read from the IC bit by bit and an acknowledgement is sent by the master to the IC after 8 bits. We can read N number of data and  finally conclude it by sending a NEGATIVE ACKNOWLEDGEMENT. The register address need not be mentioned every time, the slave device can increment the address automatically. But if you need to read from a specific address or if the slave device does not increment the address you have to mention the register address.  Thus the  data is read from the slave device. Then Stop Bit has to be sent as mentioned above to stop the communication.


The above diagram shows the start bit, transmission of device address(0xA0,WRITE mode), transmission of register address(0×0000), then transmitting the data(0×41) and finally the stop bits of EEPROM 24C256
CLOCK STRETCHING: 
While reading data from the slave, the slave might take some extra time than the one prescribed in the data sheet. In such a case the slave has the ability to hold the clock low until It puts the data in the SDA line. this process is known as CLOCK STRETCHING.
AUTHOR
Hari Prasath
Project Engineer

No comments:

Post a Comment