单片机解密HOR1-HOR4连接到单片机的输入引脚上去;LON1-LON4连接到单片机的开漏输出引脚上去,注意这4个引脚必须设置为开漏模式!
程序上首先将LON1所连接的IO输出低电平其余3个IO输出高电平,同时检测HOR1-HOR4的电平来获取K1-K4的按键状态;然后将LON2所连接的IO输出低电平其余3个IO输出高电平,同时检测HOR1-HOR4的电平来获取K5-K8的按键状态;依次类推。单片机代码时间
- 单片机解密#ifndef __Key_matrix_H
- 单片机解密#define __Key_matrix_H
- #include "gpio.h"
- #include "gpio_bool.h"
- /*务必把这4个输出IO设置为上拉输入*/
- #define KEY_HOR1 PAin(7)
- #define KEY_HOR2 PAin(6)
- #define KEY_HOR3 PAin(5)
- #define KEY_HOR4 PAin(4)
- /*务必把这4个输出IO设置为开漏*/
- #define KEY_LON1 PBout(0)
- #define KEY_LON2 PCout(5)
- #define KEY_LON3 PCout(4)
- #define KEY_LON4 PCout(3)
- #define KEY_PRESS_TIME 20//消抖常数
- #define KEY_LONG_PRESS_TIME 3000//单个按键长按阈值3s
- /*通过读取(只读)这三个变量即可获得按键的单按、长按和组合键信息*/
- extern volatile uint16_t Key_Phy_Num;
- extern volatile uint8_t Key_Pulse_Num;
- extern volatile uint16_t Key_LP_Num;
- typedef enum
- {
- KPL_DISABLE=0,
- KPL_ENABLE
- }K_L_P;//按键的长按状态
- typedef struct
- {
- K_L_P KEY_LONG_PRESS;
- uint16_t KeyOpenCount;
- uint8_t KOC_EN;
- uint16_t KeyCloseCount;
- uint8_t KCC_EN;
- }Key_Para;
- exter Key_Par Key_1,Key_2,Key_3,Key_4,Key_5,Key_6,Key_7,Key_8,Key_9,Key_10,Key_11,Key_12,Key_13,Key_14,Key_15,Key_16;
- void Clear_Key_Pulse_Num(void);//当读取完Key_Pulse_Num后调用
- void KeyCount_Run(void);//在1ms滴答里调用
- void Key_Scan(void);//大循环或者滴答里边都行
- #endif