#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit dula=P2^6;
sbit wela=P2^7;
sbit lcden=P3^4;
sbit lcdrs=P3^5;
sbit key1=P1^2;
sbit key2=P1^1;
sbit key3=P1^0;
uchar shi=0,fen=0,miao=0,num,jie,cnt;
uint year=2000,month=1,day=1,weekday;
uchar table_num[]="0123456789";
uchar table_week[][4]={"SUN","MON","TUE","WED","THU","FRI","SAT"};
void delay(uint xms)
{
uint i,j;
for(i=xms;i>0;i--)
for(j=114;j>0;j--);
}
void write_com(uchar com)
{
lcdrs=0;
P0=com;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void write_data(uchar date)
{
lcdrs=1;
P0=date;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void init()
{
TMOD=0x11;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
TR0=1;
ET0=1;
EA=1;
dula=0;
wela=0;
lcden=0;
write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x01);
write_com(0x80);
}
uchar run(uint y)
{
if((!(y%4)&&(y%100))||!(y%400))
return 1;
else
return 0;
}
void write_time()
{
write_com(0x80+0x40+0x04);
write_data(table_num[shi/10]);write_data(table_num[shi%10]);write_data(':');
write_data(table_num[fen/10]);write_data(table_num[fen%10]);write_data(':');
write_data(table_num[miao/10]);write_data(table_num[miao%10]);
}
void write_date()
{
write_com(0x80+0x01);
write_data(table_num[year/1000]);write_data(table_num[year%1000/100]);write_data(table_num[year%1000%100/10]);write_data(table_num[year%10]);
write_data('-');write_data(table_num[month/10]);write_data(table_num[month%10]);write_data('-');
write_data(table_num[day/10]);write_data(table_num[day%10]);
write_data(' ');write_data(' ');
write_data(table_week[weekday][0]);write_data(table_week[weekday][1]);write_data(table_week[weekday][2]);
}
void time_cnt()
{
if(num>=20)
{
num=0;
miao++;
if(miao>=60)
{
miao=0;
fen++;
if(fen>=60)
{
fen=0;
shi++;
if(shi>=24)
{
shi=0;
day++;
weekday++;
if(weekday>6)
weekday=0;
switch(month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
jie=31;
break;
case 4:
case 6:
case 9:
case 11:
jie=30;
break;
case 2:
if(run(year))
jie=29;
else
jie=28;
break;
}
if(day>jie)
{
day=1;
month++;
if(month>12)
{
month=1;
year++;
}
}
}
}
}
write_time();
write_date();
}
}
void timer0() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
num++;
time_cnt();
}
void adjust()
{
if(key1==0)
{
delay(5);
if(key1==0)
{
cnt++;
switch(cnt)
{
case 1:
TR0=0;
write_com(0x80+0x03);
write_com(0x0f);
break;
case 2:
write_com(0x80+0x07);
break;
case 3:
write_com(0x80+0x0a);
break;
case 4:
write_com(0x80+0x0e);
break;
case 5:
write_com(0x80+0x40+0x05);
break;
case 6:
write_com(0x80+0x40+0x08);
break;
case 7:
write_com(0x80+0x40+0xb);
break;
case 8:
write_com(0x0c);
TR0=1;
cnt=0;
break;
}
}
while(!key1) ;
}
if(key2==0)
{
delay(5);
if(key2==0)
{
switch(cnt)
{
case 1:
year++;
write_com(0x0c);
write_date();
write_com(0x80+0x03);
write_com(0x0f);
break;
case 2:
month++;
if(month>=13)
month=1;
write_com(0x0c);
write_date();
write_com(0x80+0x07);
write_com(0x0f);
break;
case 3:
day++;
switch(month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
jie=31;
break;
case 4:
case 6:
case 9:
case 11:
jie=30;
break;
case 2:
if(run(year))
jie=29;
else
jie=28;
break;
}
if(day>jie)
day=1;
write_com(0x0c);
write_date();
write_com(0x80+0x0a);
write_com(0x0f);
break;
case 4:
weekday++;
if(weekday>6)
weekday=0;
write_com(0x0c);
write_date();
write_com(0x80+0x0e);
write_com(0x0f);
break;
case 5:
shi++;
if(shi>=24)
shi=0;
write_com(0x0c);
write_time();
write_com(0x80+0x40+0x05);
write_com(0x0f);
break;
case 6:
fen++;
if(fen>=60)
fen=0;
write_com(0x0c);
write_time();
write_com(0x80+0x40+0x08);
write_com(0x0f);
break;
case 7:
miao++;
if(miao>=60)
miao=0;
write_com(0x0c);
write_time();
write_com(0x80+0x40+0xb);
write_com(0x0f);
break;
case 0:;
}
while(!key2) ;
}
}
if(key3==0)
{
delay(5);
if(key3==0)
{
switch(cnt)
{
case 1:
year--;
write_com(0x0c);
write_date();
write_com(0x80+0x03);
write_com(0x0f);
break;
case 2:
month--;
if(month<1)
month=12;
write_com(0x0c);
write_date();
write_com(0x80+0x07);
write_com(0x0f);
break;
case 3:
day--;
switch(month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
jie=31;
break;
case 4:
case 6:
case 9:
case 11:
jie=30;
break;
case 2:
if(run(year))
jie=29;
else
jie=28;
break;
}
if(day<1)
day=jie;
write_com(0x0c);
write_date();
write_com(0x80+0x0a);
write_com(0x0f);
break;
case 4:
weekday--;
if(weekday==-1)
{weekday=6;}
write_com(0x0c);
write_date();
write_com(0x80+0x0e);
write_com(0x0f);
break;
case 5:
shi--;
if(shi==-1)
{shi=23;}
write_com(0x0c);
write_time();
write_com(0x80+0x40+0x05);
write_com(0x0f);
break;
case 6:
fen--;
if(fen==-1)
{fen=59;}
write_com(0x0c);
write_time();
write_com(0x80+0x40+0x08);
write_com(0x0f);
break;
case 7:
miao--;
if(miao==-1)
{miao=59;}
write_com(0x0c);
write_time();
write_com(0x80+0x40+0xb);
write_com(0x0f);
break;
case 0:;
}
while(!key3) ;
}
}
}
void main()
{
init();
while(1)
{
adjust();
}
}
- 1
- 2
前往页