📄 24c16.h
字号:
//*********************************I2c开始函数*************************
//函数;void I2cStart(void)
//描述;启动I2C通讯 -----\
//数据有效状态: \--- SCL
// -----\
// \-- SDA
//*********************************************************************
void I2cStart(void) //起始信号
{
SDA = 1; //数据线拉高
SCL=1; //时钟线拉高 使启动有效
SDA=0; //数据线由高到低 启动I2C通信
SCL = 0; //时钟线拉低
}
//**********************************I2c停止函数************************
//函数;void I2cStop(void)
//描述;停止I2C通讯 -----\
//数据有效状态: \--- SCL
// /-----
// --/ SDA
//*********************************************************************
void I2cStop(void) //停止信号
{
SDA = 0; // 数据线拉低
SCL = 1; // 时钟线拉高 使停止有效
SDA = 1; // 数据线由低到高 停止I2C通信
SDA=0; // 数据线拉低
SCL=0; // 时钟线拉低
}
//*********************************I2c应答函数*************************
//函数;void I2cAck(void)
//描述;启动I2C通讯
//*********************************************************************
void I2cAck(void) //应答信号
{
SDA=0;
SCL=1;wait;
SCL=0;wait; //
}
//*********************************I2c应答函数*************************
//函数;void I2cNOAck(void)
//描述;
//*********************************************************************
void I2cNOAck(void) //应答信号
{
SDA=1;wait;
SCL=1;wait;
SCL=0;wait;
}
//***********************************I2c发送字节函数*********************
//函数; unsigned char I2cSendByte(unsigned char bytedata)
//描述; 发送一个字节到slave
//参数; bytedata---被发送的数据
//返回值: 1,收到应答;0,没收到应答
//***********************************************************************
void I2cSendByte(unsigned char bytedata)
{
unsigned char i;
for(i=0;i<8;i++)
{
if(bytedata & 0x80)
SDA = 1;
else
SDA = 0;
bytedata <<= 1;
SCL = 1;
wait;
SCL = 0;
}
SDA=1;
SCL=1;
SCL=0;
__RESET_WATCHDOG();
}
//**************************************I2c接收字节函数*********************
//函数;unsigned char I2cReceiveByte(void)
//描述;I2C接收一个字节数据
//参数;
//返回值:接收的字节数据
//**************************************************************************
unsigned char I2cReceiveByte(void)
{
unsigned char i;
unsigned char bytedata = 0;
SDA_IO=0;
// SDA_UP=1;
for(i=0;i<8;i++)
{
SCL = 1;
bytedata <<= 1;
if(SDA)
{
bytedata |= 0x01;
}
SCL = 0;
}
SDA_IO=1;
__RESET_WATCHDOG();
return bytedata;
}
//**************************************I2c发送字节函数*********************
//函数; unsigned char I2SendByte(unsigned char device,unsigned int address,unsigned char bytedata)
//描述; I2C发送一个字节数据
//参数; device---命令字 ,address---slave内的地址,bytedata--发送的数据
//返回值:接收的字节数据
//**************************************************************************
void I2SendByte(unsigned char device,unsigned int address,unsigned char bytedata)
{
unsigned char addressh,addressl;
addressh =(uchar)(address&0xff00>>8);
addressl =(uchar)(address&0x00ff);
I2cStart();
I2cSendByte(device);
I2cSendByte(addressh);
I2cSendByte(addressl);
I2cSendByte(bytedata);
I2cStop();
}
//**************************************I2c读入一个字节函数*************************
//函数;unsigned char I2cByteRead(unsigned char device,unsigned char address)
//描述;从slave接收1字节数据
//参数;device---命令字 ,address---slave内的地址
//返回值:bytedata---被写入的数据
//**************************************************************************
unsigned char I2cByteRead(unsigned char device,unsigned int address)
{
unsigned char bytedata;
unsigned char addressh,addressl;
addressh =(uchar)(address&0xff00>>8);
addressl =(uchar)(address&0x00ff);
I2cStart();
I2cSendByte(device);
I2cSendByte(addressh);
I2cSendByte(addressl);
I2cStart();
I2cSendByte(device|0x01);
bytedata = I2cReceiveByte();
I2cStop();
return bytedata;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -