写一段iic通讯代码,用C语言,51单片机
时间: 2023-03-16 17:43:47 浏览: 206
关于IIC通信的代码
#include <reg52.h>//I2C通讯用到的定义
sbit SDA = P3^6;
sbit SCL = P3^7;//读取I2C数据
unsigned char I2C_Read(unsigned char ack)
{
unsigned char i,data=0;
SDA = 1;
for(i=0;i<8;i++)
{
SCL = 1;
data <<= 1;
data |= SDA;
SCL = 0;
}
SDA = ack;
SCL = 1;
SCL = 0;
return(data);
}//写入I2C数据
void I2C_Write(unsigned char data)
{
unsigned char i;
for(i=0;i<8;i++)
{
SDA = (data & 0x80) >> 7;
SCL = 1;
SCL = 0;
data <<= 1;
}
SDA = 1;
SCL = 1;
SCL = 0;
}
阅读全文