/***************************************************************************
标题: 定时器中止 24小时时钟程序作用: 数码管显现24小时制时钟(具有调时功用)阐明: 运用12M晶振 ,key1=P3^7; key2=P3^6; key3=P3^5;key4=P3^4;数码管位选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; //数码管千位引脚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(); //函数声明void display(unsigned char hour,unsignedchar minute);void delay(unsigned int timer);void init(); /***********************************************************主函数***********************************************************/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);}}/***********************************************************调时***********************************************************/void keyscan(){if(key1==0) //小时加1{TR0=0;hour++;if(hour==24)hour=0;delay(200) ;TR0=1;}if(key2==0) // 小时减{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; }/***********************************************************定时器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; //小数点以半秒的时刻闪耀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; }/***********************************************************延时***********************************************************/void delay(unsigned int time){ unsignedint x,y;for(x=time;x>0;x--)for(y=110;y>0;y--);}