您好,欢迎访问三七文档
当前位置:首页 > 电子/通信 > 电子设计/PCB > 带按键修改的数码管时钟
#includereg51.h#defineucharunsignedchar#defineuintunsignedintucharcodedulatab[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e,0xbf};ucharcodekeytab[]={0xE7,0xD7,0xB7,0x77,0xEB,0xDB,0xBB,0x7B,0xED,0xDD,0xBD,0x7D,0xEE,0xDE,0xBE,0x7E};ucharbuf[8]={0};ucharnum,keyvalue,sec=0,min=0,hour=0,flag=0,counter=0,wei=-1;sbitwela=P1^3;sbitdula=P1^2;voiddelay(ucharu){uchari;for(;u0;u--)for(i=124;i0;i++);}voidinito(){TMOD=0X01;TH0=(65536-50000)/256;TL0=(65536-50000)%256;EA=1;ET0=1;TR0=1;sec=46;min=59;hour=10;}voidbcd(){buf[0]=sec%10;buf[1]=sec/10;buf[2]=16;buf[3]=min%10;buf[4]=min/10;buf[5]=16;buf[6]=hour%10;buf[7]=hour/10;}voiddisplay(){staticuchari;bcd();P0=0;wela=1;wela=0;P0=dulatab[buf[i]];dula=1;dula=0;P0=(1i);wela=1;wela=0;delay(2);if(++i8)i=0;}ucharkey(){staticucharscan1,scan2,scan;staticuchari;P2=0XF0;scan1=P2&0xf0;if(scan1!=0XF0){delay(5);if(scan1!=0xf0){scan1=P2&0xf0;P2=0X0F;scan2=P2&0x0f;while(P2!=0x0f);scan=scan1|scan2;for(i=0;i16;i++){if(scan==keytab[i]){num=i;return(num);}}/*if(i==16){;return0xff;}*/}//return0xff;}return10;}voidkeyscan(){ucharsec0,sec1,min0,min1,hour0,hour1,hour2;keyvalue=key();switch(keyvalue){case0:{if(flag==0){flag=1;TR0=0;}else{TR0=1;flag=0;wei=-1;}break;}case1:{if(flag==1){wei++;if(wei==2|wei==5)wei=wei+1;if(wei==8)wei=0;}break;}case2:{if(flag==1){if(wei==-1)wei=8;wei--;if(wei==5|wei==2)wei=wei-1;if(wei==-1)wei=7;}break;}case3:{if(flag==0){break;}if(wei==0){sec0=sec%10;sec0++;if(sec0==10)sec0=9;sec=sec/10*10+sec0;}if(wei==1){sec+=10;sec1=sec/10;if(sec1==6)sec1=5;sec=sec1*10+sec%10;}if(wei==3){min0=min%10;min0++;if(min0==10)min0=9;min=min/10*10+min0;}if(wei==4){min+=10;min1=min/10;if(min1==6)min1=5;min=min1*10+min%10;}if(wei==6){hour0=hour/10;if(hour0==2){hour1=hour%10;hour1++;if(hour1==4)hour1=3;hour=hour/10*10+hour1;}else{hour1=hour%10;hour1++;if(hour1==10)hour1=9;hour=hour/10*10+hour1;}}if(wei==7){hour+=10;if((hour%10)3){hour2=hour/10;if(hour2==2)hour2=1;}else{hour2=hour/10;if(hour2==3){hour2=2;}}hour=hour2*10+hour%10;}break;}case4:{if(flag==0){break;}if(wei==0){sec0=sec%10;sec0--;if(sec0==-1)sec0=0;sec=sec/10*10+sec0;}if(wei==1){sec1=sec/10;sec1--;if(sec1==-1)sec1=0;sec=sec1*10+sec%10;}if(wei==3){min0=min%10;min0--;if(min0==-1)min0=0;min=min/10*10+min0;}if(wei==4){min1=min/10;min1--;if(min1==-1)min1=0;min=min1*10+min%10;}if(wei==6){hour0=hour%10;hour0--;if(hour0==-1)hour0=0;hour=hour/10*10+hour0;}if(wei==7){hour1=hour/10;hour1--;if(hour1==-1)hour1=0;hour=hour1*10+hour%10;}break;}default:break;}}voidmain(){inito();while(1){display();keyscan();}}voidtimer0()interrupt1{TH0=(65536-50000)/256;TL0=(65536-50000)%256;counter++;if(counter==20){counter=0;sec++;if(sec==60){sec=0;min++;if(min==60){min=0;hour++;if(hour==24){hour=0;}}}}}
本文标题:带按键修改的数码管时钟
链接地址:https://www.777doc.com/doc-5313005 .html