《51单片机数码管显示时钟程序(共7页).doc》由会员分享,可在线阅读,更多相关《51单片机数码管显示时钟程序(共7页).doc(7页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、精选优质文档-倾情为你奉上#include/#include#include#define uchar unsigned char#define uint unsigned intsbit dula=P26;sbit wela=P27;sbit key1=P34;sbit key2=P35;sbit key3=P36;sbit beep=P23;unsigned code table=0x3f , 0x06 , 0x5b , 0x4f ,0x66 , 0x6d , 0x7d , 0x07 ,0x7f , 0x6f , 0x77 , 0x7c,0x39 , 0x5e , 0x79 , 0x71
2、;uchar num1,num2,s,s1,m,m1,f,f1,num,numf,nums,dingshi;uchar ns,ns1,nf,nf1,numns,numnf;void delay(uint z); void keyscan ();void keyscan1 ();void alram();void display(uchar m,uchar m1,uchar f,ucharf1,uchars,uchars1);void display0(uchar nf,uchar nf1,uchar ns,uchar ns1); void main() TMOD=0x01;/设定定时器0工作方
3、式1 TH0=(65536-46080)/256 ;TL0=(65536-46080)%256 ;EA=1;/开总中断ET0=1;/开定时器0中断TR0=1;/启动定时器0中断numns=12;numnf=0; while(1) if(dingshi=0) keyscan (); display(m,m1,f,f1,s,s1); alram(); else keyscan1 (); display0(nf,nf1,ns,ns1); void keyscan () if(key1=0) delay(10); if(key1=0) nums+; if (nums=24) nums=0; while
4、(!key1);display(m,m1,f,f1,s,s1); if(key2=0) delay(10); if(key2=0) numf+; if (numf=60) numf=0; while(!key2); display(m,m1,f,f1,s,s1); if(key3=0) delay(10); if(key3=0) dingshi=dingshi; while(!key3) display(m,m1,f,f1,s,s1); /* if(key4=0) delay(10); if(key4=0) flag=1; while(!key4); display(m,m1,f,f1,s,s
5、1); */ void keyscan1() if(key1=0) delay(10); if(key1=0) numns+; if (numns=24) numns=0; while(!key1);/ display(nf,nf1,ns,ns1); if(key2=0) delay(10); if(key2=0) numnf+; if (numnf=60) numnf=0; while(!key2);/ display(nf,nf1,ns,ns1); if(key3=0) delay(10); if(key3=0) dingshi=0; while(!key3);/ display(m,m1
6、,f,f1,s,s1); void alram() if(numnf=numf)&(numns=nums) beep=0; if(numnf+1=numf)&(numns=nums)&(dingshi=0)/一分钟报时提示 beep=1; void display0(uchar nf,uchar nf1,uchar ns,uchar ns1)/闹钟显示函数 nf=numnf%10; nf1=numnf/10; ns=numns%10; ns1=numns/10;/* wela=1; P0=0xc0;/送位选数据 wela=0; P0=0xff;*/ dula=1; P0=tablens1; d
7、ula=0; P0=0xff; wela=1; P0=0xfe; wela=0; delay(1); dula=1; P0=tablens|0x80; dula=0; P0=0xff; wela=1; P0=0xfd; wela=0; delay(1); dula=1; P0=tablenf1; dula=0; P0=0xff; wela=1; P0=0xfb; wela=0; delay(1); dula=1; P0=tablenf; dula=0; P0=0xff; wela=1; P0=0xf7; wela=0; delay(1); void display(uchar m,uchar
8、m1,uchar f,ucharf1,uchars,uchars1)/时间显示 dula=1; P0=tablem1; /秒位第1位 dula=0; P0=0xff; wela=1; P0=0xef; wela=0; delay(1); dula=1; P0=tablem; /秒位第2位 dula=0; P0=0xff; wela=1; P0=0xdf; wela=0; delay(1); dula=1; P0=tables1; /时位第一位 dula=0; P0=0xff; wela=1; P0=0xfe; wela=0; delay(1); dula=1; P0=tables|0x80;
9、dula=0; P0=0xff; wela=1; P0=0xfd; wela=0; delay(1); dula=1; P0=tablef1;/分位第一位 dula=0; P0=0xff; wela=1; P0=0xfb; wela=0; delay(1); dula=1; P0=tablef|0x80; dula=0; P0=0xff; wela=1; P0=0xf7; wela=0; delay(1); void T0_timer() interrupt 1 TH0=(65536-46080)/256 ;TL0=(65536-46080)%256 ;num2+; if(num2=20) num+; num2=0; m=num%10; m1=num/10; f=numf%10; f1=numf/10; s=nums%10; s1=nums/10; if(num=59) num=0; numf+; if(numf=59) numf=0; nums+; if (nums=24) nums=0; void delay(uint z) uint x,y; for(x=110;x0;x-) for(y=z;y0;y-);专心-专注-专业
限制150内