/***************************************************************************標(biāo)題: 定時(shí)器中斷 24小時(shí)時(shí)鐘程序效果: 數(shù)碼管顯示24小時(shí)制時(shí)鐘(具有調(diào)時(shí)功能)說明: 使用12M晶振 ,key1=P3^7; key2=P3^6; key3=P3^5;key4=P3^4;數(shù)碼管位選P1.0~P1.3 ,段選P0******************************************************************************/#include#define uchar unsigned char;#define uint unsigned int;uchar position; uchar tt; uchar second; uchar minute;uchar hour; uchar code table[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};//0--9sbit smg_q = P1^0; //數(shù)碼管千位引腳sbit smg_b = P1^1; sbit smg_s = P1^2;sbit smg_g = P1^3;sbit key1=P3^7; //按鍵定義sbit key2=P3^6;sbit key3=P3^5;sbit key4=P3^4;void keyscan(); //函數(shù)聲明void display(unsigned char hour,unsignedchar minute);void delay(unsigned int timer);void init(); /***********************************************************主函數(shù)***********************************************************/void main(){init(); while(1){if(tt==20){ tt=0;second++;if(second==60){second=0;minute++;if(minute==60){ minute=0;hour++;if(hour==24)hour=0;}}}keyscan();display(hour,minute); delay(1);}}/***********************************************************調(diào)時(shí)***********************************************************/void keyscan(){if(key1==0) //小時(shí)加1{TR0=0;hour++;if(hour==24)hour=0;delay(200) ;TR0=1;}if(key2==0) // 小時(shí)減{TR0=0;hour--;if(hour<0)hour=23;delay(200) ;TR0=1;}if(key3==0) // 分鐘加{TR0=0;minute++;if(minute==60)minute=0;delay(200) ;TR0=1;}if(key4==0) // 分鐘減{TR0=0;minute--;if(minute<0)minute=59;delay(200) ;TR0=1;}}/***********************************************************初始化***********************************************************/void init(){tt=0;position=0; second=0; minute=0; hour=0; smg_q=1;smg_b=1;smg_s=1;smg_g=1;key1=1;key2=1;TMOD=0X01; TH0=(65536-50000)/256; TL0=(65536-50000)%256;EA=1; ET0=1; TR0=1; }/***********************************************************定時(shí)器TO中斷***********************************************************/void t0() interrupt 1{TH0=(65536-50000)/256; TL0=(65536-50000)%256;tt++; }/***********************************************************顯示***********************************************************/void display(unsigned char hour,unsignedchar minute){P0=0XFF; switch(position){case0: smg_g=1;smg_q=0;P0=table[hour/10]; break;case1:smg_q=1; smg_b=0; P0=table[hour%10];if(tt>=10)P0&=0x7f; //小數(shù)點(diǎn)以半秒的時(shí)間閃爍break;case2: smg_b=1; smg_s=0; P0=table[minute/10];break;case3: smg_s=1;smg_g=0;P0=table[minute%10];break;}position++; if(position>3)position=0; }/***********************************************************延時(shí)***********************************************************/void delay(unsigned int time){ unsignedint x,y;for(x=time;x>0;x--)for(y=110;y>0;y--);}
評(píng)論