STM32 按键
建工程就不再赘述。第一篇博客《STM32点亮LED》有详细说明。直接上关键代码--其实也就几行代码。
int main(int argc, char **argv){
delay_init();
LED_Init();
while (1)
{
//有锁存
if (GPIO_ReadInputDataBit(GPIOB, KEY_0)) {//检测是否按下
delay_ms(20);//消抖
if (GPIO_ReadInputDataBit(GPIOB, KEY_0)) {
GPIO_WriteBit(GPIOB, LED,(BitAction) (1-GPIO_ReadOutputDataBit(GPIOB, LED)));//翻转
while (GPIO_ReadInputDataBit(GPIOB, KEY_0));//等待按键松开
}
}
}
}
//无锁存
if (!GPIO_ReadInputDataBit(GPIOB, KEY_0)) {
GPIO_WriteBit(GPIOB, LED, (BitAction) 0);
} else {
GPIO_WriteBit(GPIOB, LED, (BitAction) 1);
}