hc32l130 4x5矩阵键盘实现逻辑,gpio中断方式代码实现逻辑
时间: 2024-03-28 18:36:48 浏览: 22
以下是HC32L130芯片的4x5矩阵键盘的GPIO中断方式的实现逻辑,供参考:
1. 配置行为普通输出,列为中断输入
2. 配置GPIO中断的触发条件为上升沿或下降沿
3. 在GPIO中断服务函数中,轮询行和列的电平状态,确定按键的位置和值
4. 处理按键按下的操作
其中,需要注意以下几点:
1. 行和列的GPIO口需要分别进行配置
2. 在GPIO中断服务函数中,需要先将行的GPIO口配置为输出状态,并将所有行的GPIO口都拉低,然后再逐个将行的GPIO口拉高,检测列的电平状态,判断按键的位置和值
3. 处理按键按下的操作时,可以采用中断标志位或者计时器等方式,避免连续触发按键事件
相关问题
hc32l130 4x5矩阵键盘实现逻辑,gpio中断方式代码实现
以下是基于HC32L130芯片的4x5矩阵键盘的GPIO中断方式的代码实现,仅供参考:
```c
#include "hc32l130.h"
#define COL_NUM 5
#define ROW_NUM 4
uint8_t key_table[ROW_NUM][COL_NUM] = {
{1, 2, 3, 4, 5},
{6, 7, 8, 9, 10},
{11, 12, 13, 14, 15},
{16, 17, 18, 19, 20}
};
void GPIO_PA_IRQHandler(void)
{
uint8_t row, col, key_value;
for (row = 0; row < ROW_NUM; row++) {
GPIO_SetPinsAsOutput(GPIO_PORT_1, GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3);
GPIO_SetPinValue(GPIO_PORT_1, GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3, GPIO_PIN_RESET);
GPIO_SetPinValue(GPIO_PORT_1, GPIO_PIN_0 + row, GPIO_PIN_SET);
for (col = 0; col < COL_NUM; col++) {
if (GPIO_GetPinValue(GPIO_PORT_1, GPIO_PIN_4 + col) == GPIO_PIN_RESET) {
key_value = key_table[row][col];
// 处理按键按下的操作
break;
}
}
}
GPIO_ClearPinsInterrupt(GPIO_PORT_1, GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3);
}
void init_keypad(void)
{
// 配置行为普通输出
GPIO_SetPinsAsOutput(GPIO_PORT_1, GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3);
// 配置列为中断输入
GPIO_SetPinsAsInput(GPIO_PORT_1, GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_7);
GPIO_SetPinInterrupt(GPIO_PORT_1, GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_7, GPIO_INT_RISING_EDGE);
// 初始化GPIO中断
NVIC_ClearPendingIRQ(GPIO_PA_IRQn);
NVIC_SetPriority(GPIO_PA_IRQn, 3);
NVIC_EnableIRQ(GPIO_PA_IRQn);
}
int main(void)
{
init_keypad();
while (1) {
// 不需要轮询读取列的电平,GPIO中断会自动检测
}
}
```
上述代码中,我们通过GPIO口的输入和输出功能,配合GPIO中断方式,实现了4x5矩阵键盘的按键检测和处理功能。其中,中断服务函数用于检测列的变化,不需要轮询函数。GPIO中断方式相比外部中断方式,代码更加简洁,但需要注意GPIO口的配置和中断触发条件的设置。
hc32l130 4x5矩阵键盘实现逻辑,中断方式代码实现
以下是基于HC32L130芯片的4x5矩阵键盘的中断方式的代码实现,仅供参考:
```c
#include "hc32l130.h"
#define COL_NUM 5
#define ROW_NUM 4
uint8_t key_table[ROW_NUM][COL_NUM] = {
{1, 2, 3, 4, 5},
{6, 7, 8, 9, 10},
{11, 12, 13, 14, 15},
{16, 17, 18, 19, 20}
};
void EXTI_PA_IRQHandler(void)
{
uint8_t row, col, key_value;
for (row = 0; row < ROW_NUM; row++) {
GPIO_SetPinsAsInput(GPIO_PORT_1, GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3);
GPIO_SetPinValue(GPIO_PORT_1, GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3, GPIO_PIN_RESET);
GPIO_SetPinValue(GPIO_PORT_1, GPIO_PIN_0 + row, GPIO_PIN_SET);
for (col = 0; col < COL_NUM; col++) {
if (GPIO_GetPinValue(GPIO_PORT_1, GPIO_PIN_4 + col) == GPIO_PIN_RESET) {
key_value = key_table[row][col];
// 处理按键按下的操作
break;
}
}
}
EXTI_ClearFlag(EXTI_CH00);
}
void init_keypad(void)
{
// 配置行为中断输入
GPIO_SetPinsAsInput(GPIO_PORT_1, GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3);
GPIO_SetPinInterrupt(GPIO_PORT_1, GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3, GPIO_INT_RISING_EDGE);
// 配置列为轮询输入
GPIO_SetPinsAsInput(GPIO_PORT_1, GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_7);
// 初始化外部中断
NVIC_ClearPendingIRQ(EXTI_PA_IRQn);
NVIC_SetPriority(EXTI_PA_IRQn, 3);
NVIC_EnableIRQ(EXTI_PA_IRQn);
}
int main(void)
{
init_keypad();
while (1) {
// 轮询读取列的电平,确定按下的键值
uint8_t row, col, key_value;
for (row = 0; row < ROW_NUM; row++) {
GPIO_SetPinsAsInput(GPIO_PORT_1, GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3);
GPIO_SetPinValue(GPIO_PORT_1, GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3, GPIO_PIN_RESET);
GPIO_SetPinValue(GPIO_PORT_1, GPIO_PIN_0 + row, GPIO_PIN_SET);
for (col = 0; col < COL_NUM; col++) {
if (GPIO_GetPinValue(GPIO_PORT_1, GPIO_PIN_4 + col) == GPIO_PIN_RESET) {
key_value = key_table[row][col];
// 处理按键按下的操作
break;
}
}
}
}
}
```
上述代码中,我们通过GPIO口的输入和输出功能,配合外部中断和轮询方式,实现了4x5矩阵键盘的按键检测和处理功能。其中,中断服务函数用于检测行的变化,轮询函数用于检测列的变化,两者结合起来可以实现完整的按键检测和处理流程。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)