分享单片机编程步骤 c51单片机编程教程( 二 )


return(rbyte);
}
unsigned char Read_24LC16B(unsigned int RomAddress)
{
unsigned char output,block;
ReadDeviceAddress=0B10100001;
WriteDeviceAddress=0B10100000;
block=RomAddress/256;
RomAddress=RomAddress%256;
WriteDeviceAddress=WriteDeviceAddress|(block<<1);
ReadDeviceAddress=ReadDeviceAddress|(block<<1);
i2cStart();
WriteByte_24LC16B(WriteDeviceAddress);
WriteByte_24LC16B((unsigned char)RomAddress);
i2cStart();
WriteByte_24LC16B(ReadDeviceAddress);
output=ReadByte_24LC16B();
i2cNoAck_MCU();
i2cStop();
_delay(2000);
return(output);
}
3、页写操作
Wdata为输入数组的首地址,RomAddress为需要进行存储的地址,范围在0~2047之间,cnt为一次需要写入的字节个数,建议采用8的倍数的cnt,因为本函数不采用自动分页,不是8的倍数会在超出页写入最多的字节数之后覆盖掉原来的数 。
void WritePage_24LC16B(unsigned char *Wdata,unsigned int RomAddress,unsigned char cnt)
{
unsigned char block;
WriteDeviceAddress=0B10100000;
block=RomAddress/256;
RomAddress=RomAddress%256;
WriteDeviceAddress=WriteDeviceAddress|(block<<1);
i2cStart();
WriteByte_24LC16B(WriteDeviceAddress);
WriteByte_24LC16B((unsigned char)RomAddress);
while(cnt–)
{
WriteByte_24LC16B(*Wdata++);
}
i2cStop();
}
注意:连续进行多页写操作,需要在WritePage_24LC16B函数后添加150μs以上的延迟,这段时间,24C02内部需要将数据存储到芯片内部 。
例子:
D_buffer[8] = {1,2,3,4,5,6,7,8};
WritePage_24LC16B(D_buffer,0,8);
_delay(150); //延迟150μs以及以上
WritePage_24LC16B(D_buffer,8,8);
4、页读操作
void ReadPage_24LC16B(unsigned char *Rdata,unsigned int RomAddress,unsigned char cnt)
{
unsigned char block;
ReadDeviceAddress=0B10100001;
WriteDeviceAddress=0B10100000;
block=RomAddress/256;
RomAddress=RomAddress%256;
WriteDeviceAddress=WriteDeviceAddress|(block<<1);
ReadDeviceAddress=ReadDeviceAddress|(block<<1);
i2cStart();
WriteByte_24LC16B(WriteDeviceAddress);
WriteByte_24LC16B((unsigned char)RomAddress);
i2cStart();
WriteByte_24LC16B(ReadDeviceAddress);
while(cnt>1)
{
*Rdata++ = ReadByte_24LC16B();
cnt–;
i2cAck_MCU();//发送完读地址后,需要应答一下
}
*Rdata = https://www.520longzhigu.com/shenghuo/ReadByte_24LC16B();
i2cNoAck_MCU();//读取最后一个字节需要非应答
i2cStop();
_delay(2000);


以上关于本文的内容,仅作参考!温馨提示:如遇健康、疾病相关的问题,请您及时就医或请专业人士给予相关指导!

「四川龙网」www.sichuanlong.com小编还为您精选了以下内容,希望对您有所帮助: