//==================24C02写单个字节====================
void Write_24C02_Byte(unsigned char addr, unsigned char dat)
{
I2CStart(); //起始信号
I2CSendByte(0xa0); //设备写地址
I2CWaitAck(); //等待从机应答
I2CSendByte(addr); //内存单元地址
I2CWaitAck(); //等待从机应答
I2CSendByte(dat); //写入数据
I2CWaitAck(); //等待从机应答
I2CStop(); //停止信号
}
//==================24C02读单个字节====================
unsigned char Read_24C02_Byte(unsigned char addr)
{
unsigned char tmp;
//进行一个伪写操作
I2CStart(); //起始信号
I2CSendByte(0xa0); //设备写地址
I2CWaitAck(); //等待从机应答
I2CSendByte(addr); //内存单元地址
I2CWaitAck(); //等待从机应答
//进行字节读操作
I2CStart(); //起始信号
I2CSendByte(0xa1); //设备读地址
I2CWaitAck(); //等待从机应答
tmp = I2CReceiveByte(); //读取数据
I2CSendAck(1); //产生非应答信号
I2CStop(); //停止信号
return tmp;
}
unsigned char dat[8];
//==================24C02写多个字节====================
void Write_24C02_Page(unsigned char addr, unsigned char *dat, unsigned char len)
{
unsigned char i;
I2CStart(); //起始信号
I2CSendByte(0xa0); //设备写地址
I2CWaitAck(); //等待从机应答
I2CSendByte(addr); //内存单元地址
I2CWaitAck(); //等待从机应答
for(i = 0; i < len; i++)
{
I2CSendByte(dat[i]); //连续写入多个字节
I2CWaitAck(); //等待从机应答
}
I2CStop(); //停止信号
}
unsigned char dat[8];
//==================24C02读多个字节====================
void Read_24C02_Page(unsigned char addr, unsigned char *dat, unsigned char len)
{
unsigned char i;
//进行一个伪写操作
I2CStart(); //起始信号
I2CSendByte(0xa0); //设备写地址
I2CWaitAck(); //等待从机应答
I2CSendByte(addr); //内存单元地址
I2CWaitAck(); //等待从机应答
//进行字节读操作
I2CStart(); //起始信号
I2CSendByte(0xa1); //设备读地址
I2CWaitAck(); //等待从机应答
for(i = 0; i < len-1; i++)
{
dat[i] = I2CReceiveByte(); //连续读取多个字节
I2CSendAck(0); //产生应答信号
}
dat[len-1] = I2CReceiveByte(); //读取最后一个字节
I2CSendAck(1); //产生非应答信号
I2CStop(); //停止信号
}