资源描述:
《带按键修改的数码管时钟.doc》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、#include#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,0x
2、BE,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(;u>0;u--)for(i=124;i>0;i++);}voidinito(){TMOD=0X01;TH0=(65536-50000)/256;TL0=(65536-50000)%256;EA=1;ET0=1;TR0=1;sec=4
3、6;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=(1<
4、8)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
5、scan2;for(i=0;i<16;i++){if(scan==keytab[i]){num=i;return(num);}}/*if(i==16)
6、{;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
7、wei==5)wei=wei+1;if(wei==8)we
8、i=0;}break;}case2:{if(flag==1){if(wei==-1)wei=8;wei--;if(wei==5
9、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*1
10、0+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{hour
11、1=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