搜索
bottom↓
回复: 2

at24c64的读取问题……

[复制链接]

出0入0汤圆

发表于 2011-2-10 18:41:06 | 显示全部楼层 |阅读模式
我用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 [A2:A0]是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 [A2:A0]是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中   
}

阿莫论坛20周年了!感谢大家的支持与爱护!!

曾经有一段真挚的爱情摆在我的面前,我没有珍惜,现在想起来,还好我没有珍惜……

出0入0汤圆

发表于 2011-2-11 19:15:30 | 显示全部楼层
每次操作之前都有一定时间间隔的(页读或页写算是一个操作),试着延时几十ms再操作?

出0入0汤圆

发表于 2012-11-9 14:29:16 | 显示全部楼层
我也是 的 每次读取都是0xff
回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片。注意:要连续压缩2次才能满足要求!!】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|Archiver|amobbs.com 阿莫电子技术论坛 ( 粤ICP备2022115958号, 版权所有:东莞阿莫电子贸易商行 创办于2004年 (公安交互式论坛备案:44190002001997 ) )

GMT+8, 2024-8-26 15:13

© Since 2004 www.amobbs.com, 原www.ourdev.cn, 原www.ouravr.com

快速回复 返回顶部 返回列表