新闻中心

EEPW首页 > 嵌入式系统 > 设计应用 > I2C通信模块c语言库文件

I2C通信模块c语言库文件

作者: 时间:2016-11-30 来源:网络 收藏

//(主机接收函数将要调用)从设备发生的应答信号
void I2C_Ack()
{
SDA=0;
_nop_();
_nop_();
SCL=1;
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
SCL=0;
_nop_();
_nop_();
_nop_();
}

//非应答信号产生函数
void I2C_NoAck()
{
SDA=1;
_nop_();
_nop_();
SCL=1;
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
SCL=0;
_nop_();
_nop_();
_nop_();
}

//主机发送多字节函数
bit SendString(uint8 address,uint8 *s,uint8 num)
{
uint8 i;
I2C_Start();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
I2C_SendByte(address);
if(ACK==0)
return 0;
for(i=0;i{
I2C_SendByte(*s);
if(ACK==0)
return 0;
DelayMs(10);//以等待设备写入数据处理完成
s++;
}
I2C_Stop();
DelayUs(10);
return 1;
}

//主机接收多字节函数
bit ReceiveString(uint8 address,uint8 *s,uint8 num)
{
uint8 i;
I2C_Start();
I2C_SendByte(address);
if(ACK==0)
return 0;
I2C_Start();
I2C_SendByte(address+1);
if(!ACK)
return 0;
for(i=0;i{
*s=I2C_ReceiveByte();
I2C_Ack();
DelayMs(10);
s++;
}
*s=I2C_ReceiveByte();
I2C_NoAck(); //给的非应答信号
I2C_Stop(); //停止总线
return 1;
}

#endif

库文件的调用例子:http://www.51hei.com/bbs/dpj-20623-1.html


上一页 1 2 下一页

评论


技术专区

关闭