[【Mengxin STM32 Learning 20--Key Input Experiment】】

New STM32 learning 20 – key input experiment

After learning about the buzzer experiment, we conducted another small experiment, which was not explained in class by Punctual Atom.
Insert image description here
For this kind of non-smooth situation, we will also choose more methods to eliminate jitter.
Software debounce: there are many methods. We use the simplest delay debouncing in our routine. After detecting that a button is pressed, a 10ms delay is generally performed to skip the jitter period. If the debounce effect is not good, you can adjust this 10ms delay, because the jitter time of different types of buttons may vary. After the delay, the button status is detected. If it is not pressed, then we judge that it is caused by jitter or interference; if it is still pressed, then we think that the button is really pressed. The same applies to the judgment of button release.
Routine requirements:
Control the LED light and buzzer through three independent buttons on the development board: KEY_UP controls the buzzer to flip, KEY1 controls LED1 to flip, and KEY0 controls LED0/LED1 to flip at the same time.
This requires the use of the buzzer and LED control we described above, as well as the implementation of the newly added buttons.
Insert image description here
Here we can judge according to our previous logic.

Here we introduce several new functions
HAL_GPIO_ReadPin
to read the function of the pin
GPIO_PinState HAL_GPIO_ReadPin (GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin);
the first part refers to the number of GPIO
and the latter part refers to the pin number from 0 to any of 15

When we use GPIO, no matter which one we use, please enable the GPIO in that direction. It can be GPIOA or GPIOE, etc. __HAL_RCC_GPIOA_CLK_ENABLE
();
__HAL_RCC_GPIOE_CLK_ENABLE();

Flowchart
Insert image description here
Simple analysis of the code part
From the hardware design section, we know that KEY0, KEY1 and KEY_UP are connected to PE4, PE3 and PA0 respectively

Insert image description here
Insert image description here
Next, let's focus on analyzing the implementation of the scanning function.
Insert image description here
In fact, it means other
means KEY00 KEY10 is valid at low level,
WK_UP==1 is valid at high level.
Then many people are not aware of this static. In fact, static is a static variable that can only be initialized once. The
remaining mode is 1. The following means key_up is always released to receive data anytime and anywhere. The change of
priority is explained in this way.
My wk_up is at the bottom. The sequential execution of the program will make the bottom one have the highest priority. If you think so, I will not transfer the data until the end, even if I have modified the data before. But in the end, I have repeatedly closed and revised the conditions, isn't it still a credit to WK_up?

Below is main.c

int main(void){
    
     uint8_t key; HAL_Init(); /* 初始化 HAL 库 */ sys_stm32_clock_init(RCC_PLL_MUL9); /* 设置时钟
, 72Mhz */ delay_init(72); 
/* 延时初始化 */ led_init(); /* 初始化 LED */ beep_init(); /* 初始化蜂鸣器 */ key_init();
 /* 初始化按键 */ LED0(0);
 /* 先点亮 LED0 */  
while(1) {
    
     key = key_scan(0); /* 得到键值 */
 if (key) 
{
    
     switch (key) 
{
    
     case WKUP_PRES: /* 控制蜂鸣器 */ BEEP_TOGGLE(); /* BEEP 状态取反 */ 
break; case KEY1_PRES: /* 控制 LED1(GREEN)翻转 */ 
LED1_TOGGLE(); /* LED1 状态取反 */ 
break;
 case KEY0_PRES: 
/* 同时控制 LED0, LED1 翻转 */ LED0_TOGGLE();
 /* LED0 状态取反 */ LED1_TOGGLE();
 /* LED1 状态取反 */ break; } } 
else {
    
     delay_ms(10); } }}

Guess you like

Origin blog.csdn.net/weixin_50965981/article/details/132566041