at24c64的读取问题……
我用w24cxx把数据写入了at24c64an中,在用单片机读取时,为什么读出的都是0xff呢?可能是什么原因?请看看程序上有问题吗?程序:
#include <reg51.h>
#define DelayTime 1000 //DelayTime用于控制IIC的速度,有些器件可能并不支持高速的IIC通信
//可以调整DelayTime来调节IIC的速度
sbit SCL=P1^4; //IIC的时钟线
sbit SDA=P1^5; //IIC的数据线
//函数声明
void IIC_Start();
void IIC_Stop();
void IIC_Ack();
void IIC_NAck();
unsigned char IIC_Get_Ack();
unsigned char IIC_Write_Byte(unsigned char dat);
unsigned char IIC_Read_Byte();
void AT24C64_Write_Byte(unsigned int addr,unsigned char dat);
void AT24C64_Write_Page(unsigned int addr,unsigned char *pbuf);
void AT24C64_Current_Addr_Read(unsigned int length,unsigned char *pbuf);
unsigned char AT24C64_Read_Byte(unsigned int addr);
void AT24C64_Sequential_Read(unsigned int addr,unsigned int length,unsigned char *pbuf);
void delay(unsigned int time)
{
while(time--);
}
void IIC_Start()
{
SDA=1;
delay(DelayTime);
SCL=1;
delay(DelayTime);
SDA=0;
delay(DelayTime);
SCL=0;
delay(DelayTime);
}
void IIC_Stop()
{
SDA=0;
delay(DelayTime);
SCL=1;
delay(DelayTime);
SDA=1;
delay(DelayTime);
SCL=0;
delay(DelayTime);
}
void IIC_Ack()
{
SDA=0;
delay(DelayTime);
SCL=1;
delay(DelayTime);
SCL=0;
delay(DelayTime);
}
void IIC_NAck()
{
SDA=1;
delay(DelayTime);
SCL=1;
delay(DelayTime);
SCL=0;
delay(DelayTime);
}
unsigned char IIC_Get_Ack()
{
unsigned char Error;
SDA=1;
delay(DelayTime);
SCL=1;
delay(DelayTime);
Error=SDA;
delay(DelayTime);
SCL=0;
delay(DelayTime);
return Error;
}
unsigned char IIC_Write_Byte(unsigned char dat)
{
unsigned char i;
for(i=0;i<8;i++)
{
SDA=((dat<<i)&0x80);
SCL=1;
delay(DelayTime);
SCL=0;
delay(DelayTime);
}
return IIC_Get_Ack();
}
unsigned char IIC_Read_Byte()
{
unsigned char i,rbyte=0;
SDA=1;
for(i=0;i<8;i++)
{
SCL=1;
delay(DelayTime);
if(SDA) rbyte|=(0x80>>i);
SCL=0;
delay(DelayTime);
}
return rbyte;
}
void AT24C64_Write_Byte(unsigned int addr,unsigned char dat)
{
IIC_Start(); //启动IIC传输
IIC_Write_Byte(0xa0); //1010 A2 A1 A0 R/W 是AT24C64的芯片地址,0~7,即同一条IIC总线上可以挂接8个芯片;R/W是读/写选择位,0为写操作,1为读操作
IIC_Write_Byte(addr>>8);//写入地址高字节
IIC_Write_Byte(addr); //写入地址低字节
IIC_Write_Byte(dat); //写入一个字节的数据
IIC_Stop(); //IIC传输结束,AT24C64开始执行,即将这一个字节写入EEPROM中
delay(15000); //等待AT24C64操作完成,最大5ms
}
void AT24C64_Write_Page(unsigned int addr,unsigned char *pbuf)
{
unsigned char i;
IIC_Start(); //启动IIC传输
IIC_Write_Byte(0xa0); //1010 A2 A1 A0 R/W 是AT24C64的芯片地址,0~7,即同一条IIC总线上可以挂接8个芯片;R/W是读/写选择位,0为写操作,1为读操作
IIC_Write_Byte(addr>>8);//写入地址高字节
IIC_Write_Byte(addr); //写入地址低字节
for(i=0;i<32;i++)
{
IIC_Write_Byte(pbuf); //将pbuf中的32个字节写入AT24C64的数据缓冲区
}
IIC_Stop(); //IIC传输结束,AT24C64开始执行,即将这32个字节写入EEPROM中
delay(15000); //等待AT24C64操作完成,最大5ms
}
void AT24C64_Current_Addr_Read(unsigned int length,unsigned char *pbuf)
{
unsigned char i;
IIC_Start(); //启动传输,这里再次启动传输,因为下面要写入“读操作”的指令码,即0xa1
IIC_Write_Byte(0xa1); //写入0xa1,从AT24C64中读取数据
for(i=0;i<length-1;i++)
{
pbuf=IIC_Read_Byte(); //读取length-1个字节
IIC_Ack(); //前length-1个字节,每读完一个字节要向AT24C64提供ACK,即告诉其继续提供下面的数据,读取操作还未完毕
}
pbuf=IIC_Read_Byte(); //读取最后一个字节,即第length个字节
IIC_NAck(); //读取最后一个字节后,要向AT24C64提NACK,告诉其读取操作结束,不再向下读了
IIC_Stop(); //传输结束
}
unsigned char AT24C64_Read_Byte(unsigned int addr)
{
unsigned char temp;
IIC_Start(); //启动传输
IIC_Write_Byte(0xa0);
IIC_Write_Byte(addr>>8); //写入地址高字节
IIC_Write_Byte(addr); //写入地址低字节
AT24C64_Current_Addr_Read(1,&temp); //从当前地址读取一个字节,放入temp中
return temp;//返回temp的值,即读到的字节
}
void AT24C64_Sequential_Read(unsigned int addr,unsigned int length,unsigned char *pbuf)
{
IIC_Start(); //启动传输
IIC_Write_Byte(0xa0);
IIC_Write_Byte(addr>>8); //写入地址高字节
IIC_Write_Byte(addr); //写入地址低字节
AT24C64_Current_Addr_Read(length,pbuf); //从当前地址读取32个字节,放入pbuf中
} 每次操作之前都有一定时间间隔的(页读或页写算是一个操作),试着延时几十ms再操作? 我也是 的 每次读取都是0xff
页:
[1]