中断服务程序中得把当时的时间值放在EEPROM,然后再主函数的初始化程序上读EEPROM放时间的地址用显示代码刷一遍时间!
呵呵,点“结贴”……建议一下LZ(52内置了EEPROM,可以设置寄存器来使用它)不需要买个外围芯片,比较经济,学得也多
我把正确答案给大家共享一下,谢谢huxinhui的指教。
#include
#define uchar unsigned char
char shu[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff};
char wei[4]={0xfe,0xfd,0xfb,0xf7};
char dis[4]={0x00,0x00,0x00,0x00};
char timedata[2]={0x00,0x00};
sbit sda=P3^7;
sbit scl=P3^6;
char ms50=0x00;
void delay1ms(int t)
{
int i,j;
for(i=0;i
for(j=0;j<100;j++);
}
scan()
{
char k;
dis[1]=timedata[0]%10;
dis[0]=timedata[0]/10;
// dis[2]=timedata[1]%10;
// dis[3]=timedata[1]/10;
for(k=0;k<2;k++)
{
P0=shu[dis[k]];
P2=wei[k];
delay1ms(1);
// P2=0xfd;
}
}
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 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();
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;
}
uchar shuju()
{
uchar Time;
Time=timedata[0]+timedata[1]*100;
return Time;
}
void time(void) interrupt 1
{
ET0=0;TR0=0;TH0=0x3c;TL0=0xb0;TR0=1;ms50++;
if(ms50==20)
{
ms50=0x00;timedata[0]++;
if(timedata[0]==100)
{
timedata[0]=0;timedata[1]++;
if(timedata[1]==100)
{
timedata[1]=0;
}
}
write_add(12,shuju());
}
ET0=1;
}
void init()
{
uchar x;
sda=1;
delay();
scl=1;
delay();
x=read_add(12);
timedata[0]=x%100;
timedata[1]=x/100;
scan();
delay1(1);
}
main()
{
init();
shuju();
TMOD=0x01;
TH0=0x3c;
TL0=0xb0;
TR0=1;
ET0=1;
EA=1;
while(1)
{scan();}
}