hc32l13x矩阵键盘4x5带数码管示例,每个按键控制一个led
时间: 2024-02-03 14:12:40 浏览: 162
以下是一个基于hc32l13x芯片的4x5矩阵键盘带数码管示例代码,每个按键都控制一个LED的亮灭。
首先,需要定义矩阵键盘的行列引脚和LED的引脚:
```c
#define ROW1_PORT GPIO_PORT_0
#define ROW1_PIN GPIO_PIN_0
#define ROW2_PORT GPIO_PORT_0
#define ROW2_PIN GPIO_PIN_1
#define ROW3_PORT GPIO_PORT_0
#define ROW3_PIN GPIO_PIN_2
#define ROW4_PORT GPIO_PORT_0
#define ROW4_PIN GPIO_PIN_3
#define COL1_PORT GPIO_PORT_0
#define COL1_PIN GPIO_PIN_4
#define COL2_PORT GPIO_PORT_0
#define COL2_PIN GPIO_PIN_5
#define COL3_PORT GPIO_PORT_0
#define COL3_PIN GPIO_PIN_6
#define COL4_PORT GPIO_PORT_0
#define COL4_PIN GPIO_PIN_7
#define COL5_PORT GPIO_PORT_1
#define COL5_PIN GPIO_PIN_0
#define LED1_PORT GPIO_PORT_1
#define LED1_PIN GPIO_PIN_1
#define LED2_PORT GPIO_PORT_1
#define LED2_PIN GPIO_PIN_2
#define LED3_PORT GPIO_PORT_1
#define LED3_PIN GPIO_PIN_3
#define LED4_PORT GPIO_PORT_1
#define LED4_PIN GPIO_PIN_4
#define LED5_PORT GPIO_PORT_1
#define LED5_PIN GPIO_PIN_5
```
然后需要初始化这些引脚:
```c
void GPIO_Init(void)
{
en_gpio_port_t enGpioPort;
stc_gpio_cfg_t stcGpioCfg;
// 初始化行引脚
enGpioPort = ROW1_PORT;
stcGpioCfg.enDir = GpioDirIn;
stcGpioCfg.enDrv = GpioDrvH;
stcGpioCfg.enPuPd = GpioPu;
stcGpioCfg.enOD = GpioOdDisable;
GPIO_Init(enGpioPort, ROW1_PIN, &stcGpioCfg);
enGpioPort = ROW2_PORT;
GPIO_Init(enGpioPort, ROW2_PIN, &stcGpioCfg);
enGpioPort = ROW3_PORT;
GPIO_Init(enGpioPort, ROW3_PIN, &stcGpioCfg);
enGpioPort = ROW4_PORT;
GPIO_Init(enGpioPort, ROW4_PIN, &stcGpioCfg);
// 初始化列引脚
enGpioPort = COL1_PORT;
stcGpioCfg.enDir = GpioDirOut;
stcGpioCfg.enDrv = GpioDrvH;
stcGpioCfg.enPuPd = GpioPu;
stcGpioCfg.enOD = GpioOdDisable;
GPIO_Init(enGpioPort, COL1_PIN, &stcGpioCfg);
enGpioPort = COL2_PORT;
GPIO_Init(enGpioPort, COL2_PIN, &stcGpioCfg);
enGpioPort = COL3_PORT;
GPIO_Init(enGpioPort, COL3_PIN, &stcGpioCfg);
enGpioPort = COL4_PORT;
GPIO_Init(enGpioPort, COL4_PIN, &stcGpioCfg);
enGpioPort = COL5_PORT;
GPIO_Init(enGpioPort, COL5_PIN, &stcGpioCfg);
// 初始化LED引脚
enGpioPort = LED1_PORT;
stcGpioCfg.enDir = GpioDirOut;
stcGpioCfg.enDrv = GpioDrvH;
stcGpioCfg.enPuPd = GpioPu;
stcGpioCfg.enOD = GpioOdDisable;
GPIO_Init(enGpioPort, LED1_PIN, &stcGpioCfg);
enGpioPort = LED2_PORT;
GPIO_Init(enGpioPort, LED2_PIN, &stcGpioCfg);
enGpioPort = LED3_PORT;
GPIO_Init(enGpioPort, LED3_PIN, &stcGpioCfg);
enGpioPort = LED4_PORT;
GPIO_Init(enGpioPort, LED4_PIN, &stcGpioCfg);
enGpioPort = LED5_PORT;
GPIO_Init(enGpioPort, LED5_PIN, &stcGpioCfg);
}
```
接下来是扫描矩阵键盘和控制LED的代码:
```c
void ScanKey(void)
{
static uint8_t keyPrev = 0;
// 首先将所有列引脚输出低电平
GPIO_ResetPins(COL1_PORT, COL1_PIN);
GPIO_ResetPins(COL2_PORT, COL2_PIN);
GPIO_ResetPins(COL3_PORT, COL3_PIN);
GPIO_ResetPins(COL4_PORT, COL4_PIN);
GPIO_ResetPins(COL5_PORT, COL5_PIN);
// 然后分别扫描每一行
if (GPIO_ReadInputPortPin(ROW1_PORT, ROW1_PIN) == 0)
{
if (keyPrev != 1)
{
keyPrev = 1;
GPIO_SetPins(LED1_PORT, LED1_PIN);
}
}
else
{
GPIO_ResetPins(LED1_PORT, LED1_PIN);
}
if (GPIO_ReadInputPortPin(ROW2_PORT, ROW2_PIN) == 0)
{
if (keyPrev != 2)
{
keyPrev = 2;
GPIO_SetPins(LED2_PORT, LED2_PIN);
}
}
else
{
GPIO_ResetPins(LED2_PORT, LED2_PIN);
}
if (GPIO_ReadInputPortPin(ROW3_PORT, ROW3_PIN) == 0)
{
if (keyPrev != 3)
{
keyPrev = 3;
GPIO_SetPins(LED3_PORT, LED3_PIN);
}
}
else
{
GPIO_ResetPins(LED3_PORT, LED3_PIN);
}
if (GPIO_ReadInputPortPin(ROW4_PORT, ROW4_PIN) == 0)
{
if (keyPrev != 4)
{
keyPrev = 4;
GPIO_SetPins(LED4_PORT, LED4_PIN);
}
}
else
{
GPIO_ResetPins(LED4_PORT, LED4_PIN);
}
// 最后将当前行的引脚输出高电平,以便扫描下一行
GPIO_SetPins(COL1_PORT, COL1_PIN);
GPIO_SetPins(COL2_PORT, COL2_PIN);
GPIO_SetPins(COL3_PORT, COL3_PIN);
GPIO_SetPins(COL4_PORT, COL4_PIN);
GPIO_SetPins(COL5_PORT, COL5_PIN);
}
```
在主函数中,只需要调用`GPIO_Init()`函数初始化引脚,并在循环中不断调用`ScanKey()`函数扫描矩阵键盘和控制LED即可。
```c
int main(void)
{
// 初始化GPIO引脚
GPIO_Init();
while (1)
{
ScanKey();
}
}
```
阅读全文