简单的模拟,但是读取的数据总是0xff,请教各位。iic的函数应该不会有问题,因为是之前学开发板的标准程序 复制过来的。
但是怎么改都是0xff
已经改了两天了。还是这样。
麻烦各位帮忙解答一下 谢谢
void main()
{
unsigned char dat;
Init_Uart();
E2WriteByte(0x02,0x01) ;
dat = E2ReadByte(0x02);
SendByte(dat);
while (1);
}
unsigned char E2ReadByte(unsigned char addr)
{
unsigned char dat;
I2CStart();
I2CWrite(0x50<<1);
I2CWrite(addr);
I2CStart();
I2CWrite((0x50<<1)|0x01);
dat = I2CReadNAK();
I2CStop();
return dat;
}
void E2WriteByte(unsigned char addr, unsigned char dat)
{
I2CStart();
I2CWrite(0x50<<1);
I2CWrite(addr);
I2CWrite(dat);
I2CStop();
}
void I2CStart()
{
I2C_SDA = 1;
I2C_SCL = 1;
I2CDelay();
I2C_SDA = 0;
I2CDelay();
I2C_SCL = 0;
}
void I2CStop()
{
I2C_SCL = 0;
I2C_SDA = 0;
I2CDelay();
I2C_SCL = 1;
I2CDelay();
I2C_SDA = 1;
I2CDelay();
}
bit I2CWrite(unsigned char dat)
{
bit ack;
unsigned char mask;
for (mask=0x80; mask!=0; mask>>=1)
{
if ((mask&dat) == 0)
I2C_SDA = 0;
else
I2C_SDA = 1;
I2CDelay();
I2C_SCL = 1;
I2CDelay();
I2C_SCL = 0;
}
I2C_SDA = 1;
I2CDelay();
I2C_SCL = 1;
ack = I2C_SDA;
I2CDelay();
I2C_SCL = 0;
return (~ack);
}
unsigned char I2CReadNAK()
{
unsigned char mask;
unsigned char dat;
I2C_SDA = 1;
for (mask=0x80; mask!=0; mask>>=1)
{
I2CDelay();
I2C_SCL = 1;
if(I2C_SDA == 0)
dat &= ~mask;
else
dat |= mask;
I2CDelay();
I2C_SCL = 0;
}
I2C_SDA = 1;
I2CDelay();
I2C_SCL = 1;
I2CDelay();
I2C_SCL = 0;
return dat;
}
unsigned char I2CReadACK()
{
unsigned char mask;
unsigned char dat;
I2C_SDA = 1;
for (mask=0x80; mask!=0; mask>>=1)
{
I2CDelay();
I2C_SCL = 1;
if(I2C_SDA == 0)
dat &= ~mask;
else
dat |= mask;
I2CDelay();
I2C_SCL = 0;
}
I2C_SDA = 0;
I2CDelay();
I2C_SCL = 1;
I2CDelay();
I2C_SCL = 0;
return dat;
}
|