写了一个响应单按键多击的程序,按键弹起大于0.5S后返回键值,有问题,无法运行。可是我才疏学浅,找不到问题出在哪里。请各位高人帮忙看看问题出在哪?
#include <reg52.h>
sbit LED0 = P1^0;
sbit LED1 = P1^1;
sbit LED2 = P1^2;
sbit KEY1 = P3^2;
unsigned int count = 0,ClickFlag = 0,ClickFlagCount = 0,LongClickFlag = 0;
unsigned char i = 0,Click = 0;
unsigned char keynum = 0;
unsigned char keyscan(void)
{
if(KEY1)
{
if(count > 30 && count < 3000)
{
ClickFlag = 1;
keynum++;
}
count = 0;
if(ClickFlag == 1)
{
if(ClickFlagCount++ > 500)
{
ClickFlag = 0;
ClickFlagCount = 0;
return keynum;
keynum = 0;
}
else
{
ClickFlag = 0;
ClickFlagCount = 0;
}
}
else
{
ClickFlagCount = 0;
}
}
else
{
count++;
if(count > 3000)
count = 3001;
}
return 0;
}
void main()
{
EA = 1; //使能总中断
TMOD = 0x01; //设置T0为模式1
TH0 = 0xFC; //为T0赋初值0xFC67,定时1ms
TL0 = 0x67;
ET0 = 1; //使能T0中断
TR0 = 1; //启动T0
while (1)
{
if(i==1)
LED0 = ~LED0;
if(i==2)
LED1 = ~LED1;
if(i==3)
LED2 = ~LED2;
}
}
/* 定时器0中断服务函数 */
void InterruptTimer0() interrupt 1
{
TH0 = 0xFC; //重新加载初值
TL0 = 0x67;
i = keyscan();
} |