我用51模拟了I2C协议来读写CAT1025,但是读入的和写出的总不一样,查不出原因,麻烦各位帮我看看,先谢谢了,下面是我的子程序代码
void CAT1025_Write_Byte(unchar addr, unchar dat)
{
unchar cat_addr = 0xa0;
unchar i;
SCL = 0; //start condition
SDA = 1;
delay2(5);
SCL = 1;
delay2(5);
SDA = 0;
SCL = 0;
delay2(5);
for(i=0; i<8; i++) //send the cat1025 address
{
if(cat_addr&0x80)SDA=1;
else SDA=0;
SCL = 1;
delay2(5);
cat_addr <<= 1;
SCL = 0;
delay2(5);
}
SDA = 1; //release bus
SCL = 1; // ask from cat1025
delay2(5);
SCL = 0;
delay2(5);
for(i=0; i<8; i++) //send the cat1025 address
{
if(addr&0x80)SDA=1;
else SDA=0;
SCL = 1;
delay2(5);
addr <<= 1;
SCL = 0;
delay2(5);
}
SDA = 1; //ask from cat1025
SCL = 1;
delay2(5);
SCL = 0;
delay2(5);
for(i=0; i<8; i++) //send the dat
{
if(dat&0x80)SDA=1;
else SDA=0;
SCL = 1;
delay2(5);
dat <<= 1;
SCL = 0;
delay2(5);
}
SDA = 1; //ask from cat1025
SCL = 1;
delay2(5);
SCL = 0;
delay2(5);
SDA = 0;
SCL = 1;
delay2(5);
SDA = 1;
delay2(100);
}
unchar CAT1025_Read_Byte(unchar addr)
{
unchar i;
unchar cat_addr;
unchar dat=0;
SCL = 0; //start condition
SDA = 1;
delay2(5);
SCL = 1;
delay2(5);
SDA = 0;
SCL = 0;
delay2(5);
cat_addr = 0xa0;
for(i=0; i<8; i++) // send the addr of cat1025
{
if(cat_addr&0x80) SDA = 1;
else SDA = 0;
SCL = 1;
delay2(5);
SCL = 0;
delay2(5);
cat_addr <<= 1;
}
SDA = 1; //ask from cat1025
SCL = 1;
delay2(5);
SCL = 0;
delay2(5);
for(i=0; i<8; i++) //send the addr of dat
{
if(addr&0x80) SDA = 1;
else SDA = 0;
SCL = 1;
delay2(5);
SCL = 0;
addr <<= 1;
delay2(5);
}
SDA = 1; //ask from cat1025
SCL = 1;
delay2(5);
SCL = 0;
SDA = 1; // start condition
SCL = 1;
delay2(5);
SDA = 0;
SCL = 0;
delay2(5);
cat_addr = 0xa1; // send the addr of cat1025
for(i=0; i<8; i++)
{
if(cat_addr&0x80)SDA = 1;
else SDA = 0;
SCL = 1;
delay2(5);
SCL = 0;
delay2(5);
cat_addr <<= 1;
}
SDA = 1; //ask from cat1025
SCL = 1;
delay2(5);
SCL = 0;
delay2(5);
for(i=0; i<8; i++) //receive dat from cat1025
{
SCL = 1;
delay2(5);
if(SDA) dat|=0x01;
dat <<= 1;
SCL = 0;
delay2(5);
}
SDA = 0; // stop condition
SCL = 1;
delay2(5);
SDA = 1;
delay2(5);
return dat;
}
|