uint8_t Key_GetNum(void)
{
uint8_t KeyNum = 0;
if (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_1) == 0)
{
Delay_ms(20);
while (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_1) == 0);
Delay_ms(20);
KeyNum = 1;
}
if (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_11) == 0)
{
Delay_ms(20);
while (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_11) == 0);
Delay_ms(20);
KeyNum = 2;
}
return KeyNum;
}
Delay_ms(20);
while (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_11) == 0);
Delay_ms(20);
我不了解为什么delay后,为什么还要进行判断,判断后为什么还要delay消抖。
刚才又看了视频,懂了。
if (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_1) == 0)
{
Delay_ms(20); //刚进去需要消抖
while (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_1) == 0);
//我们按键结果是在按键松手后才出现,不松手就一直死循环,不变。
Delay_ms(20);//按键结束后还需要销一下抖
KeyNum = 1;
}