51单片机(AT89C52)一键多功能(短按、长按)
时间:04-08 11:30 阅读:1248次
*温馨提示:点击图片可以放大观看高清大图
简介:短按(按下持续时间小于3s)一次数码管显示加1,同时流水灯计数加1。长按(按下持续时间大于3s)时,数码管显示每500ms加1,同时流水灯每500ms计数加1。
#include
typedefunsignedcharuchar;
typedefunsignedintuint;
#defineNOKEY0
#defineKEYDOWN1
#defineKEYUP2
ucharkeystate;
sbitkey=P2^7;
voidkeyscan(void);
uchartimeflag;
ucharcodetable[]=
{
0x80,0xf2,0x48,0x60,
0x32,0x24,0x04,0xf0,
0x00,0x20,0x10,0x06,
0x8c,0x42,0x0c,0x1c
};
uchari;
voidmain(void)
{
TMOD=0x10;
TH1=(65536-50000)/256;
TL1=(65536-50000)%256;
EA=1;
ET1=1;
TR1=1;
while(1)
{
if(timeflag)
{
timeflag=0;
keyscan();
}
}
}
voidkeyscan(void)
{
staticuchariscontinue=0;
P2|=0x80;
switch(keystate)
{
caseNOKEY:
if(key==0)
{
keystate=KEYDOWN;
return;
}
else
{
keystate=NOKEY;
return;
}
caseKEYDOWN:
if(key==0)
{
keystate=KEYUP;
return;
}
else
{
keystate=NOKEY;
return;
}
caseKEYUP:
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;
}
}
}
voidtimer1isr()interrupt3
{
TH1=(65536-50000)/256;
TL1=(65536-50000)%256;
timeflag=1;
}