IIC communication protocol

A, IIC basic features

Two-wire communication: a data line (SDA) and a clock line (SCL).
IIC can be linked to a plurality of devices, the main points of the machine (host access Slave). On the IIC bus device has a unique device address, a corresponding host is determined by the slave device address.
IIC half-duplex communication, the hardware required pull-up resistor is generally typical value is 4.7K ~ 10K 4.7K. The maximum limit on the bus capacitance of 400pF.
Here Insert Picture Description

Two, IIC Communication Timing

IIC is high when idle.
Write data from the host machine to
signal the start and stop Here Insert Picture Description
start signal: when the SCL is high, SDA from high to low
stop signal: when the SCL is high, SDA from low to high

void I2C_Start(void)   //起始信号函数
{
 SDA_Out(1);   //SDA输出高
 SCL_Out(1);   //SCL输出高
 delay_us(5);
 SDA_Out(0);   //SDA输出低
 delay_us(5);
 SCL_Out(0);
 delay_us(3);
}
void I2c_Stop(void)   //停止信号函数
{
 SDA_Out(0);    //SDA输出低
 SCL_Out(0);    //SCL输出低
 delay_us(5);
 SCL_Out(1);    //SCL输出高
 delay_us(5);
 SDA_Out(1);
 delay_us(3);
}

Data changes to a low level only in SCl

 ****主机发送数据到从机****

Here Insert Picture Description
Shaded data transmitted to the host machine, blank data is returned from the machine.
Host first sends: start signal + high [7bit slave address as an address, the lowest bit is read-write bit (0: write operation to the slave; 1: Slave read) Read +] ACK (sent from the slave to the master ) read + write data + ACK + ... read + write data + ACK + stop signal.
Send data to the host computer is a slave to the host machine from the ACK.
Receiving data from a host machine to
Here Insert Picture Description
transmit data to the data returned from the slave machine, the host is shaded white.
Host first sends: start signal + high [7bit slave address as an address, the lowest bit is read-write bit (0: write operation to the slave; 1: Slave read) Read +] ACK (sent from the slave to the master ) read + write data + ACK + ... + read + write data ACK + stop signal.
Summary ACK: ACK is sent to the data receiving device of the data transmission device.

void Ack(void)  //主机读ACK
{ 
    SCL_Out(0);  
    delay_us(2); 
 SDA_Out(1);
 SDA_IN(); 
 delay_us(2);
 SCL_Out(1);
 SDA_Out(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_7));  
    delay_us(4);
 SCL_Out(0);
    SDA_OUT();
 delay_us(2);
}
void Nack(uint8_t ack)  //主机写ACK
{
 SCL_Out(0);
 delay_us(2); 
 if(ack == 1)
    SDA_Out(1);
 else
   SDA_Out(0); //写应答信号
  delay_us(2);   
  SCL_Out(1);                    //拉高时钟线
  delay_us(4);                 //延时
  SCL_Out(0);                   //拉低时钟线
  delay_us(2);                 //延时

}

The master sends Ack function data, and the last function is the host receives a Nack Nack high data

void I2c_SendByte(uint8_t _ucByte)  //主机发送8bit数据
{
  uint8_t i;
  for (i = 0; i < 8; i++)
 {  
   if (_ucByte & 0x80)
    SDA_Out(1);
  else
    SDA_Out(0); 
 delay_us(2);
  SCL_Out(1);
  delay_us(2);
  _ucByte <<= 1;   //左移一个bit 
  SCL_Out(0);
 }
 SDA_Out(0);
 delay_us(1);
 SCL_Out(0);
 delay_us(1);
}
uint8_t I2c_ReadByte(void)    //主机接收8bit数据
{
  uint8_t i;
  uint8_t value;
  /* 读到第1个bit为数据的bit7 */
 value = 0;
 for (i = 0; i < 8; i++)
 {
  value<<=1;
  SCL_Out(0);
  SDA_IN(); 
  delay_us(2);
  if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_7))
   value |= 0x01 ;
  else
   value &= 0xfe;
  SCL_Out(1);
  delay_us(2);
 }
 SCL_Out(0);
 SDA_OUT();
 delay_us(2);
 SDA_Out(0);
 delay_us(2);
 return value;
}
int I2C_Write(uint8_t addr , uint8_t* buff, uint8_t size)  //主机发数据
{
 uint8_t i; 
 I2C_Start();
  I2c_SendByte(addr);
  Ack();
  for(i = 0; i < size; i++)
 {
 I2c_SendByte(buff[i]);
  Ack();
 }
  I2c_Stop(); 
 return 0;
}
int I2C_Read(uint8_t addr , uint8_t* buff, uint8_t size,uint8_t bus_bit) //主机接收数据
{
 uint8_t i,busy; 
 I2C_Start();
  I2c_SendByte(addr+1);
 delay_us(2);
 Ack(); 
  for(i = 0; i< (size-1); i++)
 {
      buff[i] = I2c_ReadByte();
   delay_us(2);
    Nack(0);
  } 
  buff[size-1] = I2c_ReadByte();
 delay_us(2);
  Nack(1);
 delay_us(10);
  I2c_Stop(); 
 return 0;
}
Published an original article · won praise 0 · Views 16

Guess you like

Origin blog.csdn.net/qq_37387733/article/details/104786198