游客
主题
帖子
积分34
阅读权限1
注册时间2011-12-25
最后登录1970-1-1
在线时间 小时
|
时序应该是对的吧,代码如下,请教下器件地址为什么是0x78呢?手册里A2A3好像是接地的额。。/***********************************************************
@function: bool I2C_Start(void)
@description:I2C启动函数
@input: void
@output: void
************************************************************/
bool I2C_Start(void)
{
SDA_H();
SCL_H();
I2C_delay();
SDA_L();
I2C_delay();
if(SDA_Read()){return FALSE;}
//SDA线为高电平则总线出错,退出
SDA_L();
I2C_delay();
return TRUE;
}
/***********************************************************
@function: void I2C_Stop(void)
@description:I2C停止函数
@input: void
@output: void
************************************************************/
void I2C_Stop(void)
{
SCL_L();
I2C_delay();
SDA_L();
I2C_delay();
SCL_H();
I2C_delay();
SDA_H();
I2C_delay();
}
/***********************************************************
@function: void I2C_Ack(void)
@description:I2C应答函数
@input: void
@output: void
************************************************************/
void I2C_Ack(void)
{
SCL_L();
I2C_delay();
SDA_L();
I2C_delay();
SCL_H();
I2C_delay();
SCL_L();
I2C_delay();
}
/***********************************************************
@function: void I2C_NoAck(void)
@description:I2C不应答函数
@input: void
@output: void
************************************************************/
void I2C_NoAck(void)
{
SCL_L();
I2C_delay();
SDA_H();
I2C_delay();
SCL_H();
I2C_delay();
SCL_L();
I2C_delay();
}
/***********************************************************
@function: bool I2C_WaitAck(void)
@description:I2C等待应答函数
@input: void
@output: TRUE-收到应答 ,FLASE-无应答
************************************************************/
bool I2C_WaitAck(void)
//返回为:=1有ACK,=0无ACK
{
SCL_L();
I2C_delay();
SDA_H();
I2C_delay();
SCL_H();
I2C_delay();
if(SDA_Read())
{
SCL_L();
return FALSE;
}
SCL_L();
return TRUE;
}
/***********************************************************
@function: void I2C_SendByte(u8 SendByte)
@description:I2C发送一字节函数
@input: void
@output: void
************************************************************/
void I2C_SendByte(u8 SendByte) //数据从高位到低位//
{
u8 i=8;
while(i--)
{
SCL_L();
I2C_delay();
if(SendByte&0x80)
{
SDA_H();
}
else
{
SDA_L();
}
SendByte<<=1;
I2C_delay();
SCL_H();
I2C_delay();
}
SCL_L();
}
/***********************************************************
@function: uchar I2C_ReceiveByte(void)
@description:I2C接收一字节函数
@input: void
@output: 接收的字节
uchar类型
************************************************************/
u8 I2C_ReceiveByte(void)
{
u8 i=8;
u8 ReceiveByte=0;
SDA_H();
while(i--)
{
ReceiveByte<<=1;
SCL_L();
I2C_delay();
SCL_H();
I2C_delay();
if(SDA_Read())
{
ReceiveByte|=0x01;
}
}
SCL_L();
return ReceiveByte;
} |
|