#include
#define uint unsigned int
#define uchar unsigned char
sbit k0 = P3^0 ; //分"+"键
sbit k1 = P3^1 ; //时"+"键
uint hour=12,min=0,sec=0;
uint tmcnt;//定时器计数
uchar code seg7[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90}; //共阳数码管0—9码值
uchar code disp[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf}; // 位选
void delay1ms(uint k) //延时约1毫秒
{ uint a,b;
for(a=0;a
{ for(b=0;b<120;b++);
}
}
void init_timer( ) //定时20毫秒
{ TMOD=0x01;
TH0=0x4c; //50ms初值 晶振11.0592
TL0=0x00;
EA=1;
ET0=1 ;
TR0=1 ;
}
void display( ) //显示子程序
{ uchar i;
uchar data disdata[6]; //数据处理
disdata[0]=hour/10;
disdata[1]=hour%10;
disdata[2]=min/10;
disdata[3]=min%10;
disdata[4]=sec/10;
disdata[5]=sec%10;
for(i=0;i<6;i++)
{ P0 = 0x00;
P0 = seg7[disdata];
P1=disp;
delay1ms(2);
}
}
void time0() interrupt 1
{ TMOD=0x01;
TH0=0x4c; //50ms初值 晶振11.0592
TL0=0x00;
tmcnt++;
if(tmcnt==20) //计数50次为1秒
{ tmcnt=0;
sec++;
if(sec==60)
{ sec=0;
min++;
if(min==60)
{ min=0;
hour++;
if(hour==24)
{ hour=0;
}
}
}
}
}
void keyscan( ) //按键扫描
{ if(k1==0)
{ delay1ms(10);
if(k1==0)
{ while(k1==0);
sec=0;
hour++;
if(hour==24)
{ hour=0;
}
}
}
if(k0==0)
{ delay1ms(10);
if(k0==0)
{ while(k0==0);
sec=0;
min++;
if(min==60)
{ min=0;
}
}
}
}
void main( ) //主函数
{ init_timer( );
while(1)
{ keyscan( );
display( );
}
} |