WriteDeviceAddress=0B10100000;
block=RomAddress/256;
RomAddress=RomAddress%256;
WriteDeviceAddress=WriteDeviceAddress|(block<<1);
i2cStart();
WriteByte_24LC16B(WriteDeviceAddress);
WriteByte_24LC16B((unsigned char)RomAddress);
WriteByte_24LC16B(Wdata);
i2cStop();
_delay(5500);
}
2、字节读操作
unsigned char ReadByte_24LC16B()
{
unsigned char bit_count,rbyte=0;
SDA=1;
SDA_CTRL=1;
_delay(10);
for(bit_count=8;bit_count!=0;bit_count–)
{
rbyte=rbyte<<1;
_delay(2);
SCL=1;
rbyte=rbyte|((unsigned char)(SDA));
_delay(2);
SCL=0;
_delay(2);
}
SDA_CTRL=0;
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.jinnalai.com/fenxiang/ReadByte_24LC16B();
i2cNoAck_MCU();//读取最后一个字节需要非应答
i2cStop();
_delay(2000);
【c51单片机编程实例讲解 c51单片机编程入门】
推荐阅读
- 编程课对孩子有用吗 编程课适合多大孩子学
- 怎么编程做一个小游戏 如何设计游戏
- 无基础学编程有多难 如何编程
- 入门学习编程 想学习计算机编程,如何入门?
- 免费编程软件 c语言编程软件哪个好
- 单片机怎么编程点亮Led灯 单片机编程步骤模板
- 程序员如何提高代码能力 如何提高编程能力
- python用什么软件编程好 常用python编程软件推荐
- arduino模拟器软件 arduino编程语言入门知识
- c语言开发单片机的优点 单片机c语言必背代码