《基于.51单片机的数码管时钟(带闹钟调时秒表).docx》由会员分享,可在线阅读,更多相关《基于.51单片机的数码管时钟(带闹钟调时秒表).docx(13页珍藏版)》请在优知文库上搜索。
1、*基于51单片机的数码管时钟程序*#include#defineuintunsignedint#defineucharunsignedcharsbitdula=P26;申明ul锁存器的锁存端sbitwela=P27;申明u2锁存器的锁存端sbitkeyl=P30;功能键sbitkey2=P3人1;增大键sbitkey3=P3人2;减小键sbitkey4=P3人3;秒表查看键sbitbeep=P2人3;蜂鸣端ucharnum,numl,num2,numm;ucharnumf,num,numsznum;uchars,slzfzflzmzml;ucharns,nsl,nf,nfl;ucharnumn
2、sznumnf;uchardingshi;voiddisplay(ucharmzucharmlzucharfzucharflzuchars,ucharsi);voiddisplayl(ucharnfzucharnflzucharnszucharnsl);voidkey();voidkeyl();voidalram();voiddi();voiddelayms(xms);voidT0Jme()interrupt1;voidmiaobiao();ucharcodetable=0x3fz0x06z0x5bz04fz0x66,0x6d,0x77,0x7c,0x39z0x5e,0x79z0x71;主函数
3、voidmain()intmain()TMOD=0X01;TH0=(65536-46080)256;TL0=(65536-46080)%256;EA=I;ETO=1;TRO=1;nums=12;numf=0;num2=0;num=0;while(l)(if(dingshi=O)(keyscan();display(mzmlzfzfl,szsl);alarm();)elsemiaobiao();keyscanl();displayl(nfznflznsznsl);returnO;蜂鸣器函数di()voiddi()(beep=0;delayms(100);beep=l;按键调时函数key()voi
4、dkeyscan()(if(keyl=O)(delayms(10);if(keyl=O)nums+;if(nums=24);nums=0;while(!keyl);di();display(mzmlzf,flzs,sl);if(key2=0)delayms(10);if(key2=0)numf+;if(numf=24);numf=0;while(!key2);di();display(mzmlzfzflzszsl);)if(key3=0)delayms(10);if(key3=0)(dingshi=dingshi;WhiIe(Ikeyl);di();display(mzmlzfzflzszsl
5、);按键设定闹钟函数keyscanl();voidkeyscan1()if(keyl=O)delayms(10);if(keyl=O)numns+;if(numns=24);numns=0;while(!keyl);di();displayl(nfznfl,nsznsl);)if(key2=0)delayms(10);if(key2=0)numnf+;if(numnf=24);numnf=0;while(!key2);di();displayl(nfznflznsznsl);)if(key3=0)(delayms(10);if(key3=0)dingshi=O;while(!keyl);di(
6、);displayl(nf,nflzns,nsl);)闹钟函数alram()voidalram()if(nums=numns)&(numnf=numf)beep=0;)秒表函数miaobiao()voidmiaobiao()if(key4=0)delayms(100);if(key4=O)while(!key4);di();displaym(ucharmzucharml);正常显示函数dispgy()voiddisplay(ucharmzucharmlzucharfzucharflzucharszucharsi)dula=l;PO=tableml秒位第1位dula=0;PO=Oxff;/送入位
7、选信号前关闭所有显示,防止翻开位选锁存时wela=1;PO=Oxef;wela=0;delayms(10);dula=l;PO=tablem;秒第2位dula=O;PO=Oxff;wela=l;PO=Oxdf;wela=0;delayms(100)dula=l;PO=tablefl;分第1位dula=O;PO=Oxff;wela=1;PO=Oxfb;wela=O;delayms(100)dula=l;PO=tablef;分第2位dula=O;PO=Oxff;wela=l;P0=0xf7;wela=0;delayms(10)dula=l;PO=tablesl;时第1位dula=O;PO=Oxff
8、;wela=1;PO=Oxfe;wela=0;delayms(100)dula=l;PO=tables;时第2位dula=O;PO=Oxff;wela=l;PO=Oxfd;wela=0;delayms(10)调时闹钟显示函数displayl()voiddisplayl(ucharnfzucharnflzucharnszucharnsl)nfl=numnf10;nf=numnf%10;nsl=nums10;ns=nums%10;dula=l;PO=tablenfl;分第1位dula=O;PO=Oxff;wela=l;PO=OXfb;待改参数wela=0;delayms(10);dula=l;PO
9、=tablenf;分第2位dula=O;PO=Oxff;wela=l;Po=OXf7;待改参数wela=0;delayms(10);dula=l;PO=tablensl;时第1位dula=O;PO=Oxff;wela=1;PO=OXfe;待改参数wela=0;delayms(10);dula=l;PO=tablens;时第2位dula=O;PO=Oxfd;wela=1;Po=OXm待改参数wela=0;delayms(10);秒表显示函数displaym()voiddisplaym(ucharmzucharml)dula=l;PO=tableml;秒位第T立dula=O;PO=Oxff;wel
10、a=l;PO=OXbf;wela=0;delayms(10);dula=l;PO=tablem;dula=O;PO=Oxff;wela=l;P0=0X7f;wela=0;delayms(10);中断服务函数voidTOjjme()interrupt1voidT0_time()interrupt1TH0=(65536-45872)256;/46080TL0=(65536-45872)%256;num2+;if(num2=20)num+;num2=0;m=num%10;ml=num10;f=numf%10;fl=numf10;s=nums%10;sl=nums10;if(num=59)num=0;numf+;numf=O;nums+;if(nums=24)nums=0;)延时函数delayms()voiddelayms(xms)uintizj;for(i=xms;i0;i-)for0=HO;jO;j-);