资源描述:
《单片机C语言 电子时钟程序》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、#include#defineuintunsignedint#defineucharunsignedcharunsignedcharkey2;bitding=1;unsignedcharGetkey(void);uchara,n=0,shi,fen,miao;voiddelay01s(void);ucharLED[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};ucharLED1[]={0x40,0x79,0x24,0x30,0x19,0x12,0x
2、02,0x78,0x00,0x10};//有小数点的voidinit();//函数声明voidDelay(unsignedcharz);//函数声明voiddisplay();//函数声明//函数声明voidmain()//函数声明{P1=0xfe;//对P1口赋初值init();//函数调用while(1){//函数调用key2=Getkey();switch(key2){case1:shi++;if(shi==24){shi=0;}break;case2:fen++;if(fen==60){fen=0;}break;case3
3、:if(fen!=0)fen--;if(fen==24){fen=0;}break;case4:ding=~ding;default:break;}display();//函数调用}}voidinit(){TMOD=0x01;//定时器工作方式选择和赋初值TH0=(65536-50000)/256;TL0=(65536-50000)%256;EA=1;//开总中断ET0=1;//开定时器中断TR0=1;//启动定时器}voidtimer0()interrupt1//中断服务程序{if(ding==1){TH0=(65536-500
4、00)/256;//中断时间50msTL0=(65536-50000)%256;//定时器重新赋初值a++;if(a==10){n=~n;}if(a==20){n=~n;a=0;P1=P1<<1
5、P1>>7;miao++;if(miao==60){miao=0;fen++;if(fen==60){fen=0;shi++;if(shi==24){shi=0;}}}}}}voiddisplay()//显示程序{P0=LED[shi/10];P2=((P2&0x0f)
6、0x70);Delay(4);if(n==0){P0=LED[shi
7、%10];}else{P0=LED1[shi%10];}P2=((P2&0x0f)
8、0xb0);Delay(4);P0=LED[fen/10];P2=((P2&0x0f)
9、0xd0);Delay(4);P0=LED[fen%10];P2=((P2&0x0f)
10、0xe0);Delay(4);}/**********获得键值子程序**********************/unsignedcharbool;//bool是否松键的标志unsignedcharGetkey(void){unsignedchartemp,key=0;P2=
11、(P2&0xff)
12、0x0f;if((P2&0xff)!=((P2&0xff)
13、0x0f))//有键按下{//delay01s();if(((P2&0xff)!=((P2&0xff)
14、0x0f))&&(bool==0))//有键按下{temp=~(P2
15、0xf0);if(temp==1)key=1;elseif(temp==2)key=2;elseif(temp==4)key=3;elseif(temp==8)key=4;bool=1;}}if(((P2&0xff)==((P2&0xff)
16、0x0f))&&(bool==1)){b
17、ool=0;}returnkey;//返回1~16键值}/********延时程序******/voiddelay01s(void){unsignedcharj,k;for(j=5;j>0;j--)//198{for(k=15;k>0;k--)//248{;}}}voidDelay(unsignedcharz){unsignedchari,j,k;//定义变量for(i=z;i>0;i--)for(j=25;j>0;j--)for(k=20;k>0;k--);}