hc32l13x ch455h 4x5矩阵键盘,数码管驱动,按键使用gpio,数码管使用PB12,PB13gpio引脚控制,具体实现代码
时间: 2023-10-21 22:05:17 浏览: 107
以下是使用HC32L13X芯片的CH455H 4x5矩阵键盘和数码管的代码实现:
```c
#include "hc32l13x.h"
#define KEY_PORT GpioPortA
#define KEY_PIN GpioPin0
#define DISP_PORT GpioPortB
#define DISP_PIN1 GpioPin12
#define DISP_PIN2 GpioPin13
#define KEY_ROW1_PORT GpioPortA
#define KEY_ROW1_PIN GpioPin1
#define KEY_ROW2_PORT GpioPortA
#define KEY_ROW2_PIN GpioPin2
#define KEY_ROW3_PORT GpioPortA
#define KEY_ROW3_PIN GpioPin3
#define KEY_ROW4_PORT GpioPortA
#define KEY_ROW4_PIN GpioPin4
#define KEY_COL1_PORT GpioPortA
#define KEY_COL1_PIN GpioPin5
#define KEY_COL2_PORT GpioPortA
#define KEY_COL2_PIN GpioPin6
#define KEY_COL3_PORT GpioPortA
#define KEY_COL3_PIN GpioPin7
#define KEY_COL4_PORT GpioPortA
#define KEY_COL4_PIN GpioPin8
#define KEY_COL5_PORT GpioPortA
#define KEY_COL5_PIN GpioPin9
#define KEY_ROW1_HIGH() Gpio_SetIO(KEY_ROW1_PORT, KEY_ROW1_PIN)
#define KEY_ROW1_LOW() Gpio_ClrIO(KEY_ROW1_PORT, KEY_ROW1_PIN)
#define KEY_ROW2_HIGH() Gpio_SetIO(KEY_ROW2_PORT, KEY_ROW2_PIN)
#define KEY_ROW2_LOW() Gpio_ClrIO(KEY_ROW2_PORT, KEY_ROW2_PIN)
#define KEY_ROW3_HIGH() Gpio_SetIO(KEY_ROW3_PORT, KEY_ROW3_PIN)
#define KEY_ROW3_LOW() Gpio_ClrIO(KEY_ROW3_PORT, KEY_ROW3_PIN)
#define KEY_ROW4_HIGH() Gpio_SetIO(KEY_ROW4_PORT, KEY_ROW4_PIN)
#define KEY_ROW4_LOW() Gpio_ClrIO(KEY_ROW4_PORT, KEY_ROW4_PIN)
#define KEY_COL1_HIGH() Gpio_SetIO(KEY_COL1_PORT, KEY_COL1_PIN)
#define KEY_COL1_LOW() Gpio_ClrIO(KEY_COL1_PORT, KEY_COL1_PIN)
#define KEY_COL2_HIGH() Gpio_SetIO(KEY_COL2_PORT, KEY_COL2_PIN)
#define KEY_COL2_LOW() Gpio_ClrIO(KEY_COL2_PORT, KEY_COL2_PIN)
#define KEY_COL3_HIGH() Gpio_SetIO(KEY_COL3_PORT, KEY_COL3_PIN)
#define KEY_COL3_LOW() Gpio_ClrIO(KEY_COL3_PORT, KEY_COL3_PIN)
#define KEY_COL4_HIGH() Gpio_SetIO(KEY_COL4_PORT, KEY_COL4_PIN)
#define KEY_COL4_LOW() Gpio_ClrIO(KEY_COL4_PORT, KEY_COL4_PIN)
#define KEY_COL5_HIGH() Gpio_SetIO(KEY_COL5_PORT, KEY_COL5_PIN)
#define KEY_COL5_LOW() Gpio_ClrIO(KEY_COL5_PORT, KEY_COL5_PIN)
#define DISP_HIGH() Gpio_SetIO(DISP_PORT, DISP_PIN1); Gpio_SetIO(DISP_PORT, DISP_PIN2)
#define DISP_LOW() Gpio_ClrIO(DISP_PORT, DISP_PIN1); Gpio_ClrIO(DISP_PORT, DISP_PIN2)
#define KEY_SCAN_INTERVAL 10000
static uint8_t KeyScan(void)
{
uint8_t key = 0xFF;
KEY_ROW1_HIGH(); KEY_ROW2_HIGH(); KEY_ROW3_HIGH(); KEY_ROW4_LOW(); KEY_COL1_LOW(); DelayUs(KEY_SCAN_INTERVAL);
if (!Gpio_GetIO(KEY_PORT, KEY_PIN)) { key = 0; }
KEY_ROW1_HIGH(); KEY_ROW2_HIGH(); KEY_ROW3_LOW(); KEY_ROW4_HIGH(); KEY_COL1_LOW(); DelayUs(KEY_SCAN_INTERVAL);
if (!Gpio_GetIO(KEY_PORT, KEY_PIN)) { key = 1; }
KEY_ROW1_HIGH(); KEY_ROW2_HIGH(); KEY_ROW3_LOW(); KEY_ROW4_HIGH(); KEY_COL1_HIGH(); KEY_COL2_LOW(); DelayUs(KEY_SCAN_INTERVAL);
if (!Gpio_GetIO(KEY_PORT, KEY_PIN)) { key = 2; }
KEY_ROW1_HIGH(); KEY_ROW2_HIGH(); KEY_ROW3_LOW(); KEY_ROW4_HIGH(); KEY_COL2_HIGH(); KEY_COL3_LOW(); DelayUs(KEY_SCAN_INTERVAL);
if (!Gpio_GetIO(KEY_PORT, KEY_PIN)) { key = 3; }
KEY_ROW1_HIGH(); KEY_ROW2_HIGH(); KEY_ROW3_LOW(); KEY_ROW4_HIGH(); KEY_COL3_HIGH(); KEY_COL4_LOW(); DelayUs(KEY_SCAN_INTERVAL);
if (!Gpio_GetIO(KEY_PORT, KEY_PIN)) { key = 4; }
KEY_ROW1_HIGH(); KEY_ROW2_HIGH(); KEY_ROW3_HIGH(); KEY_ROW4_LOW(); KEY_COL4_HIGH(); DelayUs(KEY_SCAN_INTERVAL);
if (!Gpio_GetIO(KEY_PORT, KEY_PIN)) { key = 5; }
KEY_ROW1_HIGH(); KEY_ROW2_LOW(); KEY_ROW3_HIGH(); KEY_ROW4_HIGH(); KEY_COL1_LOW(); DelayUs(KEY_SCAN_INTERVAL);
if (!Gpio_GetIO(KEY_PORT, KEY_PIN)) { key = 6; }
KEY_ROW1_HIGH(); KEY_ROW2_LOW(); KEY_ROW3_HIGH(); KEY_ROW4_HIGH(); KEY_COL1_HIGH(); KEY_COL2_LOW(); DelayUs(KEY_SCAN_INTERVAL);
if (!Gpio_GetIO(KEY_PORT, KEY_PIN)) { key = 7; }
KEY_ROW1_HIGH(); KEY_ROW2_LOW(); KEY_ROW3_HIGH(); KEY_ROW4_HIGH(); KEY_COL2_HIGH(); KEY_COL3_LOW(); DelayUs(KEY_SCAN_INTERVAL);
if (!Gpio_GetIO(KEY_PORT, KEY_PIN)) { key = 8; }
KEY_ROW1_HIGH(); KEY_ROW2_LOW(); KEY_ROW3_HIGH(); KEY_ROW4_HIGH(); KEY_COL3_HIGH(); KEY_COL4_LOW(); DelayUs(KEY_SCAN_INTERVAL);
if (!Gpio_GetIO(KEY_PORT, KEY_PIN)) { key = 9; }
KEY_ROW1_LOW(); KEY_ROW2_HIGH(); KEY_ROW3_HIGH(); KEY_ROW4_HIGH(); KEY_COL1_LOW(); DelayUs(KEY_SCAN_INTERVAL);
if (!Gpio_GetIO(KEY_PORT, KEY_PIN)) { key = 10; }
KEY_ROW1_LOW(); KEY_ROW2_HIGH(); KEY_ROW3_HIGH(); KEY_ROW4_HIGH(); KEY_COL1_HIGH(); KEY_COL2_LOW(); DelayUs(KEY_SCAN_INTERVAL);
if (!Gpio_GetIO(KEY_PORT, KEY_PIN)) { key = 11; }
KEY_ROW1_LOW(); KEY_ROW2_HIGH(); KEY_ROW3_HIGH(); KEY_ROW4_HIGH(); KEY_COL2_HIGH(); KEY_COL3_LOW(); DelayUs(KEY_SCAN_INTERVAL);
if (!Gpio_GetIO(KEY_PORT, KEY_PIN)) { key = 12; }
KEY_ROW1_LOW(); KEY_ROW2_HIGH(); KEY_ROW3_HIGH(); KEY_ROW4_HIGH(); KEY_COL3_HIGH(); KEY_COL4_LOW(); DelayUs(KEY_SCAN_INTERVAL);
if (!Gpio_GetIO(KEY_PORT, KEY_PIN)) { key = 13; }
KEY_ROW1_HIGH(); KEY_ROW2_HIGH(); KEY_ROW3_HIGH(); KEY_ROW4_HIGH(); KEY_COL5_LOW(); DelayUs(KEY_SCAN_INTERVAL);
if (!Gpio_GetIO(KEY_PORT, KEY_PIN)) { key = 14; }
return key;
}
static void DispNum(uint8_t num)
{
static const uint8_t DISP_TBL[] =
{
0x3F, 0x06, 0x5B, 0x4F, 0x66,
0x6D, 0x7D, 0x07, 0x7F, 0x6F,
};
uint8_t digit1 = num % 10;
uint8_t digit2 = num / 10;
uint8_t disp1 = (digit2 == 0) ? 0 : DISP_TBL[digit2];
uint8_t disp2 = DISP_TBL[digit1];
DISP_LOW();
Gpio_SetIO(DISP_PORT, DISP_PIN1);
HC32L13X_WriteIO(DISP_PORT, DISP_PIN2, (disp1 != 0) ? disp1 : 0x00);
DelayMs(1);
DISP_LOW();
Gpio_SetIO(DISP_PORT, DISP_PIN2);
HC32L13X_WriteIO(DISP_PORT, DISP_PIN1, disp2);
DelayMs(1);
}
int main(void)
{
Sysctrl_SetPeripheralGate(SysctrlPeripheralGpio, TRUE);
Gpio_InitIO(KEY_PORT, KEY_PIN, GpioDirIn);
Gpio_InitIO(DISP_PORT, DISP_PIN1, GpioDirOut);
Gpio_InitIO(DISP_PORT, DISP_PIN2, GpioDirOut);
Gpio_InitIO(KEY_ROW1_PORT, KEY_ROW1_PIN, GpioDirOut);
Gpio_InitIO(KEY_ROW2_PORT, KEY_ROW2_PIN, GpioDirOut);
Gpio_InitIO(KEY_ROW3_PORT, KEY_ROW3_PIN, GpioDirOut);
Gpio_InitIO(KEY_ROW4_PORT, KEY_ROW4_PIN, GpioDirOut);
Gpio_InitIO(KEY_COL1_PORT, KEY_COL1_PIN, GpioDirOut);
Gpio_InitIO(KEY_COL2_PORT, KEY_COL2_PIN, GpioDirOut);
Gpio_InitIO(KEY_COL3_PORT, KEY_COL3_PIN, GpioDirOut);
Gpio_InitIO(KEY_COL4_PORT, KEY_COL4_PIN, GpioDirOut);
Gpio_InitIO(KEY_COL5_PORT, KEY_COL5_PIN, GpioDirOut);
while (1)
{
uint8_t key = KeyScan();
if (key != 0xFF)
{
DispNum(key);
}
}
}
```
在此代码中,我们将矩阵键盘的行列分别连接到不同的GPIO引脚上,并使用`KeyScan`函数扫描按键状态。扫描完成后,我们将数字显示在数码管上,使用`DispNum`函数将数字转换为7段LED数码管可以显示的格式。注意,该代码需要配合HC32L13X芯片的外设库一起使用。
阅读全文