我在打万年历,给时间进行赋值,加入掉电保护后,虽然能够保护,但显示的时间和我赋的值完全不一样,哪位大神能帮我看看?
#include <reg52.h>
#define uchar unsigned char
#define uint unsigned int
bit write=0; //д24C02??????
sbit sda=P2^0;
sbit scl=P2^1;
sbit key1=P3^0;
sbit key2=P3^1;
sbit key3=P3^2;
sbit key4=P3^3;
sbit BEEP=P3^7;
sbit beep=P3^7;
uint code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
uint code table1[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x80};
uint num,num0,miao=30,fen=28,shi=11,fen1=30,shi1=16,s1,s2,m1,m2,m3,m4,h1,h2,h3,h4,r=23,y=2,n=2019,z,a,b,c,d,e,X,nian,yue,ri;
uchar i,j;
void shijian(); //???
void riqi(); //????
void keyscan(); //????
void xingqi(); //????
// void naozhong();
//????
void delayms(uint xms)//????????
{
uint i,j;
for(i=xms;i>0;i--)
for(j=248;j>0;j--);
}
void shijian() //???????????
{
s2=miao/10;
s1=miao%10;
m2=fen/10;
m1=fen%10;
h2=shi/10;
h1=shi%10;
P2=0x00;
P0=table[s1];
delayms(1);
P0=0X0;
P2=0x04;
P0=table[s2];
delayms(1);
P0=0X0;
P2=0x08;
P0=0x40;
delayms(1);
P0=0X0;
P2=0x0c;
P0=table[m1];
delayms(1);
P0=0X0;
P2=0x10;
P0=table[m2];
delayms(1);
P0=0X0;
P2=0x14;
P0=0x40;
delayms(1);
P0=0X0;
P2=0x18;
P0=table[h1];
delayms(1);
P0=0X0;
P2=0x1c;
P0=table[h2];
delayms(1);
P0=0X0;
}
void riqi() //????????????
{
P2=0x00;
P0=table1[r%10];
delayms(1);
P0=0X0;
P2=0x04;
P0=table1[r/10];
delayms(1);
P0=0X0;
P2=0x08;
P0=0x80;
delayms(1);
P0=0X0;
P2=0x08;
P0=table1[y%10];
delayms(1);
P0=0X0;
P2=0x0c;
P0=table1[y/10];
delayms(1);
P0=0X0;
P2=0x10;
P0=0x80;
delayms(1);
P0=0X0;
P2=0x10;
P0=table1[n%10];
delayms(1);
P0=0X0;
P2=0x14;
P0=table1[n%1000%100/10];
delayms(1);
P0=0X0;
P2=0x18;
P0=table1[n%1000/100];
delayms(1);
P0=0X0;
P2=0x1c;
P0=table1[n/1000];
delayms(1);
P0=0X0;
}
void xingqi() //????????????
{
int yue=y ;
int nian=n ;
int ri=r;
if(yue==2||yue==1)
{
nian-=1;
yue+=12;
}
num0=(ri+2*yue+3*(yue+1)/5+nian+nian/100+yue/400)%7-1;
switch(num0)
{
case 0:P0=table[2];P2=0x04;break;
case 1:P0=table[3];P2=0x04;break;
case 2:P0=table[4];P2=0x04;break;
case 3:P0=table[5];P2=0x04;break;
case 4:P0=table[6];P2=0x04;break;
case 5:P0=table[7];P2=0x04;break;
case 6:P0=table[8];P2=0x04;break;
}
}
void delay0(){;;}
void start() //??????
{
sda=1;
delay0();
scl=1;
delay0();
sda=0;
delay0();
}
void stop() //??
{
sda=0;
delay0();
scl=1;
delay0();
sda=1;
delay0();
}
void respons() //???
{
uchar i;
scl=1;
delay0();
while((sda==1)&&(i<250))i++;
scl=0;
delay0();
}
void init()//IIC?????????
{
sda=1;
delay0();
scl=1;
delay0();
}
void write_byte(uchar date)//д?????????
{
uchar i,temp;
temp=date;
for(i=0;i<8;i++)
{
temp=temp<<1;
scl=0;
delay0();
sda=CY;
delay0();
scl=1;
delay0();
}
scl=0;
delay0();
sda=1;
delay0();
}
uchar read_byte()//???????????
{
uchar i,k;
scl=0;
delay0();
sda=1;
delay0();
for(i=0;i<8;i++)
{
scl=1;
delay0();
k=(k<<1)|sda;
scl=0;
delay0();
}
return k;
}
void write_add(uchar address,uchar date)//??????д??????
{
start();
write_byte(0xa0);
respons();
write_byte(address);
respons();
write_byte(date);
respons();
stop();
}
char 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 naozhong()
{
if(miao==0)
{
if(fen==0)
{
if(shi==0||1||2||3||4||5||6||7||8||9||10||11||12||13||14||15||16||17||18||19||20||21||22||23||24)
{
beep=~beep;
}
}
if(miao==0)
{
if(fen==1)
{
beep=0;
}
}
}
} */
void keyscan() //????????????
{
/////////----------------------????------??????--------------------////////////////////////////////////////////////////////
if(key1==0)
{
delayms(10);
if(key1==0) //???K1??????
{
z=1; //???
while(!key1) //??????
{
riqi();
}
while(z) //?????????????????????
{
riqi();
/////////////---------??????????????--------/////////
if(key1==0)
{
delayms(10);
if(key1==0)
{
z=1;
while(!key1)
{
xingqi();
}
while(z) //?л?????
{
xingqi();
if(key1==0)
{
delayms(10);
if(key1==0)
{
z=0;
while(!key1)
{
xingqi();
}
}
}
}
}
}
/////////--???????---///////////
if(key1==0) //??????K1????????????
{
delayms(10);
if(key1==0)
{
z=0;
while(!key1) //??????
{
riqi();
}
}
}
//////////////-----------????------????------//////////
if(key1==0)
{
delayms(10);
if(key1==0)
{
X=1;
while(!key1);
{
xingqi();
}
while(X)
{
xingqi();
if(key1==0)
{
delayms(10);
if(key1==0)
{
X=0;
while(!key1);
{
xingqi();
}
}
}
}
}
}
///////////////////////-------------------????-----??----------------////////////////////////////////////
if(key2==0) //????????K2?????????л?
{
delayms(10); //????
if(key2==0) //???K2??????
{
c=1; //???
while(!key2); //??????
{
riqi();
}
while(c) //?????????????????????
{
riqi();
if(key1==0) //??????K1???????????
{
delayms(10);
if(key1==0)
{
c=0;
while(!key1); //??????
{
riqi();
}
}
}
if(key3==0) //????key3????
{
delayms(10);
if(key3==0) //??Ρ?????
{
r++;
while(!key3)
{ //??????????
riqi();
}
}
}
if(key4==0) //????key4????
{
delayms(10);
if(key4==0)
{
r--;
if(r==0)
{
r=30; //???????ж?28??29??30??31
}
while(!key4)
{
riqi();
}
}
}
////////////////////----------????-----??--------//////////////////////////
if(key2==0) //????????K2?????????л?
{
delayms(10); //????
if(key2==0) //???K2??????
{
d=1; //???
while(!key2); //??????
{
riqi();
}
while(d) //?????????????????????
{
riqi();
if(key1==0) //??????K1??????μ???
{
delayms(10);
if(key1==0)
{
d=0;
while(!key1); //??????
{
riqi();
}
}
}
if(key3==0) //????key3????
{
delayms(10);
if(key3==0) //??Ρ?????
{
y++;
while(!key3)
{ //??????????
riqi();
}
}
}
if(key4==0) //????key4????
{
delayms(10);
if(key4==0)
{
if(y==1)
{
y=12;
}
else
y--;
while(!key4)
{
riqi();
}
}
}
//////////////////----------????-------??-------//////////////////////////
if(key2==0) //????????K2?????????л?
{
delayms(10); //????
if(key2==0) //???K2??????
{
e=1; //???
while(!key2); //??????
{
riqi();
}
while(e) //?????????????????????
{
riqi();
if(key1==0) //??????K1??????????
{
delayms(10);
if(key1==0)
{
e=0;
while(!key1); //??????
{
riqi();
}
}
}
if(key3==0) //????key3????
{
delayms(10);
if(key3==0) //??Ρ?????
{
n++;
while(!key3)
{ //??????????
riqi();
}
}
}
if(key4==0) //????key4????
{
delayms(10);
if(key4==0)
{
n--;
while(!key4)
{
riqi();
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
///////////////-------------???仯-------????---------------------////////////////////////////////////////////////////
if(key3==0) //????key3????
{
delayms(10);
if(key3==0) //??Ρ?????
{
miao++;
write_add(1,miao);
while(!key3)
{ //??????????
shijian();
}
}
}
if(key4==0) //????key4??????
{
delayms(10);
if(key4==0)
{
miao--;
write_add(1,miao);
if(miao==-1)
{
miao=59;
}
while(!key4)
{
shijian();
}
}
}
///////////////-------------???仯-----????-----------------------////////////////////////////
if(key2==0) //????????K2??????????л?
{
delayms(10); //????
if(key2==0) //???K2??????
{
a=1; //???
while(!key2); //??????
{
shijian();
}
while(a) //???????????????????
{
shijian();
if(key1==0) //??????K1???????????
{
delayms(10);
if(key1==0)
{
a=0;
while(!key1); //??????
{
shijian();
}
}
}
if(key3==0) //????key3????
{
delayms(10);
if(key3==0) //??Ρ?????
{
fen++;
write_add(2,fen);
while(!key3)
{ //??????????
shijian();
}
}
}
if(key4==0) //????key4??????
{
delayms(10);
if(key4==0)
{
fen--;
write_add(2,fen);
if(fen==-1)
{
fen=59;
}
while(!key4)
{
shijian();
}
}
}
///////////////////-----------???仯-------С?-------------///////////////////////////
if(key2==0) //????????K2??????????л?
{
delayms(10); //????
if(key2==0) //???K2??????
{
b=1; //???
while(!key2) //??????
{
shijian();
}
while(b) //???????????????????
{
shijian();
if(key1==0) //??????K1???????????
{
delayms(10);
if(key1==0)
{
b=0;
while(!key1) //??????
{
shijian();
}
}
}
if(key3==0) //????key3????
{
delayms(10);
if(key3==0) //??Ρ?????
{
shi++;
write_add(3,shi);
while(!key3)
{ //??????????
shijian();
}
}
}
if(key4==0) //????key4?????
{
delayms(10);
if(key4==0)
{
shi--;
write_add(3,shi);
if(shi==-1)
{
shi=23;
}
while(!key4)
{
shijian();
}
}
}
}
}
}
}
}
}
}
void main() //??????
{
init();
miao=read_add(1);
if(miao>60)
miao=0;
fen=read_add(2);
shi=read_add(3);
TMOD=0x01; //????????0????????1
TH0=(65536-45872)/256;
TL0=(65536-45872)%256;
EA=1; //???ж??????
ET0=1; //????????ж?
TR0=1; //????????
while(1)
{
xingqi();
shijian();
keyscan();
//naozhong();
/* if(shi1==shi,fen1=fen)
{
BEEP=~BEEP;
if(fen1=31)
{
BEEP=0;
}
}*/
}
}
void T0_time()interrupt 1 //??????ж?
{
TH0=(65536-45872)/256;
TL0=(65536-45872)%256;
num++;
if(num==20) //1????
{
num=0;
miao++;
write_add(1,miao);
}
if(miao==60) //????60s??????1min????
{
miao=0; //??????0
write_add(1,miao);
fen++; //??????
write_add(2,fen);
}
if(fen==60) //????60min??????1h????
{
fen=0; //??????0
write_add(2,fen);
shi++; //??????
write_add(3,shi);
}
if(shi==24) //????24h???????????
{
shi=0; //?????0
write_add(3,shi);
r++; //??????
}
if(y==1||y==3||y==5||y==7||y==8||y==10||y==12)
{
if(r==32) //????32?????????μ???
{
r=1; //???????1
y++; //??????
}
}
if(y==13) // ???12?
{
y=1; //???μ???1
n++; //????
}
if(y==4||y==6||y==9||y==11) //???y?4,6,9,11
{
if(r==31) //????31?????????μ???
{
r=1; //???????1
y++; //??????
}
}
if(n%4==0||n%400==0||n%100!=0) //????????4????
{
if(y==2) //???μ???2 ????29??
{
if(r==30) //????29?
{
r=1; //???????1
y++; //??????
}
}
}
if(n%4!=0) //????????4????
{
if(y==2) //???μ???2 ????28??
{
if(r==29) //????28?
{
r=1; //???????1
y++; //??????
}
}
}
}
|