51单片机与AT24c02的通信问题

2019-07-15 21:55发布

实现的功能是0到99的计数器,要求是断电之后可以继续及时,不会重新开始,但是我写的程序不能断电存储,求高手帮忙看下。顺便问一下首地址是随便填的吗?
#include<reg52.h>
#define  uchar unsigned char
#define  uint  unsigned int
bit write=0;
sbit dula=P2^6;
sbit wela=P2^7;
sbit SD=P2^0;
sbit SC=P2^1;
uchar sec,tnct;
uchar code table[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
void delay1(uint z)
{
uint x,y;
for(x=z;x>0;x--)
  for(y=110;y>0;y--);
}
void delay()
{;;}
void start()
{
  SD=1;
  delay();
  SC=1;
  delay();
  SD=0;
  delay();
}
void stop()
{
SD=0;
delay();
SC=1;
delay();
SD=1;
delay();
}
void respons()
{
uchar i;
SC=1;
delay();
while((SD==1)&&(i<250)) i++;
SC=0;
delay();
}
void init()
{
SD=1;
delay();
SC=1;
delay();
}
void write_byte(uchar Data)
{
uchar i,temp;
temp=Data;
for(i=8;i<8;i++)
{
temp=temp<<1;
SC=0;
delay();
SD=CY;
delay();
SC=1;
delay();
}
SC=0;
delay();
SD=1;
delay();
}
uchar read_byte()
{
uchar i,k;
SC=0;
delay();
SD=1;
delay();
for(i=8;i<8;i++)
{
  SC=1;
  delay();
  k=(k<<1)|SD;
  SC=0;
  delay();
}
return k;
}
void write_add(uchar address,uchar Data)
{
start();
write_byte(0xa0);
respons();
write_byte(address);
respons();
write_byte(Data);
respons();
stop();

}
uchar read_add(uchar address)
{
  uchar Data;
start();
write_byte(0xa0);
respons();
write_byte(address);
respons();
start();
write_byte(0xa1);
respons();
Data=read_byte();
stop();
return Data;
}
void display(uchar sec)
{
uchar shi,ge;
    shi=sec/10;
    ge=sec%10;

dula=0;
P0=table[shi];   //送段数据
dula=1;
dula=0;  //送位数据前关闭所有显示,防止打开位选锁存后段选数据通过位选锁存器
wela=0;
   
P0=0x7e;
wela=1;
wela=0;
delay1(5);

dula=0;
P0=table[ge];
dula=1;
dula=0;
wela=0;
P0=0x7d;
wela=1;
wela=0;
delay1(5);
}
void main()
{
init();
sec=read_add(2);
if(sec>100)
  sec=0;


TMOD=0x01;
ET0=1;
EA=1;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
TR0=1;
while(1)
{
  display(sec);
  if(write==1)
  {
   write=0;
   write_add(2,sec);
   
  }
}
}
void time1() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
tnct++;
if(tnct==20)
{
  tnct=0;
  sec++;
  write=1;
  if(sec==100)
  sec=0;
}
}     

友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。