hc32l13x矩阵键盘4x5带led灯示例,led灯用三个lcd显示
时间: 2023-12-19 17:06:32 浏览: 91
以下是一个基于HC32L13x的4x5矩阵键盘带LED灯的示例程序,用三个LCD显示LED灯状态:
```c
#include "hc32l13x.h"
#include "hc32l13x_clk.h"
#include "hc32l13x_gpio.h"
#include "hc32l13x_lcd.h"
#define LED_NUM 20
#define LED_ROW 4
#define LED_COL 5
#define LED_ON 1
#define LED_OFF 0
#define KEY_PORT GPIO_PORT_A
#define KEY_PIN_START GPIO_PIN_0
#define KEY_PIN_END GPIO_PIN_3
#define LED_PORT GPIO_PORT_B
#define LED_PIN_START GPIO_PIN_0
#define LED_PIN_END GPIO_PIN_4
#define LCD_PORT0 GPIO_PORT_D
#define LCD_PORT1 GPIO_PORT_E
#define LCD_PIN_DB0 GPIO_PIN_0
#define LCD_PIN_DB1 GPIO_PIN_1
#define LCD_PIN_DB2 GPIO_PIN_2
#define LCD_PIN_DB3 GPIO_PIN_3
#define LCD_PIN_DB4 GPIO_PIN_4
#define LCD_PIN_DB5 GPIO_PIN_5
#define LCD_PIN_DB6 GPIO_PIN_6
#define LCD_PIN_DB7 GPIO_PIN_7
#define LCD_PIN_RS GPIO_PIN_4
#define LCD_PIN_RW GPIO_PIN_5
#define LCD_PIN_EN GPIO_PIN_6
typedef struct
{
uint8_t row;
uint8_t col;
uint8_t state;
} Key_t;
static const Key_t g_tKeyMap[LED_ROW][LED_COL] =
{
{ {0, 0, LED_OFF}, {0, 1, LED_OFF}, {0, 2, LED_OFF}, {0, 3, LED_OFF}, {0, 4, LED_OFF} },
{ {1, 0, LED_OFF}, {1, 1, LED_OFF}, {1, 2, LED_OFF}, {1, 3, LED_OFF}, {1, 4, LED_OFF} },
{ {2, 0, LED_OFF}, {2, 1, LED_OFF}, {2, 2, LED_OFF}, {2, 3, LED_OFF}, {2, 4, LED_OFF} },
{ {3, 0, LED_OFF}, {3, 1, LED_OFF}, {3, 2, LED_OFF}, {3, 3, LED_OFF}, {3, 4, LED_OFF} },
};
static void LCD_Init(void)
{
stc_lcd_cfg_t stcCfg;
CLK_FcgPeriphClockCmd(CLK_FCG_LCD, Enable);
GPIO_SetFunc(LCD_PORT0, LCD_PIN_DB0, GPIO_FUNC_7_LCD);
GPIO_SetFunc(LCD_PORT0, LCD_PIN_DB1, GPIO_FUNC_7_LCD);
GPIO_SetFunc(LCD_PORT0, LCD_PIN_DB2, GPIO_FUNC_7_LCD);
GPIO_SetFunc(LCD_PORT0, LCD_PIN_DB3, GPIO_FUNC_7_LCD);
GPIO_SetFunc(LCD_PORT1, LCD_PIN_DB4, GPIO_FUNC_7_LCD);
GPIO_SetFunc(LCD_PORT1, LCD_PIN_DB5, GPIO_FUNC_7_LCD);
GPIO_SetFunc(LCD_PORT1, LCD_PIN_DB6, GPIO_FUNC_7_LCD);
GPIO_SetFunc(LCD_PORT1, LCD_PIN_DB7, GPIO_FUNC_7_LCD);
GPIO_SetFunc(LCD_PORT0, LCD_PIN_RS, GPIO_FUNC_7_LCD);
GPIO_SetFunc(LCD_PORT0, LCD_PIN_RW, GPIO_FUNC_7_LCD);
GPIO_SetFunc(LCD_PORT0, LCD_PIN_EN, GPIO_FUNC_7_LCD);
stcCfg.enDispMode = LCD_DISP_MODE_4_BIT;
stcCfg.enMuxMode = LCD_MUX_MODE_1_COM;
stcCfg.enBias = LCD_BIAS_1_3;
stcCfg.enBiasBuf = LCD_BIAS_BUF_ON;
stcCfg.enWaveform = LCD_WAVEFORM_TYPE_A;
stcCfg.enPulseWidth = LCD_PULSE_WIDTH_2_CYCLE;
stcCfg.enDeadTime = LCD_DEAD_TIME_1_CYCLE;
stcCfg.enClkDiv = LCD_CLK_DIV32;
stcCfg.u8Contrast = 0x3F;
(void)LCD_Init(&stcCfg);
LCD_Cmd(Enable);
}
static void LED_Init(void)
{
uint8_t i;
for (i = LED_PIN_START; i <= LED_PIN_END; i++)
{
GPIO_SetFunc(LED_PORT, i, GPIO_FUNC_0_GPIO);
GPIO_SetOutputEnable(LED_PORT, i, Enable);
GPIO_WriteBit(LED_PORT, i, LED_OFF);
}
}
static void Key_Init(void)
{
uint8_t i;
for (i = KEY_PIN_START; i <= KEY_PIN_END; i++)
{
GPIO_SetFunc(KEY_PORT, i, GPIO_FUNC_0_GPIO);
GPIO_SetInputEnable(KEY_PORT, i, Enable);
GPIO_SetPull(KEY_PORT, i, GPIO_PULL_UP);
}
}
static void LCD_ShowLedStatus(void)
{
uint8_t i, j;
char szBuf[16];
LCD_SetCursorPos(0, 0);
LCD_WriteString("LED STATUS");
for (i = 0; i < LED_ROW; i++)
{
for (j = 0; j < LED_COL; j++)
{
LCD_SetCursorPos(i + 2, j * 4);
sprintf(szBuf, "[%d,%d]:%d", i, j, g_tKeyMap[i][j].state);
LCD_WriteString(szBuf);
}
}
}
void LED_Set(uint8_t row, uint8_t col, uint8_t state)
{
if (state == LED_ON)
{
GPIO_WriteBit(LED_PORT, LED_PIN_START + row * LED_COL + col, LED_ON);
}
else
{
GPIO_WriteBit(LED_PORT, LED_PIN_START + row * LED_COL + col, LED_OFF);
}
g_tKeyMap[row][col].state = state;
}
int main(void)
{
uint8_t i, j;
uint8_t u8KeyState;
uint8_t u8PrevKeyState;
Key_t *ptKey;
uint8_t u8LedState;
uint8_t u8PrevLedState;
CLK_HrcCmd(Enable);
CLK_SetHrcTrim(CLK_HRC_TRIM_16MHZ);
CLK_SetSysclkDiv(CLK_SYSCLK_DIV1);
CLK_SetPeripheralDiv(CLK_PERIPHERAL_DIV1);
LCD_Init();
LED_Init();
Key_Init();
LCD_ShowLedStatus();
while (1)
{
u8PrevKeyState = u8KeyState;
u8KeyState = 0;
for (i = KEY_PIN_START; i <= KEY_PIN_END; i++)
{
u8KeyState |= (GPIO_ReadInputDataBit(KEY_PORT, i) << (i - KEY_PIN_START));
}
if (u8KeyState != u8PrevKeyState)
{
for (i = 0; i < LED_ROW; i++)
{
for (j = 0; j < LED_COL; j++)
{
ptKey = (Key_t *)&g_tKeyMap[i][j];
if ((u8KeyState & (1 << i)) && (u8KeyState & (1 << (j + 4))))
{
if (ptKey->state == LED_OFF)
{
LED_Set(i, j, LED_ON);
}
else
{
LED_Set(i, j, LED_OFF);
}
}
}
}
}
u8PrevLedState = u8LedState;
u8LedState = 0;
for (i = 0; i < LED_ROW; i++)
{
for (j = 0; j < LED_COL; j++)
{
u8LedState |= (g_tKeyMap[i][j].state << (i * LED_COL + j));
}
}
if (u8LedState != u8PrevLedState)
{
LCD_ShowLedStatus();
}
}
}
```
该程序中定义了一个Key_t结构体,表示一个按键的行、列和状态信息。g_tKeyMap数组表示所有按键的状态,初始化为LED_OFF。在主循环中,先读取按键状态,如果有按键按下和抬起的状态变化,则根据按键的行、列信息改变对应LED灯的状态。然后将所有LED灯的状态信息转换成一个8位二进制数,并将该数转换成十六进制字符串显示在三个LCD上。
需要注意的是,本程序中使用了HC32L13x的LCD驱动模块来控制LCD显示,需要在程序中调用LCD_Init()函数初始化LCD模块。此外,由于HC32L13x的GPIO模块没有直接控制LED灯的接口,因此需要在程序中手动控制LED灯的状态。
阅读全文