[求助] 51單片機配合使用AT24C128B EEPROM程式修改

fone   2015-8-17 17:43 楼主
#include
#define uchar unsigned char
sbit sda=P2^0;
sbit scl=P2^1;
uchar a;
void delay()
{ ;; }
void start()  //开始信号
{
sda=1;
delay();
scl=1;
delay();
sda=0;
delay();
}
void stop()   //停止
{
sda=0;
delay();
scl=1;
delay();
sda=1;
delay();
}
void respons()  //应答
{
uchar i;
scl=1;
delay();
while((sda==1)&&(i<250))i++;
scl=0;
delay();
}
void init()
{
sda=1;
delay();
scl=1;
delay();
}
void write_byte(uchar date)
{
uchar i,temp;
temp=date;

for(i=0;i<8;i++)
{
  temp=temp<<1;
  scl=0;
     delay();
  sda=CY;
  delay();
  scl=1;
  delay();
// scl=0;
     //   delay();
}
scl=0;
delay();
sda=1;
delay();
}
uchar read_byte()
{
uchar i,k;
scl=0;
delay();
sda=1;
delay();
for(i=0;i<8;i++)
{
  scl=1;
  delay();
  k=(k<<1)|sda;
  scl=0;
  delay();
}
return k;
}
void delay1(uchar x)
{
uchar a,b;
for(a=x;a>0;a--)
  for(b=100;b>0;b--);
}
void write_add(uchar address,uchar date)
{
start();
write_byte(0xa0);
respons();
write_byte(address);
respons();
write_byte(date);
respons();
stop();
}
uchar read_add(uchar address)
{
uchar date;
start();
write_byte(0xa0);
respons();
write_byte(address);
respons();
start();
write_byte(0xa1);
respons();
date=read_byte();
stop();
return date;
}
void main()
{
init();
write_add(23,0xaa);
delay1(100);
P1=read_add(23);
while(1);
}

以上是參考教材中的程式,使用的EEPROM是AT24C02。但現在我手里的單片機開發板使用的EEPROM是AT24C128B,不知如何根據以上程式修改?
WORD ADDRESS我可以寫到哪個位置?
這兩個EEPROM有如下差異:
AT24C02,Byte Write时只有一个WORD ADDRESS,而AT24C128B有两个WORD ADDRESS
  • AT24C128B

回复评论 (5)

2推荐 weizhongc 

引用: fone 发表于 2015-8-18 11:36
void write_add(uint address,uchar date)
{
        start();
        write_byte(0xa0);
        respons();
        write_byt ...

恩  对的
点赞  2015-8-18 15:09
哪位大俠能幫幫忙,謝謝啦!
点赞  2015-8-18 08:35
void write_add(uint address,uchar date)
{
start();
write_byte(0xa0);
respons();
write_byte((address>>8)&0xff);
respons();
write_byte((address)&0xff);
respons();
write_byte(date);
respons();
stop();
}
点赞  2015-8-18 08:54
引用: weizhongc 发表于 2015-8-18 08:54
void write_add(uint address,uchar date)
{
start();
write_byte(0xa0);
respons();
write_byte((add ...

void write_add(uint address,uchar date)
{
        start();
        write_byte(0xa0);
        respons();
        write_byte(address/256);
        respons();
        write_byte(address%256);
        respons();
        write_byte(date);
        respons();
        stop();
}

uchar read_add(uint address)
{
        uchar date;
        start();
        write_byte(0xa0);
        respons();
        write_byte(address/256);
        respons();
        write_byte(address%256);
        respons();
        start();
        write_byte(0xa1);
        respons();
        date=read_byte();
        stop();
        return date;
}
我是這樣寫的,不知道對不對?
点赞  2015-8-18 11:36
感謝,已調試OK。
点赞  2015-8-19 10:18
电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 京公网安备 11010802033920号
    写回复