可键盘设置倒计时时间
#include
#define uchar unsigned char
#define uint unsigned int
sbit p20=P2^0;
sbit p21=P2^1;
sbit p30=P3^0;
sbit p31=P3^1;
sbit p32=P3^2;
#define key_0 0x18
#define key_1 0x14
#define key_2 0x12
#define key_3 0x11
#define key_4 0x28
#define key_5 0x24
#define key_6 0x22
#define key_7 0x21
#define key_8 0x48
#define key_9 0x44
#define key_reset 0x42
#define key_stop 0x88
#define key_start 0x84
#define key_shi 0x82
#define key_ge 0x81
uchar t;
uchar ct=10;
uchar dh=1,dl=0;
uchar tab[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90}; //共阳极时的0~9
uchar key_table[]={0x18,0x14,0x12,0x11,0x28,0x24,0x22,0x21,0x48,0x44};
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void init(void)
{
TMOD=0x01; //选择方式1
TH0=0x3c; //12M晶振,定时时间50ms
TL0=0xb0;
EA=1; //开启总中断
ET0=1; //开启定时器0溢出中断
TR0=1; //启动定时器0
}
void display(void)
{
P0=tab[dh];
p20 = 1;
delay(2);
p20=0;
P0=tab[dl];
p21=1;
delay(2);
p21=0;
}
void display0(void)
{
P0=tab[dh];
p20 = 1;
delay(2);
p20=0;
P0=tab[dl];
p21=1;
delay(100);
p21=0;
}
void display1(void)
{
P0=tab[dh];
p20 = 1;
delay(100);
p20=0;
P0=tab[dl];
p21=1;
delay(2);
p21=0;
}
void timer0(void) interrupt 1 //定时器中断
{
uchar t;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
t++;
dh=ct/10;
dl=ct%10;
display();
if(t==20) //每秒进一次中断
{
t=0;
ct--;
if(ct==-1)
{
TR0=0;
p32=1;p31=1;p30=0;
}
}
}
void dlms(void)
{uchar i;
for(i=200;i>0;i--)
{}
}
uchar kbscan(void)
{uchar scode,recode;
P1=0xf0;
if((P1&0xf0)!=0xf0)
{dlms();
if((P1&0xf0)!=0xf0)
{scode=0xfe;
while((scode&0x10)!=0)
{P1=scode;
if((P1&0xf0)!=0xf0)
{recode=(P1&0xf0)|0x0f;
return((~scode)+(~recode));
}
else
scode=(scode
}
}
}
return(0);
}
void key_process(uchar key,uchar *dh,uchar *dl,uchar *ct)
{
static uchar k;
if(key==key_reset)
{*dh=0;
*dl=0;
*ct=(*dh)*10+(*dl);
p32=1;p31=1;p30=0;
}
if(key==key_0||key==key_1||key==key_2||key==key_3||key==key_4||key==key_5||key==key_6||key==key_7||key==key_8||key==key_9)
{k=0;
while(1)
{if(key==key_table[k])
break;
k++;
}
}
if(key==key_stop)
{TR0=0;
p32=1;p31=0;p30=1;
}
if(key==key_start)
{TR0=1;
p32=0;p31=1;p30=1;
}
if(key==key_shi)
{*dh=k;
display0();
*ct=(*dh)*10+(*dl);
}
if(key==key_ge)
{*dl=k;
display1();
*ct=(*dh)*10+(*dl);
}
}
void main()
{
uchar key;
p32=0;p31=1;p30=1;
init();
while(1)
{
key=kbscan();
dlms();
if(key!=0)
{
// switch(key)
// {case 0x18: n=0; break;
// case 0x14: n=1; break;
// case 0x12: n=2; break;
// case 0x11: n=3; break;
// case 0x28: n=4; break;
// case 0x24: n=5; break;
// case 0x22: n=6; break;
// case 0x21: n=7; break;
// case 0x48: n=8; break;
// case 0x44: n=9; break;
// case 0x42: n=10; break;
// case 0x41: n=11; break;
// case 0x88: TR0=0; break;
// case 0x84: TR0=1; break;
// case 0x82: n=14; break;
// case 0x81: n=15; break;
// }
key_process(key,&dh,&dl,&ct);
//if(p26==0)
//{dh=n;display0();ct=dh*10+dl;}
//if(p27==0)
//{dl=n;display1();ct=dh*10+dl;}
}
//if(p26==0) display0();
//if(p27==0) display1();
//else display();
display();
}
}
源程序供你参考可以改动