STM32矩阵键盘——行列扫描编码模式
生活随笔
收集整理的這篇文章主要介紹了
STM32矩阵键盘——行列扫描编码模式
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
不同于之前一篇,這篇就是走常規(guī)路子的方法了,
KEY.C
#include "Key.h"/********************************配置IO為上拉輸入********************************************/ #define configInputPullUp(port, pin, GPIO_InitStruct) { \ /* HAL_GPIO_WritePin(port, pin, GPIO_PIN_RESET); */ \(GPIO_InitStruct)->Pin = pin ; \(GPIO_InitStruct)->Mode = GPIO_MODE_INPUT ; \(GPIO_InitStruct)->Pull = GPIO_PULLUP ; \(GPIO_InitStruct)->Speed = GPIO_SPEED_FREQ_LOW; \HAL_GPIO_Init(port, GPIO_InitStruct) ; \ }/********************************配置IO為推挽輸出低電平********************************************/ #define configOutputLow(port, pin, GPIO_InitStruct) { \(GPIO_InitStruct)->Pin = pin ; \(GPIO_InitStruct)->Mode = GPIO_MODE_OUTPUT_PP ; \(GPIO_InitStruct)->Pull = GPIO_NOPULL ; \(GPIO_InitStruct)->Speed = GPIO_SPEED_FREQ_LOW; \HAL_GPIO_Init(port, GPIO_InitStruct) ; \HAL_GPIO_WritePin(port, pin, GPIO_PIN_RESET); \ }void rowScanningEnter() {GPIO_InitTypeDef GPIO_InitStruct;configInputPullUp(ROW3_GPIO_Port,ROW3_Pin,&GPIO_InitStruct);configInputPullUp(ROW2_GPIO_Port,ROW2_Pin,&GPIO_InitStruct);configInputPullUp(ROW1_GPIO_Port,ROW1_Pin,&GPIO_InitStruct);configInputPullUp(ROW0_GPIO_Port,ROW0_Pin,&GPIO_InitStruct);configOutputLow(COL3_GPIO_Port,COL3_Pin,&GPIO_InitStruct);configOutputLow(COL2_GPIO_Port,COL2_Pin,&GPIO_InitStruct);configOutputLow(COL1_GPIO_Port,COL1_Pin,&GPIO_InitStruct);configOutputLow(COL0_GPIO_Port,COL0_Pin,&GPIO_InitStruct); }void colScanningEnter() {GPIO_InitTypeDef GPIO_InitStruct;configOutputLow(ROW3_GPIO_Port,ROW3_Pin,&GPIO_InitStruct);configOutputLow(ROW2_GPIO_Port,ROW2_Pin,&GPIO_InitStruct);configOutputLow(ROW1_GPIO_Port,ROW1_Pin,&GPIO_InitStruct);configOutputLow(ROW0_GPIO_Port,ROW0_Pin,&GPIO_InitStruct);configInputPullUp(COL3_GPIO_Port,COL3_Pin,&GPIO_InitStruct);configInputPullUp(COL2_GPIO_Port,COL2_Pin,&GPIO_InitStruct);configInputPullUp(COL1_GPIO_Port,COL1_Pin,&GPIO_InitStruct);configInputPullUp(COL0_GPIO_Port,COL0_Pin,&GPIO_InitStruct);}u8 KEY_SCAN(void) {u8 Key_ROW = 0,Key_COL = 0,Key_Read = 0;u8 key;rowScanningEnter();Key_ROW = (u8)( (((u8)DETECT_R0) << 3) |(((u8)DETECT_R1) << 2) |(((u8)DETECT_R2) << 1) |((u8)DETECT_R3) );Key_ROW&=0x0f;if(Key_ROW != 0x0f){HAL_Delay(10);Key_ROW = (u8)( (((u8)DETECT_R0) << 3) |(((u8)DETECT_R1) << 2) |(((u8)DETECT_R2) << 1) |((u8)DETECT_R3) );Key_ROW&=0x0f;if(Key_ROW != 0x0f){colScanningEnter();Key_COL = (u8)( (((u8)DETECT_C0) << 3) |(((u8)DETECT_C1) << 2) |(((u8)DETECT_C2) << 1) |((u8)DETECT_C3) );Key_Read = (Key_ROW << 4) | Key_COL;switch(Key_Read){//第1行case 0x77: key = 1; break;case 0x7b: key = 2; break;case 0x7d: key = 3; break;case 0x7e: key = 4; break;//第2行case 0xb7: key = 5; break;case 0xbb: key = 6; break;case 0xbd: key = 7; break;case 0xbe: key = 8; break;//第3行 case 0xd7: key = 9; break;case 0xdb: key = 10; break;case 0xdd: key = 11; break;case 0xde: key = 12; break;//第4行case 0xe7: key = 13; break;case 0xeb: key = 14; break;case 0xed: key = 15; break;case 0xee: key = 16; break;default : break;}while(Key_COL != 0x0f) //檢測到按鍵松開了{Key_COL = (u8)( (((u8)DETECT_C0) << 3) |(((u8)DETECT_C1) << 2) |(((u8)DETECT_C2) << 1) |((u8)DETECT_C3) );}return key;}}return 0; }KEY.H
#ifndef __KEY_H__ #define __KEY_H__#include "main.h" #include "gpio.h"#define DETECT_R0 HAL_GPIO_ReadPin(ROW0_GPIO_Port,ROW0_Pin) #define DETECT_R1 HAL_GPIO_ReadPin(ROW1_GPIO_Port,ROW1_Pin) #define DETECT_R2 HAL_GPIO_ReadPin(ROW2_GPIO_Port,ROW2_Pin) #define DETECT_R3 HAL_GPIO_ReadPin(ROW3_GPIO_Port,ROW3_Pin) #define DETECT_C0 HAL_GPIO_ReadPin(COL0_GPIO_Port,COL0_Pin) #define DETECT_C1 HAL_GPIO_ReadPin(COL1_GPIO_Port,COL1_Pin) #define DETECT_C2 HAL_GPIO_ReadPin(COL2_GPIO_Port,COL2_Pin) #define DETECT_C3 HAL_GPIO_ReadPin(COL3_GPIO_Port,COL3_Pin) u8 KEY_SCAN(void);#endif在main中加這個即可。
Key_Value = KEY_SCAN();if(Key_Value!=0)printf("%d \r\n",Key_Value);bug調(diào)試
1.不要手碰到矩陣鍵盤底部,否則一通混亂
2.按鍵消抖部分
最開始是這樣的
3.跟上一篇的方法一樣的問題是,都不能檢測按鍵同時按下,比如同時按兩個,你能在行讀取的值中有兩個0,列讀取的值中,兩個0,而且還不固定,這樣就無法讀取。
總結(jié)
以上是生活随笔為你收集整理的STM32矩阵键盘——行列扫描编码模式的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 离职原因任意说
- 下一篇: 【U8】UFO另存为Excel,文件保存