STM32入门教程-2022持续更新中

按键控制LED
关于uint8_t Key_GetNum(void)函数的理解和主函数中循环的理解:
uint8_t Key_GetNum(void)函数:
uint8_t Key_GetNum(void)
{
uint8_t KeyNum=0;
初始化KeyNum,赋值0
if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_1)==0)
PB1引脚设为上拉输入,默认高电平;PB1连接按键1,按键1接低电平;按下按键1,PB1变成低电平;
{
按键要一直按,一松手就弹回去了
Delay_ms(10);
消除按下按键1产生的抖动
while(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_1)==0);
一个空循环,一直按着按键1一直循环;结束循环的条件是松开按键1;结束循环
Delay_ms(10);
消除松开按键1产生的抖动
KeyNum = 1;
发生了一次按下又松开按键1的事件,KeyNum = 1
}
if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_11)==0);
PB11引脚设为上拉输入,默认高电平;PB11连接按键2,按键2接低电平;按下按键2,PB11变成低电平;
{
Delay_ms(10);
while(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_11)==0);
Delay_ms(10);
KeyNum = 2;
发生了一次按下又松开按键2的事件,KeyNum = 2
}
return KeyNum;
返回值可能是0(无事发生),1(对应LED1),2(对应LED2)
}
主函数中循环:
while(1)
{
KeyNum=Key_GetNum();
不停向Key_GetNum()索要返回值
if(KeyNum==1)
返回值1说明按下又松开一次按键1
{
LED1_TURN();
灯1的状态反转,原来是关的现在开了,原来是开的现在关了
}
if(KeyNum==2)
返回值2说明按下又松开一次按键2
{
LED2_TURN();
灯2的状态反转
}
}