资源描述:
《51单片机电子闹钟程序(亲自编写,可用)》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、学校电子钟,有闹钟功能,按键可调时间,可调打铃时间,打铃时间长短显示,每个模块有功能注释。其中正常时间显示和闹钟时间显示可用一个开关来调整。芯片选择STC89C52程序:#include#include#defineucharunsignedchar#defineuintunsignedint//定义显示段码ucharcodetable[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x00};ucharcodebbtime[]={0x06,0x5b,0x4f,0
2、x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};ucharclock[]={0,0,0,0};ucharclock1[]={12,30,0};ucharweikong[6];ucharbbduration=4;ucharlingtime=9;//学校打铃时间组ucharshangwu1[]={8,30};ucharshangwu2[]={10,0};ucharshangwu3[]={10,20};ucharshangwu4[]={11,50};ucharxiawu1[]={13,30};uc
3、harxiawu2[]={15,00};ucharxiawu3[]={15,15};ucharxiawu4[]={16,45};//按键定义sbitmode=P1^7;sbitsec_clr=P1^0;sbitmin_set_add=P1^3;sbitmin_set_sub=P1^4;sbithour_set_add=P1^1;sbithour_set_sub=P1^2;sbitbb_set_add=P1^5;sbitbb_set_sub=P1^6;sbitspeaker=P2^6;//延时函数voiddelay(unsignedintt){while(t--)
4、;}//时钟进位函数voidclockjinwei(){clock[0]++;if(clock[0]==20){clock[1]++;clock[0]=0;if(clock[1]==60){clock[2]++;clock[1]=0;if(clock[2]==60){clock[3]++;clock[2]=0;if(clock[3]==24)clock[3]=0;}}}}//定时器0中断服务函数voidtimer0(void)interrupt1using1{TMOD=0x01;TH0=0x3c;TL0=0xb0;clockjinwei();}//时钟分位显示函
5、数voidfenwei(){weikong[0]=clock[3]/10;weikong[1]=clock[3]%10;weikong[2]=clock[2]/10;weikong[3]=clock[2]%10;weikong[4]=clock[1]/10;weikong[5]=clock[1]%10;}//闹钟分位显示函数voidnaofen(){weikong[0]=clock1[0]/10;weikong[1]=clock1[0]%10;weikong[2]=clock1[1]/10;weikong[3]=clock1[1]%10;weikong[4]=c
6、lock1[2]/10;weikong[5]=clock1[2]%10;}//闹钟定时显示函数voidnaozhongdisplay(){ucharz,s;ucharx=0x01;naofen();for(z=0;z<6;z++){P2=0;P0=table[weikong[z]];P2=x;x=_crol_(x,1);for(s=0;s<255;s++);}}//时钟显示函数voiddisplay(){uchari,j;ucharx=0x01;fenwei();for(i=0;i<6;i++){P2=0;P0=table[weikong[i]];P2=x;x=
7、_crol_(x,1);for(j=0;j<255;j++);}}//总显示函数voidzhongxian(){if(mode==1)delay(100);if(mode==1)display();if(mode==0)delay(100);if(mode==0)naozhongdisplay();}//按键处理程序voidkey_set(){zhongxian();P1=0xff;if(min_set_add==0){delay(100);if(min_set_add==0){if(mode==1){clock[2]++;if(clock[2]==60){cl
8、ock[2]=0;}wh