使用扫描事件获取键值,该模块极简
任意取用
#define Keyboard_Line_1 GPIO_Pin_0
#define Keyboard_Line_2 GPIO_Pin_1
#define Keyboard_Line_3 GPIO_Pin_2
#define Keyboard_Line_4 GPIO_Pin_3
#define Keyboard_Row_1 GPIO_Pin_4
#define Keyboard_Row_2 GPIO_Pin_5
#define Keyboard_Row_3 GPIO_Pin_6
#define Keyboard_Row_4 GPIO_Pin_7
#define IOPIN(x) ((uint16_t)(1<<(x)))
#define DEBOUNCE_CNT 5 //20ms 默认防抖时间
uint16_t KeyValue ;
uint8_t Delay_Count[16];
// 功能:设置IO为推挽输出
// 输入:port, pin按位(可为组合)
void IoDirOutput(GPIO_TypeDef* port, uint16_t pin)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = pin;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(port, &GPIO_InitStructure);
}
// 功能:设置IO为下拉输入
// 输入:port, pin按位(可为组合)
void IoDirInputPD(GPIO_TypeDef *port, uint16_t pin)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = pin;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
GPIO_Init(port, &GPIO_InitStructure);
}
void IoInit(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD
| RCC_APB2Periph_GPIOE | RCC_APB2Periph_GPIOF | RCC_APB2Periph_GPIOG, ENABLE);
IoDirOutput( GPIOE, Keyboard_Line_1 | Keyboard_Line_2 |Keyboard_Line_3 |Keyboard_Line_4);
IoDirInputPD(GPIOE, Keyboard_Line_1 | Keyboard_Line_2 | Keyboard_Line_3 | Keyboard_Line_4);
}
void IoDetectEvent(void)
{
uint8_t i, j;
uint16_t buff_porte,porte;
IoSetPin( GPIOE, Keyboard_Row_1 | Keyboard_Row_2 | Keyboard_Row_3 | Keyboard_Row_4);
porte = IoRead(GPIOE);
for (i = 0; i =0 < 4; i++ ) //读取Line
{
if (porte & IOPIN(i))
{
for (j = 4;j < 7 ; j++) //读取Row
{
IoClrPin( GPIOE, Keyboard_Line_1 | Keyboard_Line_2 | Keyboard_Line_3 | Keyboard_Line_4);
IoSetPin( GPIOE, IOPIN(j));
buff_porte = IoRead(GPIOE);
if (buff_porte & IOPIN(i))
{
if (Delay_Count[i * 4 + (j - 3)] >= DEBOUNCE_CNT) //去抖动小延时 = DEBOUNCE_CNT*4ms
{
Delay_Count[i * 4 + (j - 3)] = 0;
KeyValue = i * 4 + (j - 3);//1~16
}
else
{
Delay_Count[i * 4 + (j - 3)] ++;
}
}
else
{
Delay_Count[i * 4 + (j - 3)] = 0;//清除误触计数
}
}
}
}
}
上一篇:ARM开发(4)基于STM32的矩阵键盘按键控制TM1629A LED显示
下一篇:STM32 矩阵键盘通过串口输出
推荐阅读最新更新时间:2024-11-04 16:44
设计资源 培训 开发板 精华推荐
- LTC6261IS6 230 uA 电源电流运算放大器的典型应用
- 60 W USB-PD和Quick Charge 3.0降压-升压参考设计文档包
- AD9144-EBZ,用于 AD9144 四通道、16 位、2.8 GSPS、TxDAC+ 数模转换器的评估板
- AD8616ARZ-REEL单电源缓冲网络运算放大器典型应用电路
- mini-pcie尺寸键盘主控板
- MIC2207, 1.2V, 2.5V, 3.3V DC to DC 多路输出有线网络电源
- 使用 Analog Devices 的 LTC1458LCG 的参考设计
- l6205pd
- 具有高阻抗输入源的 LT3971-5、4V 降压转换器的典型应用电路
- BusKill:用于笔记本电脑的 USB 消杀线