您好,欢迎访问三七文档
当前位置:首页 > 行业资料 > 酒店餐饮 > 51单片机时钟c程序
单片机数字时钟C程序#includeAT89X52.H#includeintrins.h#defineRESTP3_4#defineSCLKP3_7#defineDATAP3_6unsignedcharcodedisplayCode[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};unsignedcharcodecontroladdress[]={0x80,0x82,0x84,0x86,0x88,0x8a,0x8c,0x8e,0x90,0xbe};unsignedcharhour,minute,second;unsignedcharday,month,week,year;unsignedcharclockhour,clockminute;unsignedintnum;unsignedcharselect,oneminute;bitbdatamodeselect;voiddelay();voidwritetime();voiddisplay(unsignedchartemp,unsignedcharmode);voidsound(){while(num2000){P3_0=0;}P3_0=1;}voidinitial(){P0=0;TMOD=0x22;IE=0xCF;IT0=1;IT1=1;T2CON=0;RCAP2L=0xf0;RCAP2H=0x1f;TH2=0x1c;TL2=0xf0;IP=0X06;TH0=0x48;TL0=0x48;TH1=0X48;TR0=1;PCON=0x01;hour=12;minute=0;second=0;num=0;select=0;clockhour=12;clockminute=0;modeselect=0;day=16;month=8;week=7;year=9;}voidinterrupt0()interrupt0using3{TR1=1;select++;if(select==8){select=0;}if(select==1)ET2=1;oneminute=second-1;}voidtimer0()interrupt1using3{num++;if(num==5000){num=0;second++;}if(second=60){minute++;second=0;}if(minute=60){hour++;minute=0;}if(hour=24)hour=0;}voidinterrupt1()interrupt2using3{oneminute=second-1;switch(select){case4:{hour++;if(hour=24)hour=0;break;}case5:{minute++;if(minute=60)minute=0;break;}case6:{second++;if(second=60)second=0;break;}case2:{clockhour++;if(clockhour=24)clockhour=0;break;}case3:{clockminute++;if(clockminute=60)clockminute=0;break;}default:{modeselect=0;ET2=!ET2;break;}}}voidtimer1()interrupt3using3{if(select){if(second==oneminute){select=0;TR1=0;}}}voidtimer2()interrupt5using3{sound();TF2=0;if(minute!=clockminute||hour!=clockhour)TR2=0;}voiddelay(){unsignedchari=255;while(--i){;}}voiddisplay(unsignedchartemp,unsignedcharmode){if(temp==4||temp==2)P0=0;elseP0=1;if(mode)P1=displayCode[hour/10];elseP1=displayCode[clockhour/10];delay();if(temp==4||temp==2)P0=0;elseP0=2;if(mode)P1=displayCode[hour%10];elseP1=displayCode[clockhour%10];delay();if(temp==1||temp==2||temp==3)P0=0;elseP0=4;P1=0xbf;delay();if(temp==5||temp==3)P0=0;elseP0=8;if(mode)P1=displayCode[minute/10];elseP1=displayCode[clockminute/10];delay();if(temp==5||temp==3)P0=0;elseP0=16;if(mode)P1=displayCode[minute%10];elseP1=displayCode[clockminute%10];delay();if(temp==1||temp==2||temp==3)P0=0;elseP0=32;P1=0xbf;delay();if(temp==6)P0=0;elseP0=64;if(mode)P1=displayCode[second/10];elseP1=displayCode[0];delay();if(temp==6)P0=0;elseP0=128;if(mode){if(ET2)P1=displayCode[second%10]+128;elseP1=displayCode[second%10];}elseP1=displayCode[0];delay();}voidwrite(unsignedcharBdata){unsignedchari=8;for(;i0;i--){if(Bdata&0x01)DATA=1;elseDATA=0;SCLK=1;SCLK=0;Bdata=1;}}voidwritedata(unsignedcharaddress,Bdata){REST=0;SCLK=0;_nop_();REST=1;write(address);write(Bdata);REST=0;}unsignedcharread(unsignedcharBdata){unsignedchartdata=0,i=8;REST=0;SCLK=0;_nop_();REST=1;write(Bdata);for(;i0;i--){tdata=1;if(DATA)tdata=tdata|0x80;elsetdata|=0x00;SCLK=1;SCLK=0;}returntdata;}voidwritetime(){unsignedchari,time,temp[3];temp[0]=second;temp[1]=minute;temp[2]=hour;for(i=0;i3;i++){time=temp[i]%10;time|=(temp[i]/10)4;writedata(controladdress[i],time);}}voidwriteyear(){unsignedchari,year0,temp[4];temp[0]=day;temp[1]=month;temp[2]=week;temp[3]=year;for(i=0;i4;i++){year0=temp[i]%10;year0|=(temp[i]/10)4;writedata(controladdress[i+3],year0);}}voidreadtime(){unsignedchari,time,temp[3];for(i=0;i3;i++){temp[i]=read(1+controladdress[i]);time=temp[i]&0x0f;time+=(temp[i]4)*10;temp[i]=time;}second=temp[0];minute=temp[1];hour=temp[2];}voidreadyear(){unsignedchari,year0,temp[4];for(i=0;i4;i++){temp[i]=read(1+controladdress[i+3]);year0=temp[i]&0x0f;year0+=(temp[i]4)*10;temp[i]=year0;}day=temp[0];month=temp[1];week=temp[2];year=temp[3];}voidmain(){initial();sound();writedata(0x8e,0x00);writedata(0x84,0x12);writedata(0x8f,0x00);readtime();writeyear();readyear();while(1){switch(select){case0:{display(0,1);break;}case1:{if(num=2500){display(1,0);}else{display(0,0);}break;}case2:{if(num=2500){display(2,0);}else{display(0,0);}break;}case3:{if(num=2500){display(3,0);}else{display(0,0);}break;}case4:{if(num=2500){display(4,1);}else{display(0,1);}break;}case5:{if(num=2500){display(5,1);}else{display(0,1);}break;}case6:{if(num=2500){display(6,1);}else{display(0,1);}break;}case7:{writetime();writetime();writetime();select=0;break;}default:break;}if(second==0){if(minute==0||minute==30)sound();}if(minute==clockminute&&hour==clockhour){TR2=1;modeselect=1;}if(modeselect==1&&hour==clockhour&&!((minute-clockminute)%5))TR2=1;}}复制代码本主题由feiniao于2009-9-1218:58移动收藏分享评分''回复引用订阅TOPfeiniao发短消息加为好友feiniao(麻瓜)当前离线UID1帖子256精华9积分2305阅读权限200在线时间81小时注册时间2007-10-12最后登录2010-5-12管理员精华9积分2305威望487金钱512贡献110在线时间81小时沙发发表于2009-9-1217:06|只看该作者#includeregx51.h#defineTIMER0_COUNT0xee11codeseven_seg[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};codescan[]={0xef,0xdf,0xbf,0x7f};unsignedcharcounter[]={0,0,0,0};unsignedinttimer0_tick,i=0;staticvoidtimer0_isr(void)interruptTF0_VECTORusing1{TR0=0;TL0=(TIMER0_COUNT&0x00ff);TH0=(TIMER0_COUNT8);TR0=1;P0=seven_seg[counter];P1=scan;P0_7=0;i++;if(i==4
本文标题:51单片机时钟c程序
链接地址:https://www.777doc.com/doc-5077599 .html