下面只是思想,函数名都是随便起的:
int Timer1ms; //时间计数
sbit KEY = P0^1; //按键
char myKeyZhi = 0;//按键消抖过后的值
void timer_inter_1ms()//假设这个是定时器中断函数
{
Timer1ms++;
}
void ReadKey(int timecnt)//返回按键的值
{
if(myKeyZhi==KEY)Timer1ms=0;
if(Timer1ms>=timecnt)
{
myKeyZhi=KEY;
}
}
void main()
{
//初始化 略
while(1)
{
ReadKey(3000);//3秒
}
}