第1页#includereg52.h#defineuintunsignedint#defineucharunsignedchar#defineLEDP0//数码管的段选#defineLIGHTP1//时分秒位的指示灯#defineWSP2//数码管的位选sbitkey1=P3^0;//时间暂停/开始sbitkey2=P3^1;//时间/闹钟设置sbitkey3=P3^2;//增加sbitkey4=P3^3;//减少sbitalarm=P3^6;//闹铃uchartab[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};//0-9uchartab_dp[10]={0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,0xff,0xef};//0.-9.(带小数点)uchardata1[]={0,0,0,0,0,0};uchardata2[]={0,0,0,0,0,0};uintt,k,kk,k1,flag;uintbbh,bbm,bbs,bbh1,bbm1,bbs1;uintsec,min,hour,sec1,min1,hour1;//定义秒,分,时voidinit();voiddisplay();voiddisplay_bb();voiddelay(uint);voidkeyscan();voidmain(){init();while(1){keyscan();if(k1==0||k1==1||k1==2||k1==3)//显示调节时间{display();第2页}if(k1==4||k1==5||k1==6)//显示调节闹钟{display_bb();}if((bbh==hour)&&(bbm==min)&&data1[4]==0&&data1[5]==5)//5s报时{alarm=~alarm;delay(1);}if((bbs==sec)&&(bbm==min)&&(bbh==hour))//可调报时{alarm=~alarm;delay(1);}}}voidinit(){WS=LIGHT=flag=0;sec=min=hour=0;//将0赋给时分秒TMOD=0x01;//方式1P129(见课本)TH0=0x3c;//65536-50000=15536=0x3cb0(50ms)P128(见课本)TL0=0xb0;EA=1;//开总中断P161(见课本)TR0=1;//定时/计数器0开启ET0=1;//定时器/计数器0溢出中断启动P161(见课本)}voiddelay(unsignedintt)//延时函数{unsignedinti;while(t--)for(i=0;i125;i++);}voiddisplay()//显示时间函数{if(TF0==1)//定时器/计数器溢出P130(见课本){TF0=0;//清中断标志位t++;第3页if(t==20)//(50ms*20=1s){t=0;sec++;//秒加1if(sec==60)//秒为60,则清零,分加1{sec=0;min++;}if(min==60)//分为60,则清零,时加1{min=0;hour++;}if(hour==24)//时为24,则清零{hour=0;}}}data1[5]=sec%10;data1[4]=sec/10;data1[3]=min%10;data1[2]=min/10;data1[1]=hour%10;data1[0]=hour/10;WS=0xdf;//11011111,低电平显示LED=tab[data1[5]];delay(1);WS=0xef;//11101111LED=tab[data1[4]];delay(1);WS=0xf7;//11110111LED=tab_dp[data1[3]];delay(1);WS=0xfb;//11111011LED=tab[data1[2]];delay(1);第4页WS=0xfd;//11111101LED=tab_dp[data1[1]];delay(1);WS=0xfe;//11111110LED=tab[data1[0]];delay(1);}voiddisplay_bb()//显示闹钟函数{data2[5]=bbs%10;data2[4]=bbs/10;data2[3]=bbm%10;data2[2]=bbm/10;data2[1]=bbh%10;data2[0]=bbh/10;WS=0xdf;//11011111,低电平显示LED=tab[data2[5]];delay(1);WS=0xef;//11101111LED=tab[data2[4]];delay(1);WS=0xf7;//11110111LED=tab_dp[data2[3]];delay(1);WS=0xfb;//11111011LED=tab[data2[2]];delay(1);WS=0xfd;//11111101LED=tab_dp[data2[1]];delay(1);WS=0xfe;//11111110LED=tab[data2[0]];delay(1);}voidkeyscan()//键盘扫描第5页{if(key1==0)//暂停/开始{++kk;while(!key1){display();if(kk==1){TR0=0;if(k1==0||k1==1||k1==2||k1==3)//显示调节时间{display();}if(k1==4||k1==5||k1==6)//显示调节闹钟{display_bb();}if(key2==0)//模式选择(调节时间/闹钟){k1++;while(!key2){if(k1==1)//第1次按下{sec1=sec;//保存秒的数值sec=88;//显示88,表示可以调节秒的数值了display();//显示88sec=sec1;//恢复前一刻秒的数值}if(k1==2){min1=min;min=88;display();delay(1);min=min1;}if(k1==3){hour1=hour;hour=88;第6页display();delay(1);hour=hour1;}if(k1==4){sec1=bbs;//保存秒的数值bbs=66;//显示66,表示可以调节秒的数值了display_bb();//显示66bbs=sec1;//恢复前一刻秒的数值}if(k1==5){min1=bbm;bbm=66;display_bb();delay(10);bbm=min1;}if(k1==6){hour1=bbh;bbh=66;display_bb();delay(10);bbh=hour1;}if(k1==7){k1=0;display();}}}if(key3==0)//时间/闹钟增加设置{while(!key3){if(k1==1){sec++;//秒加1第7页delay(60);if(sec==60)sec=0;display();}if(k1==2){min++;delay(60);if(min==60)min=0;display();}if(k1==3){hour++;delay(60);if(hour==24)hour=0;display();}if(k1==4){bbs++;//秒加1delay(60);if(bbs==60)bbs=0;display_bb();}if(k1==5){bbm++;delay(60);if(bbm==60)bbm=0;display_bb();}if(k1==6){bbh++;delay(60);if(bbh==24)bbh=0;display_bb();第8页}if(k1==7){k1=0;display();}}}if(key4==0)//时间/闹钟减少设置{while(!key4){if(k1==1){sec--;//秒加1delay(60);if(sec==0)sec=60;display();}if(k1==2){min--;delay(60);if(min==0)min=60;display();}if(k1==3){hour--;delay(60);if(hour==0)hour=24;display();}if(k1==4){bbs--;//秒减1delay(60);if(bbs==0)bbs=60;display_bb();第9页}if(k1==5){bbm--;delay(60);if(bbm==0)bbm=60;display_bb();}if(k1==6){bbh--;delay(60);if(bbh==0)bbh=24;display_bb();}if(k1==7){k1=0;display();}}}}}if(kk==2){kk=0;k1=0;TR0=1;}}}