单片机解密按键
按键就完全是正常按键扫描代码的写法,注意这里消抖选用的三行按键消抖,主要是考虑到延时按键消抖可能会破坏掉LVGL整个框架的时基,所以使用的这种方式。
- // KEY1 PD8
- // KEY2 PD9
- // KEY3 PD10
- // KEY4 PD11
- // KEY5 PD12
- // KEY6 PD13
- #include "Key.h"
- uint8_t Key_Value,Key_Down,Key_Up,Key_Last;
- uint8_t Key_GetValue(void)
- {
- if(HAL_GPIO_ReadPin(GPIOD,KEY1_Pin) == 0)
- return 1;
- if(HAL_GPIO_ReadPin(GPIOD,KEY2_Pin) == 0)
- return 2;
- if(HAL_GPIO_ReadPin(GPIOD,KEY3_Pin) == 0)
- return 3;
- if(HAL_GPIO_ReadPin(GPIOD,KEY4_Pin) == 0)
- return 4;
- if(HAL_GPIO_ReadPin(GPIOD,KEY5_Pin) == 0)
- return 5;
- if(HAL_GPIO_ReadPin(GPIOD,KEY6_Pin) == 0)
- return 6;
- return 0;
- }
- void Key_RemoveShake(void)
- {
- Key_Value = Key_GetValue();//获取按下键值
- Key_Down = Key_Value & (Key_Value ^ Key_Last);//获取下降沿
- Key_Up = ~Key_Value & (Key_Value ^ Key_Last);//获取上升沿
- Key_Last = Key_Value;//键值覆盖
- }
- Key_Type Key_Press(void)
- {
- return Key_Down ? (Key_Type)Key_Value : 0;
- }