连续写入
void i2c_write( unsigned char *strpuf,unsigned char num ,u8 addr) // strpuf是传入的数组,num是传递的个数,addr是分配的地址
{
I2CStart();
I2CSendByte(0xa0);
I2CWaitAck();
I2CSendByte(addr);
I2CWaitAck();
while(num--)
{
I2CSendByte(*strpuf++);
I2CWaitAck();
}
I2CStop();
Delay_Ms(5); //这个5ms的延时,是给写入预留时间,很重要!!没有它不能正确写入数据
}
连续读
void i2c_read(unsigned char *readpuf,u8 rnum,u8 Addr)
{
I2CStart();
I2CSendByte(0xa0);
I2CWaitAck();
I2CSendByte(Addr);
I2CWaitAck();
I2CStart();
I2CSendByte(0xa1);
I2CWaitAck();
while(rnum--)
{
*readpuf++ = I2CReceiveByte();
if(rnum) I2CSendAck();
else I2CSendNotAck();
}
I2CStop();
}
单个读字节
unsigned char At24c02Read(unsigned char addr) //
{
unsigned char num;
I2CStart();
I2CSendByte(0xa0);
I2CWaitAck();//???????
I2CSendByte(addr);
I2CWaitAck(); //????????
I2CStart();
I2CSendByte(0xa1);
I2CWaitAck();
num=I2CReceiveByte();
I2CSendAck();
I2CStop();
return num;
}
单个写,
void At24c02Write(unsigned char addr,unsigned char dat)
{
I2CStart();
I2CSendByte(0xa0);
I2CWaitAck();
I2CSendByte(addr);
I2CWaitAck();
I2CSendByte(dat);
I2CWaitAck();
I2CStop();
}
使用单个写时注意:
同样,