|
发表于 2010-11-7 19:37:56
|
显示全部楼层
这个是我操作AT24C512的程序,楼主可以参考一下,操作的参数都是页地址,然后在函数内部换成对应的字节地址,这样就能完整的读写一个页。
void PageWrite(unsigned int Page_add, unsigned char *buf,unsigned char eep_add)
{
unsigned char i;
unsigned int Byte_add;
unsigned char Byte_add_H,Byte_add_L;
bit EA_Save = EA;
EA = 0;EA = 0;
Byte_add = Page_add * 32;
Byte_add_H = (Byte_add>>8)&0x00ff ;
Byte_add_L = Byte_add&0x00ff;
I2C_start();
WriteByte(eep_add); //发送器件地址
WriteByte(Byte_add_H); //发送地址高字节
WriteByte(Byte_add_L); //发送地址低字节
for(i = 0; i < 32; i++) //每个数据块32字节
{
WriteByte(buf); //发送计数值
}
I2C_stop(); //停止信号
delay_ms(5); //确保写入时间
EA = EA_Save;EA = EA_Save;
}
/**********************************************************
按页读子函数 数据写入eep_buf缓存区
**********************************************************/
void PageRead(unsigned int Page_add,unsigned char eep_add)
{
unsigned char i;
unsigned char eep_addr;
unsigned int Byte_add;
unsigned char Byte_add_H,Byte_add_L;
bit EA_Save = EA;
EA = 0;EA = 0;
Byte_add = Page_add * 32;
Byte_add_H = (Byte_add>>8)&0x00ff ;
Byte_add_L = Byte_add&0x00ff;
eep_addr = eep_add + 1;
I2C_start();
WriteByte(eep_add); //发送器件地址
WriteByte(Byte_add_H); //发送地址高字节
WriteByte(Byte_add_L); //发送地址低字节
I2C_start();
WriteByte(eep_addr); //发送器件地址(读)
for(i = 0; i < 31; i++)
{
eep_buf = ReadByte(); //接收数据
I2C_Ack();
}
eep_buf[31] = ReadByte();
I2C_NAck();
I2C_stop(); //停止信号
EA = EA_Save;EA = EA_Save;
} |
|