短按(按下持續(xù)時間小于3s)一次數(shù)碼管顯示加1,同時流水燈計數(shù)加1。
長按(按下持續(xù)時間大于3s)時,數(shù)碼管顯示每500ms加1,同時流水燈每500ms計數(shù)加1。
#include<reg52.h>
typedef unsigned char uchar;
typedef unsigned int uint;
#define NOKEY 0
#define KEYDOWN 1
#define KEYUP 2
uchar keystate;
sbit key=P2^7;
void keyscan(void);
uchar timeflag;
uchar code table[]=
{
0x80,0xf2,0x48,0x60,
0x32,0x24,0x04,0xf0,
0x00,0x20,0x10,0x06,
0x8c,0x42,0x0c,0x1c
};
uchar i;
void main(void)
{
TMOD=0x10;
TH1=(65536-50000)/256;
TL1=(65536-50000)%256;
EA=1;
ET1=1;
TR1=1;
while(1)
{
if(timeflag)
{
timeflag=0;
keyscan();
}
}
}
void keyscan(void)
{
static uchar iscontinue=0;
P2 |=0x80;
switch(keystate)
{
case NOKEY:
if(key==0)
{
keystate=KEYDOWN;
return;
}
else
{
keystate=NOKEY;
return;
}
case KEYDOWN:
if(key==0)
{
keystate=KEYUP;
return;
}
else
{
keystate=NOKEY;
return;
}
case KEYUP:
if(key==0)
{
keystate=KEYUP;
iscontinue++;
if(iscontinue==255)
iscontinue=60;
if(iscontinue>=60)
{
if(iscontinue%10==0)
{
P2+=1;
P1=table[i++];
if(i>=16)
i=0;
}
}
return;
}
else
{
keystate=NOKEY;
if(iscontinue<60)
{
P2+=1;
P1=table[i++];
if(i>=16)
i=0;
}
iscontinue=0;
return;
}
}
}
void timer1isr() interrupt 3
{
TH1=(65536-50000)/256;
TL1=(65536-50000)%256;
timeflag=1;
}