资源描述:
《51单片机电子闹钟程序(亲自编写,可用)》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、学校电子钟,有闹钟功能,按键可调时间,可调打铃时间,打铃时间长短显示,每个模块有功能注释。其中正常时间显示和闹钟时间显示可用一个开关来调整。芯片选择STC89C52程序:#include#include#defineucharunsignedchar#defineuintunsignedint〃定义显示段码ucharcodetable[l={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x00};ucharcodebbtime[]={0x06,0x5
2、b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};ucharclock[J={0,0,0,0};ucharclock1[]={12,30,0};ucharweikongf6];ucharbbduration=4;ucharlingtime=9;〃学校打铃时间组ucharshangwu1[]二{&30};ucharshangwu2[]={10,0};ucharshangwu3[]={10,20);ucharshangwu4[J={11,50};ucharxiaw
3、ul[]={13,30};ucharxiawu2[]={15,00};ucharxiawu3[]={15J5};ucharxiawu4[]={16,45};〃按键定义sbitmode=PlA7;sbitsec_clr=PlA0;sbitmin_set_add=PlA3;sbitmin_set_sub=P1A4;sbithoursetadd=PlAl;sbithoursetsub=PlA2;sbitbb_set_add=PlA5;sbitbb_set_sub=P1A6;sbitspeaker=P2A6;〃延时函数voiddelay(unsigne
4、dintt)while(t-);//时钟进位函数voidclockjinwei(){clock[()]++;if(clock[0]==20){clock]1]++;clock[0]=0;if(clock[l]==60){clock[2]++;clock[l]=0;if(clock[2]==60){clock⑶++;clock[2]=0;if(clock[3]==24)clock[3]=0;〃定时器0中断服务函数voidtimeiO(void)interruptIusing1{TMOD=0x01;TH0=0x3c;TLO=()xbO;clockj
5、inwei();}〃时钟分位显示函数voidfenwei(){weikong⑹二clock[3]/10;weikong[1]=clock[3]%10;weikong[2]=clock[2]/10;weikong[3]=clock[2]%1();weikong[4]=clock[l]/10;weikong[5]=clock[l]%10;〃闹钟分位显示函数voidnaofen()weikong[0]=clockl[0J/10;weikongfl]=clockl[0]%10;weikong[2]=clock1[I]/10;weikong⑶二clock
6、l[l]%10;weikong[4]=clock1[2]/10;weikong[5]=clockl⑵%】。;}〃闹钟定时显示函数voidnaozhongdisplayO{ucharz,s;ucharx=0x01;naofen();for(z=0;z<6;z++){P2=0;PO=table[weikong[z]];P2=x;x=_crol_(x,l);for(s=0;s<255;s++);〃时钟显示函数voiddisplayO{uchari,j;ucharx=0x0l;fenwei();for(i=0;i<6;i++){P2=0;PO=tabl
7、e[weikong[i]];P2=x;x=_crol_(x,l);for(j=0;j<255;j++);〃总显示函数voidzhongxian()if(mode==l)delay(lOO);if(mode==l)displayO;if(mode==0)delay(lOO);if(mode==0)naozhongdisplayO;}〃按键处理程序voidkey_set(){zhongxian();PI=Oxff;if(min_set_add==O){delay(lOO);if(min_set_add==O){if(mode==l){clock[2
8、]++;if(clock[2]==60){clock⑵=0;}while(min_set_add==O)zhongxian();}}if(mode==