#include <REGX52.H>
sbit SCL = P2 ^ 1;
sbit SDA = P2 ^ 0;
/**
* @brief: I2C起始条件
* @author: Rukawa
* @date: 2024-03-18 08:19:36
**/
void I2C_start()
{
SDA = 1;
SCL = 1;
SDA = 0;
SCL = 0;
}
/**
* @brief: I2C结束条件
* @author: Rukawa
* @date: 2024-03-18 08:30:59
**/
void I2C_stop()
{
SDA = 0;
SCL = 1;
SDA = 1;
}
/**
* @brief: I2C发送一个字节
* @param: 要发送的数据
* @author: Rukawa
* @date: 2024-03-18 08:31:26
**/
void I2C_sendByte(unsigned char content)
{
unsigned char i;
for (i = 0; i < 8; i++)
{
SDA = content & (0x80 >> i);
SCL = 1;
SCL = 0;
}
}
/**
* @brief: I2C接收一个字节
* @retval: 接收到的数据
* @author: Rukawa
* @date: 2024-03-18 08:36:38
**/
unsigned char I2C_receiveByte()
{
unsigned char i,result = 0x00;
SDA = 1; //释放SDA
for (i = 0; i < 8; i++)
{
SCL = 1;
if(SDA){
result |= 0x80 >> i;
}
SCL = 0;
}
return result;
}
/**
* @brief: I2C发送应答
* @param: 0:应答 1:非应答
* @author: Rukawa
* @date: 2024-03-18 12:14:57
**/
void I2C_sendAck(unsigned char ack)
{
SDA = ack;
SCL = 1;
SCL = 0;
}
/**
* @brief: I2C接收应答
* @retval: 0:应答 1:非应答
* @author: Rukawa
* @date: 2024-03-18 12:15:35
**/
unsigned char I2C_receiveAck()
{
unsigned char result = 0;
SDA = 1; //释放SDA
SCL = 1;
result = SDA;
SCL = 0;
return result;
}
I2C读写
本文档详细介绍了I2C通信的起始、结束条件,以及发送、接收字节和应答处理的函数实现,适用于嵌入式系统开发。
摘要由CSDN通过智能技术生成