设计一个基于51单片机的电子时钟, 具有显示年、月、日、星期、时、分、秒、闹钟功能
#include
#define uchar unsigned char
#define uint unsigned int
sbit BF = P0^7;
sbit EN = P2^5;
sbit RS = P1^0;
sbit RW = P1^1;
sbit dula=P2^6;
sbit wela=P2^7;
sbit s1=P3^0;
sbit s2=P3^1;
sbit s3=P3^2;
sbit s4=P3^3;
sbit rd=P3^7;
sbit fm=P2^3;
uint num;
unsigned long int dat,nian=2016;
char miao=55,shi=23,fen=59,xingqi=6,kg,ri=31,yue=12,count,s1num,s4num,cshi=0,cfen=0,cmiao=0,zt;
uchar code table[]=" - - ";
uchar code table1[]=" : : ";
uchar code *table2[7]={"MON","TUE","WED","THU","FRI","SAT","SUN"};
uchar code *table3[2]={"OF","ON"};
uchar code alarm[]={" alarm setting "};
void delayms(uint z)//延时
{
uint i,j;
for(j=z;j>0;j--)
for(i=110;i>0;i--);
}
void wait(void)//判忙
{
P0=0xFF;
do{
RS=0;
RW=1;
EN=0;
EN=1;
}
while (BF==1);
EN=0;
}
void w_dat(uchar dat)//数据写入
{
wait();
EN=0;
P0=dat;
RS=1;
RW=0;
EN=1;
EN=0;
}
void w_cmd(uchar cmd)//命令写入
{
wait();
EN=0;
P0=cmd;
RS=0;
RW=0;
EN=1;
EN=0;
}
void w_sfm(uchar add,uchar date)//时分秒写入
{
uchar shi,ge;
shi=date/10;
ge=date%10;
w_cmd(0x80+add);
w_dat(0x30+shi);
w_dat(0x30+ge);
}
void w_nian(uchar add, unsigned long int dat) //年份写入
{
uchar qian,bai,shi,ge;
ge=dat%10;
shi=(dat/10)%10;
bai=(dat/100)%10;
qian=(dat/1000)%10;
w_cmd(0x80+add);
w_dat(0x30+qian);
w_dat(0x30+bai);
w_dat(0x30+shi);
w_dat(0x30+ge);
}
void xianshi(void)//显示函数
{
w_cmd(0x80);
for(num=0;num<16;num++){
w_dat(table[num]);
} //显示日期
w_cmd(0x80+0x40);
for(num=0;num<16;num++){
w_dat(table1[num]);
}
w_nian(1,nian);
w_sfm(6,yue);
w_sfm(9,ri);
w_cmd(0x80+12);
for(num=0;num<3;num++)
w_dat(table2[xingqi][num]);
w_sfm(0x40+4,shi);
w_sfm(0x40+7,fen);
w_sfm(0x40+10,miao); //显示时间
}
void Init_LCD1602(void)//初始化
{
dula=0;
wela=0;
w_cmd(0x38);
w_cmd(0x0c);
w_cmd(0x06);
w_cmd(0x01);
w_cmd(0x80);
xianshi();
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
EA=1;
ET0=1;
TR0=1;
}
void keyscan()//扫描按键
{
rd=0;
if(s1==0){
delayms(5);
if(s1==0){
s1num++;
while(!s1);
}
if(s1num==1){
s4num=0;
TR0=0;
w_cmd(0x80+0x40+11);
w_cmd(0x0f);
}
if(s1num==2){
w_cmd(0x80+0x40+8);
}
if(s1num==3){
w_cmd(0x80+0x40+5);
}
if(s1num==4){
w_cmd(0x80+14);
}
if(s1num==5){
w_cmd(0x80+10);
}
if(s1num==6){
w_cmd(0x80+7);
}
if(s1num==7){
w_cmd(0x80+4);
}
if(s1num==8){
s1num=0;
w_cmd(0x0c);
TR0=1;
}
}
if(s1num!=0){
if(s2==0)
{
delayms(5);
if(s2==0){
while(!s2);
if(s1num==1){
miao++;
if(miao==60)
miao=0;
w_sfm(0x40+10,miao);
w_cmd(0x80+0x40+11);
}
if(s1num==2){
fen++;
if(fen==60)
fen=0;
w_sfm(0x40+7,fen);
w_cmd(0x80+0x40+8);
}
if(s1num==3){
shi++;
if(shi==24)
shi=0;
w_sfm(0x40+4,shi);
w_cmd(0x80+0x40+5);
}
if(s1num==4){
uint num;
xingqi++;
if(xingqi==7)
xingqi=0;
w_cmd(0x80+12);
for(num=0;num<3;num++)
w_dat(table2[xingqi][num]);
w_cmd(0x80+14);
}
if(s1num==5){
ri++;
if(ri==29&&yue==2&&(nian%4!=0))
ri=1;
if(ri==29&&yue==2&&nian%100==0&&nian%400!=0)
ri=1;
if(ri==30&&yue==2&&((nian%100==0&&nian%400==0)||(nian%100!=0&&nian%4==0)))
ri=1;
if(ri==31&&(yue==4||yue==6||yue==9||yue==11))
ri=1;
if(ri==32&&(yue==1||yue==3||yue==5||yue==7||yue==8||yue==10||yue==12))
ri=1;
w_sfm(9,ri);
w_cmd(0x80+10);
}
if(s1num==6){
yue++;
if(yue==13)
yue=1;
w_sfm(6,yue);
w_cmd(0x80+7);
}
if(s1num==7){
nian++;
w_nian(1,nian);
w_cmd(0x80+4);
}
}
}
if(s3==0){
delayms(5);
if(s3==0){
while(!s3);
if(s1num==1){
miao--;
if(miao==-1)
miao=59;
w_sfm(0x40+10,miao);
w_cmd(0x80+0x40+11);
}
if(s1num==2){
fen--;
if(fen==-1)
fen=59;
w_sfm(0x40+7,fen);
w_cmd(0x80+0x40+8);
}
if(s1num==3){
shi--;
if(shi==-1)
shi=23;
w_sfm(0x40+4,shi);
w_cmd(0x80+0x40+5);
}
if(s1num==4){
uint num;
xingqi--;
if(xingqi==-1)
xingqi=6;
w_cmd(0x80+12);
for(num=0;num<3;num++)
w_dat(table2[xingqi][num]);
w_cmd(0x80+14);
}
if(s1num==5){
ri--;
if(ri==0&&yue==2&&(nian%4!=0))
ri=28;
if(ri==0&&yue==2&&nian%100==0&&nian%400!=0)
ri=28;
if(ri==0&&yue==2&&((nian%100==0&&nian%400==0)||(nian%100!=0&&nian%4==0)))
ri=29;
if(ri==0&&(yue==4||yue==6||yue==9||yue==11))
ri=30;
if(ri==0&&(yue==1||yue==3||yue==5||yue==7||yue==8||yue==10||yue==12))
ri=31;
w_sfm(9,ri);
w_cmd(0x80+10);
}
if(s1num==6){
yue--;
if(yue==0)
yue=12;
w_sfm(6,yue);
w_cmd(0x80+7);
}
if(s1num==7){
nian--;
w_nian(1,nian);
w_cmd(0x80+4);
}
}
}
}
if(s4==0){
delayms(5);
if(s4==0){
zt=1;
s4num++;
while(!s4);
}
if(s4num==1){
s1num=0;
w_cmd(0x80);
for(num=0;num<16;num++){
w_dat(alarm[num]);
}
w_cmd(0x80+0x40+14);
for(num=0;num<2;num++){
w_dat(table3[kg][num]);
}
w_sfm(0x40+4,cshi);
w_sfm(0x40+7,cfen);
w_sfm(0x40+10,cmiao);
w_cmd(0x80+0x40+15);
w_cmd(0x0f);
}
if(s4num==2){
w_cmd(0x80+0x40+11);
}
if(s4num==3){
w_cmd(0x80+0x40+8);
}
if(s4num==4){
w_cmd(0x80+0x40+5);
}
if(s4num==5){
zt=0;
delayms(10);
s4num=0;
w_cmd(0x0c);
}
}
if(s4num!=0){
if(s2==0){
delayms(5);
if(s2==0){
while(!s2);
if(s4num==1){
kg++;
if(kg==2)
kg=0;
w_cmd(0x80+0x40+14);
for(num=0;num<2;num++){
w_dat(table3[kg][num]);
}
w_cmd(0x80+0x40+15);
}
if(s4num==2){
cmiao++;
if(cmiao==60)
cmiao=0;
w_sfm(0x40+10,cmiao);
w_cmd(0x80+0x40+11);
}
if(s4num==3){
cfen++;
if(cfen==60)
cfen=0;
w_sfm(0x40+7,cfen);
w_cmd(0x80+0x40+8);
}
if(s4num==4){
cshi++;
if(cshi==24)
cshi=0;
w_sfm(0x40+4,cshi);
w_cmd(0x80+0x40+5);
}
}
}
if(s3==0){
delayms(5);
if(s3==0){
while(!s3);
if(s4num==1){
kg--;
if(kg==-1)
kg=1;
w_cmd(0x80+0x40+14);
for(num=0;num<2;num++){
w_dat(table3[kg][num]);
}
w_cmd(0x80+0x40+15);
}
if(s4num==2){
cmiao--;
if(cmiao==-1)
cmiao=59;
w_sfm(0x40+10,cmiao);
w_cmd(0x80+0x40+11);
}
if(s4num==3){
cfen--;
if(cfen==-1)
cfen=59;
w_sfm(0x40+7,cfen);
w_cmd(0x80+0x40+8);
}
if(s4num==4){
cshi--;
if(cshi==-1)
cshi=23;
w_sfm(0x40+4,cshi);
w_cmd(0x80+0x40+5);
}
}
}
}
}
main()
{
Init_LCD1602();
while(1){
keyscan();
if(kg&&miao==cmiao&&fen==cfen&&shi==cshi){
fm=0;
delayms(10000);
fm=1;
}
}
}
void timer0() interrupt 1
{
TH0=(65536-46080)/256;
TL0=(65536-46080)%256;
count++;
if(count==20){
count=0;
miao++;
if(miao==60){
miao=0;
fen++;
if(fen==60){
fen=0;
shi++;
if(shi==24){
shi=0;
ri++;xingqi++;
if(ri==29&&yue==2&&(nian%4!=0))
ri=1;
if(ri==29&&yue==2&&nian%100==0&&nian%400!=0)
ri=1;
if(ri==30&&yue==2&&((nian%100==0&&nian%400==0)||(nian%100!=0&&nian%4==0)))
ri=1;
if(ri==31&&(yue==4||yue==6||yue==9||yue==11))
ri=1;
if(ri==32&&(yue==1||yue==3||yue==5||yue==7||yue==8||yue==10||yue==12))
ri=1;
if(ri==1){
yue++;
if(yue==13){
yue=1;
nian++;
}
}
if(xingqi==7)
xingqi=0;
}
}
}
if(zt==0) xianshi();
}
}