我用定时器写的扫描,每个10ms扫描一次按键,我在以下里面计数,发现计数自动增加,不知该怎么处理?求高手教以下如何实现按键计数,
void TIM2_IRQHandler(void)
{
u8 temp;
if ( TIM_GetITStatus(TIM2 , TIM_IT_Update) != RESET )
{
if(GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_13) == Bit_RESET){
temp = 0;
}
else{
temp = 1;
}
KeyPrev = KeyCurrent;
KeyCurrent = temp;
if((KeyPrev == 0) && (KeyCurrent == 0)){
KeyStatus = 1;
KeyCount++;
}
else{
KeyStatus = 0;
}
TIM_ClearITPendingBit(TIM2 , TIM_FLAG_Update);
}
}
在main文件这样写
while(1)
{
if ( KeyStatus == 0 )
{ switch(KeyCount)
{ case 0:function1();
break;
case 1:function2();
break;
default: function3();
break;
}
} |