(51单片机)AT24C02一直无法写入求解
时间:10-02
整理:3721RD
点击:
我想把中断产生的秒数实时写进AT24C02以实现掉电存储,程序如下,烧录后发现,复位之后数码管依然从初始数据开始跑,试了很多次都找不到问题在哪....求高手帮忙看下
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
unsigned char code aa[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77};
uchar sec,num=0;
bit write=0;
sbit dula=P2^6;
sbit wela=P2^7;
sbit clock=P2^1;
sbit date=P2^0;
void delay()
{;;}
void delayms(int num)
{
int x,y;
for(x=num;x>0;x--)
for(y=112;y>0;y--);
}
void start()
{
date=1;
delay();
clock=1;
delay();
date=0;
delay();
}
void stop()
{
date=0;
delay();
clock=1;
delay();
date=1;
delay();
}
void init()
{
clock=1;
delay();
date=1;
delay();
}
void response()
{
char i;
clock=1;
delay();
while((date==1)&&(i<255))
i++;
clock=0;
delay();
}
void write_byte(unsigned char date)
{
uchar i,temp;
temp=date;
for(i=0;i<8;i++)
{
temp=temp<<1;
clock=0;
delay();
date=CY;
delay();
clock=1;
delay();
}
clock=0;
delay();
date=1;
delay();
}
uchar read_byte()
{
char k=0,i;
clock=0;
delay();
date=1;
delay();
for(i=0;i<8;i++)
{
clock=1;
delay();
k=(k<<1)|date;
clock=0;
delay();
}
return k;
}
void write_add(uchar address,uchar date)
{
EA=0;
start();
write_byte(0xa0);
response();
write_byte(address);
response();
write_byte(date);
response();
stop();
EA=1;
delayms(5);
}
uchar read_add(uchar address)
{
uchar date;
start();
write_byte(0xa0);
response();
write_byte(address);
response();
start();
write_byte(0xa1);
response();
date=read_byte();
stop();
return date;
}
void display(int a,int b)
{
dula=1;
P0=aa[a];
dula=0;
P0=0xff;
wela=1;
P0=0xfe;
wela=0;
delayms(2);
dula=1;
P0=aa[b];
dula=0;
P0=0xff;
wela=1;
P0=0xfd;
wela=0;
delayms(2);
}
void main()
{
unsigned char i;
TMOD=0x01;
EA=1;
ET0=1;
TR0=1;
init();
sec=read_add(2);
TH0=(65536-45872)/256;
TL0=(65536-45872)%256;
TR0=1;
while(1)
{
i=10;
while(i--) //避免数码管闪烁
{
display(sec/10,sec%10);
}
if(write==1)
{
write=0;
write_add(2,sec);
}
}
}
void time0() interrupt 1 using 0
{
TH0=(65536-45872)/256;
TL0=(65536-45872)%256;
num++;
if(num==20)
{
num=0;
sec++;
write=1;
if(sec>=100)
sec=0;
}
}
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
unsigned char code aa[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77};
uchar sec,num=0;
bit write=0;
sbit dula=P2^6;
sbit wela=P2^7;
sbit clock=P2^1;
sbit date=P2^0;
void delay()
{;;}
void delayms(int num)
{
int x,y;
for(x=num;x>0;x--)
for(y=112;y>0;y--);
}
void start()
{
date=1;
delay();
clock=1;
delay();
date=0;
delay();
}
void stop()
{
date=0;
delay();
clock=1;
delay();
date=1;
delay();
}
void init()
{
clock=1;
delay();
date=1;
delay();
}
void response()
{
char i;
clock=1;
delay();
while((date==1)&&(i<255))
i++;
clock=0;
delay();
}
void write_byte(unsigned char date)
{
uchar i,temp;
temp=date;
for(i=0;i<8;i++)
{
temp=temp<<1;
clock=0;
delay();
date=CY;
delay();
clock=1;
delay();
}
clock=0;
delay();
date=1;
delay();
}
uchar read_byte()
{
char k=0,i;
clock=0;
delay();
date=1;
delay();
for(i=0;i<8;i++)
{
clock=1;
delay();
k=(k<<1)|date;
clock=0;
delay();
}
return k;
}
void write_add(uchar address,uchar date)
{
EA=0;
start();
write_byte(0xa0);
response();
write_byte(address);
response();
write_byte(date);
response();
stop();
EA=1;
delayms(5);
}
uchar read_add(uchar address)
{
uchar date;
start();
write_byte(0xa0);
response();
write_byte(address);
response();
start();
write_byte(0xa1);
response();
date=read_byte();
stop();
return date;
}
void display(int a,int b)
{
dula=1;
P0=aa[a];
dula=0;
P0=0xff;
wela=1;
P0=0xfe;
wela=0;
delayms(2);
dula=1;
P0=aa[b];
dula=0;
P0=0xff;
wela=1;
P0=0xfd;
wela=0;
delayms(2);
}
void main()
{
unsigned char i;
TMOD=0x01;
EA=1;
ET0=1;
TR0=1;
init();
sec=read_add(2);
TH0=(65536-45872)/256;
TL0=(65536-45872)%256;
TR0=1;
while(1)
{
i=10;
while(i--) //避免数码管闪烁
{
display(sec/10,sec%10);
}
if(write==1)
{
write=0;
write_add(2,sec);
}
}
}
void time0() interrupt 1 using 0
{
TH0=(65536-45872)/256;
TL0=(65536-45872)%256;
num++;
if(num==20)
{
num=0;
sec++;
write=1;
if(sec>=100)
sec=0;
}
}
能不能发下烧录软件的显示情况,不一定是程序的问题