标签:HAL G431RBTX PIN Up Down Key 按键 GPIO 识别
uint8_t Key_Scan(void) //按键扫描,获取按键按下的键值 { uint8_t Key_val = 0; if(HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_0) == GPIO_PIN_RESET) { HAL_Delay(10); if(HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_0) == GPIO_PIN_RESET) Key_val = 1; } else if(HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_1) == GPIO_PIN_RESET) { HAL_Delay(10); if(HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_1) == GPIO_PIN_RESET) Key_val = 2; } else if(HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_2) == GPIO_PIN_RESET) { HAL_Delay(10); if(HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_2) == GPIO_PIN_RESET) Key_val = 3; } else if(HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0) == GPIO_PIN_RESET) { HAL_Delay(10); if(HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0) == GPIO_PIN_RESET) Key_val = 4; } return Key_val; }
void Key_Proc(void) //按键服务程序,获取按键状态(上升沿,下降沿,长按,短按) {
static _Bool Key_Down_Flag = 0; static _Bool Key_Up_Flag = 0; Key_Tick = uwTick; uint8_t Key_L_Down = 0; uint32_t Key_Plus_Tick = 0; static uint32_t Key_Up_Tick_Start = 0; uint32_t Key_Up_Tick_End = 0; uint8_t Key_Value = Key_Scan(); uint8_t Key_S_Down = 0; static uint8_t Key_Down = 0; static uint8_t Key_Up = 0; static uint8_t Key_Old = 0; Key_Down = Key_Value & (Key_Value ^ Key_Old); //获取按键上升沿,下降沿 Key_Up = ~Key_Value & (Key_Value ^ Key_Old); Key_Old = Key_Value; if(Key_Down) //判断长按,短按 { Key_Down_Flag = 1; Key_Up_Tick_Start = uwTick; } if(Key_Up) { Key_Down_Flag = 0; }S if(Key_Down_Flag || Key_Up) //key_S_Down为按键短按——Short { if((uwTick - Key_Up_Tick_Start) < 1000) Key_S_Down = Key_Up; else Key_L_Down = Key_Value;//key_S_Down为按键长按——Long
}
}
PS:第一次发随笔,代码中很多地方都是跟其他大佬学习,如有侵权,请及时联系,多多包涵
标签:HAL,G431RBTX,PIN,Up,Down,Key,按键,GPIO,识别 来源: https://www.cnblogs.com/ffdbb2022/p/16112119.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。