51单片机带闰年补偿的一个时钟程序
#include
#include
#define uchar unsigned char
#define uint unsigned int
uchar S1num;
sbit RS=P1^0;
sbit RW=P1^1;
sbit EN=P1^2;
sbit GND=P2^4;
sbit K2=P2^3;
sbit K4=P2^2;
sbit K6=P2^1;
uchar x,num,shi,fen,miao,day,mon,year,week,count;
void display();
void write_com(uchar com)
{
RS=0;
EN=0;
P0=com;
delay(5);
//RW=0;
EN=1;
delay(5);
EN=0;
}
void write_dat(uchar dat)
{
RS=1;
EN=0;
P0=dat;
delay(5);
//RW=0;
EN=1;
delay(5);
EN=0;
}
void timer0()interrupt 1
{
TH0=(65536-45872)/256;//45872这个值是11.0592MHz产生一次中断,最接近50Ms,如果是12MHz请用50000
TL0=(65536-45872)%256;
count++;
}
write_sfm(uchar add,uchar date)
{
uchar shi,ge;
write_com(0x80+0x40+add);
shi=date/10;
ge=date%10;
write_dat(0x30+shi);
write_dat(0x30+ge);
}
write_ymd(uchar add,uchar date)
{
uchar shi,ge;
write_com(0x80+add);
shi=date/10;
ge=date%10;
write_dat(0x30+shi);
write_dat(0x30+ge);
}
write_week(week)
{
switch(week)
{
case 1:write_dat(M);
write_dat(O);
write_dat(N);
break;
case 2:write_dat(T);
write_dat(U);
write_dat(E);
break;
case 3:write_dat(W);
write_dat(E);
write_dat(N);
break;
case 4:write_dat(T);
write_dat(H);
write_dat(U);
break;
case 5:write_dat(F);
write_dat(R);
write_dat(I);
break;
case 6:write_dat(S);
write_dat(A);
write_dat(T);
break;
case 7:write_dat(S);
write_dat(U);
write_dat(N);
break;
}
}
void init()
{
EN=0;
RW=0;
write_com(0x38);//显示模式
delay(5);
write_com(0x0c);//开显示
write_com(0x06);//光标自加
write_com(0x80);//指针左一
write_com(0x01);//清屏
TMOD=0x01;//定时器0的工作方式1
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
EA=1;//开中断
ET0=1;
TR0=1;//开定时器
display();
}
void display()
{
write_sfm(10,0);
write_com(0x80+0x40+11);
write_com(0x80+0x40+9);
write_dat(:);
write_sfm(7,0);
write_com(0x80+0x40+8);
write_com(0x80+0x40+6);
write_dat(:);
write_sfm(4,0);
write_com(0x80+0x40+5);
write_ymd(10,29);
write_com(0x80+11);
write_com(0x80+9);
write_dat(-);
write_ymd(7,3);
write_com(0x80+8);
write_com(0x80+6);
write_dat(-);
write_com(0x80+2);
write_dat(2);
write_dat(0);
write_ymd(4,12);
write_com(0x80+5);
write_com(0x80+13);
write_week(4);
}
void keyscan()
{
if(K2==0)
{
delay(5);
if(K2==0);//防止误动作
{
S1num++;
while(!K2);
switch(S1num)
{
case 1:
TR0=0;
write_com(0x80+0x40+11);
write_com(0x0f);
break;
case 2:
write_com(0x80+0x40+8);
break;
case 3:
write_com(0x80+0x40+5);
break;
case 4:
write_com(0x80+11);
break;
case 5:
write_com(0x80+8);
break;
case 6:
write_com(0x80+5);
break;
case 7:
write_com(0x80+13);
break;
case 8:
S1num=0;
TR0=1;
write_com(0x0c);
break;
}
}
}
if(S1num!=0)
{
if(K4==0)
{
delay(5);
while(!K4);
switch(S1num)
{
case 1:
miao++;
if(miao==60)miao=0;
write_sfm(10,miao);
write_com(0x80+0x40+11);
break;
case 2:
fen++;
if(fen==60)fen=0;
write_sfm(7,fen);
write_com(0x80+0x40+8);
break;
case 3:
shi++;
if(shi==24)shi=0;
write_sfm(4,shi);
write_com(0x80+0x40+5);
break;
case 4:
day++;
switch(mon)
{
case 1:if(day==32)day=1;break;
case 3:if(day==32)day=1;break;
case 5:if(day==32)day=1;break;
case 7:if(day==32)day=1;break;
case 8:if(day==32)day=1;break;
case 10:if(day==32)day=1;break;
case 12:if(day==32)day=1;break;
case 4:if(day==31)day=1;break;
case 6:if(day==31)day=1;break;
case 9:if(day==31)day=1;break;
case 11:if(day==31)day=1;break;
case 2:x=20*100+year;
if((x%4==0 && x%100!=0) || x%400==0)
{
if(day==30)
{
day=1;break;
}
}
else if(day==29)
{
day=1;break;
}
}
write_ymd(10,day);
write_com(0x80+11);
break;
case 5:
mon++;
if(mon==13)mon=1;
write_ymd(7,mon);
write_com(0x80+8);
break;
case 6:
year++;
write_ymd(4,year);
write_com(0x80+5);
break;
case 7:
week++;
if(week==8)week=1;
write_week(week);
write_com(0x80+13);
break;
}
}
if(K6==0)
{
delay(5);
while(!K6);
switch(S1num)
{
case 1:
miao--;
if(miao==-1)miao=59;
write_sfm(10,miao);
write_com(0x80+0x40+11);
break;
case 2:
fen--;
if(fen==-1)fen=59;
write_sfm(7,fen);
write_com(0x80+0x40+8);
break;
case 3:
shi--;
if(shi==-1)shi=23;
write_sfm(4,shi);
write_com(0x80+0x40+5);
break;
case 4:
day--;
switch(mon)
{
case 1:if(day==0)day=31;break;
case 3:if(day==0)day=31;break;
case 5:if(day==0)day=31;break;
case 7:if(day==0)day=31;break;
case 8:if(day==0)day=31;break;
case 10:if(day==0)day=31;break;
case 12:if(day==0)day=31;break;
case 4:if(day==0)day=30;break;
case 6:if(day==0)day=30;break;
case 9:if(day==0)day=30;break;
case 11:if(day==0)day=30;break;
case 2:x=20*100+year;
if((x%4==0 && x%100!=0) || x%400==0)
{
if(day==0)
{
day=29;break;
}
}
else if(day==0)
{
day=28;break;
}
}
write_ymd(10,day);
write_com(0x80+11);
break;
case 5:
mon--;
if(mon==0)mon=12;
write_ymd(7,mon);
write_com(0x80+8);
break;
case 6:
year--;
write_ymd(4,year);
write_com(0x80+5);
break;
case 7:
week--;
if(week==-1)week=7;
write_week(week);
write_com(0x80+13);
break;
}
}
}
}
void main()
{
init();
GND=0;
while(1)
{
keyscan();
if(count==20)
{
count=0;
miao++;
if(miao==60)
{
miao=0;
fen++;
if(fen==60)
{
fen=0;
shi++;
if(shi==24)
{
shi=0;
day++;
week++;
switch(mon)
{
case 1:if(day==32)day=1;mon++;break;
case 3:if(day==32)day=1;mon++;break;
case 5:if(day==32)day=1;mon++;break;
case 7:if(day==32)day=1;mon++;break;
case 8:if(day==32)day=1;mon++;break;
case 10:if(day==32)day=1;mon++;break;
case 12:if(day==32)day=1;mon++;break;
case 4:if(day==31)day=1;mon++;break;
case 6:if(day==31)day=1;mon++;break;
case 9:if(day==31)day=1;mon++;break;
case 11:if(day==31)day=1;mon++;break;
case 2:x=20*100+year;
if((x%4==0 && x%100!=0) || x%400==0)
{
if(day==30)
{
day=1;mon++;break;
}
}
else if(day==29)
{
day=1;mon++;break;
}
}
if(mon==13)
{
mon=1;
year++;
write_ymd(4,year);
}
write_ymd(10,day);
write_com(0x80+13);
write_week(week);
write_ymd(7,mon);
}
write_sfm(4,shi);
}
write_sfm(7,fen);
}
write_sfm(10,miao);
}
}
}
51单片机闰年补偿时钟程 相关文章:
- Windows CE 进程、线程和内存管理(11-09)
- RedHatLinux新手入门教程(5)(11-12)
- uClinux介绍(11-09)
- openwebmailV1.60安装教学(11-12)
- Linux嵌入式系统开发平台选型探讨(11-09)
- Windows CE 进程、线程和内存管理(二)(11-09)