hc32l13x 4x5矩阵键盘,使用9个gpio口,3个数码管,每个按键带灯,每个灯使用数码管一个led,给出示例
时间: 2023-11-04 12:05:41 浏览: 113
以下是一个基于hc32l13x的4x5矩阵键盘的示例代码,使用9个GPIO口和3个数码管,每个按键带灯,每个灯使用数码管一个led。
```c
#include "hc32l13x.h"
#define KEY_PORT GpioPortC
#define KEY_PIN_NUM GpioPin5
#define ROW1_PORT GpioPortC
#define ROW1_PIN_NUM GpioPin0
#define ROW2_PORT GpioPortC
#define ROW2_PIN_NUM GpioPin1
#define ROW3_PORT GpioPortC
#define ROW3_PIN_NUM GpioPin2
#define ROW4_PORT GpioPortC
#define ROW4_PIN_NUM GpioPin3
#define COL1_PORT GpioPortC
#define COL1_PIN_NUM GpioPin6
#define COL2_PORT GpioPortC
#define COL2_PIN_NUM GpioPin7
#define COL3_PORT GpioPortC
#define COL3_PIN_NUM GpioPin8
#define COL4_PORT GpioPortC
#define COL4_PIN_NUM GpioPin9
#define DIG1_PORT GpioPortA
#define DIG1_PIN_NUM GpioPin0
#define DIG2_PORT GpioPortA
#define DIG2_PIN_NUM GpioPin1
#define DIG3_PORT GpioPortA
#define DIG3_PIN_NUM GpioPin2
#define LED1_PORT GpioPortB
#define LED1_PIN_NUM GpioPin0
#define LED2_PORT GpioPortB
#define LED2_PIN_NUM GpioPin1
#define LED3_PORT GpioPortB
#define LED3_PIN_NUM GpioPin2
#define LED4_PORT GpioPortB
#define LED4_PIN_NUM GpioPin3
#define LED5_PORT GpioPortB
#define LED5_PIN_NUM GpioPin4
#define LED6_PORT GpioPortB
#define LED6_PIN_NUM GpioPin5
#define LED7_PORT GpioPortB
#define LED7_PIN_NUM GpioPin6
#define LED8_PORT GpioPortB
#define LED8_PIN_NUM GpioPin7
#define LED9_PORT GpioPortB
#define LED9_PIN_NUM GpioPin8
/* 按键扫描状态 */
typedef enum
{
KEY_STATE_IDLE, /* 空闲状态 */
KEY_STATE_DOWN, /* 按键按下状态 */
KEY_STATE_HOLD /* 按键持续按下状态 */
} key_state_t;
/* 按键扫描结构体 */
typedef struct
{
uint8_t row; /* 行 */
uint8_t col; /* 列 */
key_state_t state; /* 状态 */
uint32_t time; /* 按下时间 */
} key_scan_t;
/* 数码管显示状态 */
typedef enum
{
DIG_STATE_OFF, /* 关闭 */
DIG_STATE_ON /* 打开 */
} dig_state_t;
/* 数码管结构体 */
typedef struct
{
dig_state_t dig1; /* 数码管1 */
dig_state_t dig2; /* 数码管2 */
dig_state_t dig3; /* 数码管3 */
} dig_t;
/* LED状态 */
typedef enum
{
LED_STATE_OFF, /* 关闭 */
LED_STATE_ON /* 打开 */
} led_state_t;
/* LED结构体 */
typedef struct
{
led_state_t led1; /* LED1 */
led_state_t led2; /* LED2 */
led_state_t led3; /* LED3 */
led_state_t led4; /* LED4 */
led_state_t led5; /* LED5 */
led_state_t led6; /* LED6 */
led_state_t led7; /* LED7 */
led_state_t led8; /* LED8 */
led_state_t led9; /* LED9 */
} led_t;
key_scan_t key_scan[4][5] = {0}; /* 按键扫描数组 */
dig_t dig = {0}; /* 数码管结构体 */
led_t led = {0}; /* LED结构体 */
/* GPIO初始化 */
void gpio_init(void)
{
stc_gpio_config_t gpio_config;
/* 按键 */
gpio_config.enDir = GpioDirIn;
gpio_config.enDrv = GpioDrvL;
gpio_config.enPu = GpioPuEnable;
gpio_config.enPd = GpioPdDisable;
Gpio_Init(KEY_PORT, KEY_PIN_NUM, &gpio_config);
/* 行1 */
gpio_config.enDir = GpioDirOut;
gpio_config.enDrv = GpioDrvH;
gpio_config.enPu = GpioPuDisable;
gpio_config.enPd = GpioPdDisable;
Gpio_Init(ROW1_PORT, ROW1_PIN_NUM, &gpio_config);
/* 行2 */
Gpio_Init(ROW2_PORT, ROW2_PIN_NUM, &gpio_config);
/* 行3 */
Gpio_Init(ROW3_PORT, ROW3_PIN_NUM, &gpio_config);
/* 行4 */
Gpio_Init(ROW4_PORT, ROW4_PIN_NUM, &gpio_config);
/* 列1 */
gpio_config.enDir = GpioDirOut;
gpio_config.enDrv = GpioDrvH;
gpio_config.enPu = GpioPuDisable;
gpio_config.enPd = GpioPdDisable;
Gpio_Init(COL1_PORT, COL1_PIN_NUM, &gpio_config);
/* 列2 */
Gpio_Init(COL2_PORT, COL2_PIN_NUM, &gpio_config);
/* 列3 */
Gpio_Init(COL3_PORT, COL3_PIN_NUM, &gpio_config);
/* 列4 */
Gpio_Init(COL4_PORT, COL4_PIN_NUM, &gpio_config);
/* 数码管1 */
gpio_config.enDir = GpioDirOut;
gpio_config.enDrv = GpioDrvH;
gpio_config.enPu = GpioPuDisable;
gpio_config.enPd = GpioPdDisable;
Gpio_Init(DIG1_PORT, DIG1_PIN_NUM, &gpio_config);
/* 数码管2 */
Gpio_Init(DIG2_PORT, DIG2_PIN_NUM, &gpio_config);
/* 数码管3 */
Gpio_Init(DIG3_PORT, DIG3_PIN_NUM, &gpio_config);
/* LED1 */
gpio_config.enDir = GpioDirOut;
gpio_config.enDrv = GpioDrvH;
gpio_config.enPu = GpioPuDisable;
gpio_config.enPd = GpioPdDisable;
Gpio_Init(LED1_PORT, LED1_PIN_NUM, &gpio_config);
/* LED2 */
Gpio_Init(LED2_PORT, LED2_PIN_NUM, &gpio_config);
/* LED3 */
Gpio_Init(LED3_PORT, LED3_PIN_NUM, &gpio_config);
/* LED4 */
Gpio_Init(LED4_PORT, LED4_PIN_NUM, &gpio_config);
/* LED5 */
Gpio_Init(LED5_PORT, LED5_PIN_NUM, &gpio_config);
/* LED6 */
Gpio_Init(LED6_PORT, LED6_PIN_NUM, &gpio_config);
/* LED7 */
Gpio_Init(LED7_PORT, LED7_PIN_NUM, &gpio_config);
/* LED8 */
Gpio_Init(LED8_PORT, LED8_PIN_NUM, &gpio_config);
/* LED9 */
Gpio_Init(LED9_PORT, LED9_PIN_NUM, &gpio_config);
}
/* 按键扫描 */
void key_scan_task(void)
{
static uint8_t row = 0;
uint8_t col;
/* 关闭所有行 */
Gpio_SetIO(ROW1_PORT, ROW1_PIN_NUM, GpioPinReset);
Gpio_SetIO(ROW2_PORT, ROW2_PIN_NUM, GpioPinReset);
Gpio_SetIO(ROW3_PORT, ROW3_PIN_NUM, GpioPinReset);
Gpio_SetIO(ROW4_PORT, ROW4_PIN_NUM, GpioPinReset);
/* 切换到下一行 */
row = (row + 1) % 4;
switch(row)
{
case 0:
Gpio_SetIO(ROW1_PORT, ROW1_PIN_NUM, GpioPinSet);
break;
case 1:
Gpio_SetIO(ROW2_PORT, ROW2_PIN_NUM, GpioPinSet);
break;
case 2:
Gpio_SetIO(ROW3_PORT, ROW3_PIN_NUM, GpioPinSet);
break;
case 3:
Gpio_SetIO(ROW4_PORT, ROW4_PIN_NUM, GpioPinSet);
break;
default:
break;
}
/* 检测按键状态 */
for(col = 0; col < 5; col++)
{
if(Gpio_GetIO(KEY_PORT, KEY_PIN_NUM) == 0)
{
/* 按键按下 */
if(key_scan[row][col].state == KEY_STATE_IDLE)
{
key_scan[row][col].row = row;
key_scan[row][col].col = col;
key_scan[row][col].state = KEY_STATE_DOWN;
key_scan[row][col].time = SysTick_GetTickCount();
}
else if(key_scan[row][col].state == KEY_STATE_DOWN)
{
/* 按键持续按下 */
if(SysTick_GetTickCount() - key_scan[row][col].time >= 1000)
{
key_scan[row][col].state = KEY_STATE_HOLD;
}
}
}
else
{
/* 按键松开 */
if(key_scan[row][col].state != KEY_STATE_IDLE)
{
key_scan[row][col].state = KEY_STATE_IDLE;
}
}
}
}
/* 数码管显示 */
void dig_display_task(void)
{
static uint8_t dig_num = 1;
/* 关闭所有数码管 */
Gpio_SetIO(DIG1_PORT, DIG1_PIN_NUM, GpioPinReset);
Gpio_SetIO(DIG2_PORT, DIG2_PIN_NUM, GpioPinReset);
Gpio_SetIO(DIG3_PORT, DIG3_PIN_NUM, GpioPinReset);
/* 切换到下一个数码管 */
dig_num = (dig_num + 1) % 3;
switch(dig_num)
{
case 0:
Gpio_SetIO(DIG1_PORT, DIG1_PIN_NUM, (dig.dig1 == DIG_STATE_ON) ? GpioPinSet : GpioPinReset);
break;
case 1:
Gpio_SetIO(DIG2_PORT, DIG2_PIN_NUM, (dig.dig2 == DIG_STATE_ON) ? GpioPinSet : GpioPinReset);
break;
case 2:
Gpio_SetIO(DIG3_PORT, DIG3_PIN_NUM, (dig.dig3 == DIG_STATE_ON) ? GpioPinSet : GpioPinReset);
break;
default:
break;
}
}
/* LED控制 */
void led_control_task(void)
{
/* LED1 */
Gpio_SetIO(LED1_PORT, LED1_PIN_NUM, (led.led1 == LED_STATE_ON) ? GpioPinSet : GpioPinReset);
/* LED2 */
Gpio_SetIO(LED2_PORT, LED2_PIN_NUM, (led.led2 == LED_STATE_ON) ? GpioPinSet : GpioPinReset);
/* LED3 */
Gpio_SetIO(LED3_PORT, LED3_PIN_NUM, (led.led3 == LED_STATE_ON) ? GpioPinSet : GpioPinReset);
/* LED4 */
Gpio_SetIO(LED4_PORT, LED4_PIN_NUM, (led.led4 == LED_STATE_ON) ? GpioPinSet : GpioPinReset);
/* LED5 */
Gpio_SetIO(LED5_PORT, LED5_PIN_NUM, (led.led5 == LED_STATE_ON) ? GpioPinSet : GpioPinReset);
/* LED6 */
Gpio_SetIO(LED6_PORT, LED6_PIN_NUM, (led.led6 == LED_STATE_ON) ? GpioPinSet : GpioPinReset);
/* LED7 */
Gpio_SetIO(LED7_PORT, LED7_PIN_NUM, (led.led7 == LED_STATE_ON) ? GpioPinSet : GpioPinReset);
/* LED8 */
Gpio_SetIO(LED8_PORT, LED8_PIN_NUM, (led.led8 == LED_STATE_ON) ? GpioPinSet : GpioPinReset);
/* LED9 */
Gpio_SetIO(LED9_PORT, LED9_PIN_NUM, (led.led9 == LED_STATE_ON) ? GpioPinSet : GpioPinReset);
}
int main(void)
{
SystemClock_Config(); /* 系统时钟配置 */
gpio_init(); /* GPIO初始化 */
while(1)
{
key_scan_task(); /* 按键扫描 */
dig_display_task(); /* 数码管显示 */
led_control_task(); /* LED控制 */
}
}
```
注意,上述代码只是示例代码,需要根据实际情况进行修改和完善。
阅读全文