1. IIC bus communication
1 Introduction:
IIC stands for Inter-Integrated Circuit (Integrated Circuit Bus). The I2C bus is a serial bus introduced by PHLIPS. The I2C bus has only two bidirectional signal lines. One is the data line SDA and the other is the clock line SCL .
Each device connected to the I2C bus has a unique address . The data transfer between the host and other devices can be sent by the host to other devices, then the host is the transmitter. A device that receives data on the bus is a receiver.
2. Commonly used serial expansion buses are:
(1), I2C (Inter IC BUS) bus
(2), single bus (1-WIRE BUS)
(3), SPI (Serial Peripheral Interface) bus, etc.
3. IIC bus circuit connection
The I2C bus is connected to the positive power supply through a pull-up resistor. When the bus is idle, both lines are high.
4. IIC bus communication protocol
(1) Start and end signals
During the period when the SCL line is at a high level, the change of the SDA line from a high level to a low level indicates the start signal;
During the period when the SCL line is at a high level, the change of the SDA line from a low level to a high level indicates a termination signal.
(2) Answer and non-response
The IIC bus protocol stipulates that after each byte of data is transmitted, there must be a response signal to determine whether the data transmission is received by the other party. The response signal is generated by the receiving device . During the period when SCL is at a high level, the receiving device pulls SDA low to a low level, indicating that the data transmission is correct and a response is generated.
(3) Data transmission
Data Bit Validity Regulations
When the I2C bus is transmitting data , the data on the data line must remain stable during the period when the clock signal is at a high level. Changes are allowed .
5. Basic functions of IIC communication
(1) Start and stop signal
i2c_Start()
{
SDA = 1; //SCL为高电平时,SDA 出现一个下跳沿表示I2C总线启动
SCL = 1;
i2c_Delay();
SDA = 0;
i2c_Delay();
SCL = 0; // 启动后将SCL点平拉低
i2c_Delay();
}
(2) Termination signal
void i2c_Stop()
{
SDA = 0; //当SC高电平时,SDA出现一个上升沿表示I2C总线停止
SCL = 1;
i2c_Delay();
SDA = 1;
i2c_Delay();
}
(3) Send data
void i2c_SendByte(unsigned char Byte)
{
unsigned char i = 0;
for (i = 0; i < 8; i++)
{
if(Byte & 0x80) //IIC规定下发送高位数据
{
SDA = 1;
}
else
SDA = 0;
SCL = 1; //在SCL为高电平期间写入数据
i2c_Delay();
SCL = 0;
Byte <<= 1;
if (i == 7)
SDA = 1; //在发送完数据后将SDA拉高
i2c_Delay();
}
}
(4) Receive data
unsigned char i2c_ReadByte()
{
unsigned char i = 0;
unsigned char value = 0;
for (i = 0; i < 8; i++)
{
value <<= 1; //先读取高位
SCL = 1; //SCL 为高电平时读取数据
i2c_Delay();
if(SDA)
{
value++;
}
SCL = 0;
i2c_Delay();
}
return value;
}
(5) Send a response signal
void char i2c_Ack()
{
SDA = 0; //SCL为高电平时间SDA拉低表示应答
i2c_Delay();
SCL = 1;
i2c_Delay();
SCL = 0;
i2c_Delay();
SDA = 1;
i2c_Delay();
}
(6) Send a non-response signal
void i2c_NAck()
{
SDA = 1; //SCL高电平期间SDA拉高表示非应答
i2c_Delay();
SCL = 1;
i2c_Delay();
SCL = 0;
i2c_Delay();
}
(7) Waiting for a response
unsigned char i2c_WaitAck()
{
unsigned char ret;
SDA = 1;
i2c_Delay();
SCL = 1; //把SCL拉高读取SDA的点平高低,低电平表示应答
i2c_Delay();
if(SDA == 1)
{
ret = 1;
}
else
ret = 0;
SCL = 0;
i2c_Delay();
return ret;
}
(8) Check the IIC device bus
unsigned char i2c_CheckDevice(unsigned char _Address)
{
unsigned char UcAck;
if (SDA == 1 && SCL == 1)
{
i2c_Start();
i2c_SendByte(_Address|I2C_WR); //发送地址
ucAck = i2c_WaitAck(); //等待接收应答
i2c_Stop(); //停止信号
return ucAck;
}
return 1;
}