#include<reg51.h>
#include<intrins.h>
#define u8 unsigned char
#define u16 unsigned int
u8 code ledcode[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
u8 cnt_1ms,smg_bit,keynum;
u8 tmp_old=0xf0,tmp_new;
u16 cnt_10ms,beep_time,keypush_time,cnt_s;
bit flag_1ms,flag_10ms,flagpush,flag_beep,flag_run,flag_stop,flag_down;
sbit beep=P3^7;
sbit key=P3^6;
sbit led_run=P3^0;
sbit led_stop=P3^1;
u8 t_bai,t_shi,t_ge,keycnt;
int time,s_time;
/********************************************************************/
void init(void)
{TMOD=0x01;
TH0=(65536-500)/256;
TL0=(65536-500)%256;
EA=ET0=TR0=1;
P2=0xff;
}
/********************************************************************/
void display(void)
{
P2=P2|0x0f;
if(!flag_down)
{switch(smg_bit)
{case 0: P0=ledcode[t_ge];P2=P2&0xfe;break;
case 1: P0=ledcode[t_shi];P2=P2&0xfd;break;;
case 2: P0=ledcode[t_bai];P2=P2&0xfb;break;
case 3: P0=0x0;P2=P2&0xf7;break;
}
}
else
{switch(smg_bit)
{case 0: P0=0x00;P2=P2|0x0f;break;
case 1: P0=0x00;P2=P2|0x0f;break;;
case 2: P0=0x00;P2=P2|0x0f;break;
case 3: P0=0x00;P2=P2|0x0f;break;
}
}
}
/********************************************************************/
u8 keyscan(void)
{ u8 temp_keynum=0;
tmp_new=key;
if(((tmp_new&0x01)==0)&&((tmp_old&0x01)==1)) {flagpush=1;}
if(flagpush)keypush_time++;
if(key)
{if((keypush_time>1)&&(keypush_time<100))
{temp_keynum=1;flag_beep=1;}
else if (keypush_time>=300)
{temp_keynum=2;flag_beep=1;}
if (keypush_time==0) temp_keynum=0;
keypush_time=0;
flagpush=0;
}
tmp_old=tmp_new;
return temp_keynum;
}
/********************************************************************/
void keyfunction(u8 kf)
{
switch(kf)
{
case 1: if(keycnt==1) {t_bai++;if(t_bai>9){keycnt=0; t_bai=9;}
time=100*t_bai+10*t_shi+t_ge;
}
else {flag_run=1;keycnt=1;flag_down=0;flag_stop=0;time=100;}
break;
case 2: {flag_down=1;keycnt=0;flag_run=0;} break;
}
}
/********************************************************************/
void main(void)
{
init();
t_bai=0;
time=100;
while(1)
{
led_run=!flag_run;
led_stop=!flag_stop;
if(flag_1ms)
{
flag_1ms=0;
t_ge=time%10;
t_shi=time/10%10;
t_bai=time/100;
display();
smg_bit=++smg_bit%4;
}
if(flag_10ms)
{
flag_10ms=0;
keynum=keyscan();
keyfunction(keynum);
if(flag_stop)
{
s_time++;
if(s_time>799)
{
time=100;
keycnt=0;
flag_down=0;
}
}
}
}
}
/********************************************************************/
void tt0(void) interrupt 1 using 1
{
TH0=(65536-500)/256;
TL0=(65536-500)%256;
if(flag_beep)
{beep_time++;
beep=!beep;
if(beep_time>300)
{
beep_time=0;
flag_beep=0;
beep=1;
}
}
cnt_1ms++;
if(cnt_1ms>1)
{flag_1ms=1; cnt_10ms++;cnt_1ms=0;
if(cnt_10ms>9)
{flag_10ms=1;
if(flag_run)cnt_s++;
cnt_10ms=0;
if(cnt_s>99)
{cnt_s=0;
time=time-1;
if(time<=0){time=0;flag_stop=1;flag_run=0;flag_down=1;keycnt=0;}
}
}
}
}
/********************************************************************/
改下脚位可以用 |