void I2cWait(void)/*I2CBUS等待*/
{
_nop_();
_nop_();
}
void DelayX1ms(unit count)/*延时XmS子程序*/
{
unit i;
uchar j;
for(i=0;i <count;i++)
for(j=0;j <110;j++);
}
void I2cStart(void)/*I2CBUS起始信号*/
{
ISDA=1;
ISCL=1;
_nop_();
ISDA=0;
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
ISCL=0;
}
void I2cStop(void) /*I2CBUS停止信号*/
{
ISDA=0;
_nop_();
_nop_();
ISCL=1;
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
ISDA=1;
}
void I2cInit(void)/*I2CBUS初始化*/
{
ISDA=1;
ISCL=1;
}
bit slave_ack(void)/*I2CBUS应答信号*/
{
bit ack;
ISDA=1;
_nop_();
_nop_();
ISCL=1;
_nop_();
_nop_();
_nop_();
_nop_();
ack=ISDA;
ISCL=0;
return ack;
}
bit I2cSendByte(uchar bytedata)/*传送一个字节到slave*/
{
uchar i;
bit ack;
for (i=0;i <8;i++)
{
if (bytedata&0x80)ISDA=1;
else ISDA=0;
bytedata < <=1;
ISCL=1;
I2cWait();
I2cWait();
ISCL=0;
I2cWait();
}
ack=slave_ack();
return ack;
}
void IICACK(void)
{
ISDA=0;
_nop_();
_nop_();
ISCL=1;
_nop_();
_nop_();
_nop_();
_nop_();
ISCL=0;
}
/*****************不对IIC总线产生应答***************/
void IICNoAck(void)
{
ISDA=1;
_nop_();
_nop_();
ISCL=1;
_nop_();
_nop_();
_nop_();
_nop_();
ISCL=0;
}
void I2cSendAckmowledge(bit ack)/*传送确认信号至slave*/
{
ISDA=ack;
ISCL=1;
I2cWait();
ISCL=0;
}
uchar I2cReceiveByte(void) /*从slave接收一个字节*/
{
uchar i,bytedata=0;
ISDA=1;
for (i=0;i <8;i++)
{
_nop_();
_nop_();
_nop_();
ISCL=1;
I2cWait();
bytedata < <=1;
if (ISDA) bytedata|=0x01;
ISCL=0;
}
return bytedata;
}
void I2cByteWrite(uchar device,uchar address1,uchar address2,uchar bytedata)/*主机将一字节数据写入指定slave地址中*/
{
bit ack;
I2cStart();
resent1:I2cSendByte(device);
if (ack==1)
goto resent1;
resent2:I2cSendByte(address1);
if (ack==1)
goto resent2;
resent3:I2cSendByte(address2);
if (ack==1)
goto resent3;
resent4:I2cSendByte(bytedata);
if (ack==1)
goto resent4;
I2cStop();
ISCL=0;/*here is added --lwg*/
}
void I2c64ByteWrite(uchar device,uchar address1,uchar address2) /*the master send 64 bytes data to the slave*/
{
bit ack;
uchar j;
I2cStart();
resent5:I2cSendByte(device);
if (ack==1)
goto resent5;
resent6:I2cSendByte(address1);
if (ack==1)
goto resent6;
resent7:I2cSendByte(address1);
if (ack==1)
goto resent7;
for (j=0;j <64;j++)
{
sdata = j+20;
resent8:I2cSendByte(sdata);
if (ack==1)
goto resent8;
ISCL=0; /*here is added--lwg*/
}
I2cStop();
ISCL=0; /*here is added--lwg*/
}
uchar I2cByteRead(uchar device,uchar address1,uchar address2)/*主机从指定slave地址中读一字节数据*/
{
uchar ldata;
bit ack;
I2cStart();
resent9:I2cSendByte(device);
if (ack==1)
goto resent9;
resent10:I2cSendByte(address1);
if (ack==1)
goto resent10;
resent11:I2cSendByte(address2);
if (ack==1)
goto resent11;
I2cStart();
resent12:I2cSendByte(device|0x01);
if (ack==1)
goto resent12;
ldata=I2cReceiveByte();
I2cStop();
ISCL=0; /*here is added--lwg*/
return ldata;
}
uchar I2c64ByteRead(uchar device,uchar address1,uchar address2)/*主机从指定slave地址中读64字节数据*/
{
uchar k=0;
uchar ldata,j;
bit ack;
I2cStart();
resent13:I2cSendByte(device);
if (ack==1)
goto resent13;
resent14:I2cSendByte(address1);
if (ack==1)
goto resent14;
resent15:I2cSendByte(address2);
if (ack==1)
goto resent15;
for (j=0;j <64;j++)
{
rece[j]=I2cReceiveByte();
if (k <63)
I2cSendAckmowledge(0); /*the ack should be low voltage --lwg*/
ISCL=0; /*here is added--lwg*/
}
I2cStop();
ISCL=0; /*here is added--lwg*/
return 0x88;
}
void main (void)
{
uchar idata send,receive,j;
uchar idata addr1,addr2;
again:
P1_7=0;
slvdevice = 0xa2;
addr1 =0x00;
addr2 =0x00;
I2c64ByteWrite(slvdevice,addr1,addr2);/*主机将send内数据slave的0x55单元中,send在Debug下输入*/
receive=I2c64ByteRead(slvdevice,addr1,addr2);/*主机将slave的0x55单元中的数据读入它的receive 内*/
DelayX1ms(10);
P1_7=1;
goto again;
}