hc32l13x ch455h 4x5矩阵键盘,数码管驱动,按键使用gpio,一个ch455h控制三个数码管,每个按键按下电亮数码管中的一个led,具体实现代码
时间: 2023-10-24 07:08:39 浏览: 147
以下是一个示例代码,可以实现HC32L13X控制CH455H和4x5矩阵键盘以及数码管的驱动:
```c
#include "hc32l13x.h"
#include "hc32l13x_clk.h"
#include "hc32l13x_gpio.h"
#include "hc32l13x_it.h"
#include "hc32l13x_rmu.h"
#include "hc32l13x_uart.h"
#define LED_PORT GpioPortD
#define LED_PIN GpioPin0
#define KEY1_PORT GpioPortA
#define KEY1_PIN GpioPin0
#define KEY2_PORT GpioPortA
#define KEY2_PIN GpioPin1
#define KEY3_PORT GpioPortA
#define KEY3_PIN GpioPin2
#define KEY4_PORT GpioPortA
#define KEY4_PIN GpioPin3
#define KEY5_PORT GpioPortA
#define KEY5_PIN GpioPin4
#define DIGIT1_PORT GpioPortC
#define DIGIT1_PIN GpioPin0
#define DIGIT2_PORT GpioPortC
#define DIGIT2_PIN GpioPin1
#define DIGIT3_PORT GpioPortC
#define DIGIT3_PIN GpioPin2
#define CH455H_CLK 8000000UL
#define CH455H_CS_PORT GpioPortB
#define CH455H_CS_PIN GpioPin0
#define CH455H_DI_PORT GpioPortB
#define CH455H_DI_PIN GpioPin1
#define CH455H_DO_PORT GpioPortB
#define CH455H_DO_PIN GpioPin2
#define DIGIT_MAX 999
static uint8_t s_u8Digit1 = 0;
static uint8_t s_u8Digit2 = 0;
static uint8_t s_u8Digit3 = 0;
static uint8_t s_u8LedState = 0;
static void LED_Init(void)
{
stc_gpio_init_t stcGpioInit;
/* 端口方向配置 */
GPIO_StructInit(&stcGpioInit);
stcGpioInit.u16PinDir = PIN_OUT;
GPIO_Init(LED_PORT, LED_PIN, &stcGpioInit);
/* 端口驱动能力配置 */
GPIO_StructInit(&stcGpioInit);
stcGpioInit.u32PinDrv = GPIO_DRV_HIGH;
GPIO_Init(LED_PORT, LED_PIN, &stcGpioInit);
GPIO_ResetPins(LED_PORT, LED_PIN);
}
static void KEY_Init(void)
{
stc_gpio_init_t stcGpioInit;
/* 端口方向配置 */
GPIO_StructInit(&stcGpioInit);
stcGpioInit.u16PinDir = PIN_IN;
GPIO_Init(KEY1_PORT, KEY1_PIN, &stcGpioInit);
GPIO_Init(KEY2_PORT, KEY2_PIN, &stcGpioInit);
GPIO_Init(KEY3_PORT, KEY3_PIN, &stcGpioInit);
GPIO_Init(KEY4_PORT, KEY4_PIN, &stcGpioInit);
GPIO_Init(KEY5_PORT, KEY5_PIN, &stcGpioInit);
/* 端口上拉 */
GPIO_StructInit(&stcGpioInit);
stcGpioInit.u16PinPUD = PIN_PU;
GPIO_Init(KEY1_PORT, KEY1_PIN, &stcGpioInit);
GPIO_Init(KEY2_PORT, KEY2_PIN, &stcGpioInit);
GPIO_Init(KEY3_PORT, KEY3_PIN, &stcGpioInit);
GPIO_Init(KEY4_PORT, KEY4_PIN, &stcGpioInit);
GPIO_Init(KEY5_PORT, KEY5_PIN, &stcGpioInit);
}
static void DIGIT_Init(void)
{
stc_gpio_init_t stcGpioInit;
/* 端口方向配置 */
GPIO_StructInit(&stcGpioInit);
stcGpioInit.u16PinDir = PIN_OUT;
GPIO_Init(DIGIT1_PORT, DIGIT1_PIN, &stcGpioInit);
GPIO_Init(DIGIT2_PORT, DIGIT2_PIN, &stcGpioInit);
GPIO_Init(DIGIT3_PORT, DIGIT3_PIN, &stcGpioInit);
/* 端口上拉 */
GPIO_StructInit(&stcGpioInit);
stcGpioInit.u16PinPUD = PIN_PU;
GPIO_Init(DIGIT1_PORT, DIGIT1_PIN, &stcGpioInit);
GPIO_Init(DIGIT2_PORT, DIGIT2_PIN, &stcGpioInit);
GPIO_Init(DIGIT3_PORT, DIGIT3_PIN, &stcGpioInit);
/* 数码管初始化 */
s_u8Digit1 = 0;
s_u8Digit2 = 0;
s_u8Digit3 = 0;
GPIO_SetPins(DIGIT1_PORT, DIGIT1_PIN);
GPIO_SetPins(DIGIT2_PORT, DIGIT2_PIN);
GPIO_SetPins(DIGIT3_PORT, DIGIT3_PIN);
}
static void CH455H_Init(void)
{
stc_gpio_init_t stcGpioInit;
/* 端口方向配置 */
GPIO_StructInit(&stcGpioInit);
stcGpioInit.u16PinDir = PIN_OUT;
GPIO_Init(CH455H_CS_PORT, CH455H_CS_PIN, &stcGpioInit);
GPIO_Init(CH455H_DI_PORT, CH455H_DI_PIN, &stcGpioInit);
/* 端口上拉 */
GPIO_StructInit(&stcGpioInit);
stcGpioInit.u16PinPUD = PIN_PU;
GPIO_Init(CH455H_CS_PORT, CH455H_CS_PIN, &stcGpioInit);
GPIO_Init(CH455H_DI_PORT, CH455H_DI_PIN, &stcGpioInit);
/* CH455H初始化 */
CH455H_Reset();
CH455H_WriteByte(0x80);
CH455H_WriteByte(0x02);
}
static void CH455H_Reset(void)
{
GPIO_ResetPins(CH455H_CS_PORT, CH455H_CS_PIN);
GPIO_SetPins(CH455H_DI_PORT, CH455H_DI_PIN);
for (uint32_t i = 0; i < 8; i++)
{
GPIO_ResetPins(CH455H_CLK_PORT, CH455H_CLK_PIN);
GPIO_SetPins(CH455H_CLK_PORT, CH455H_CLK_PIN);
}
GPIO_SetPins(CH455H_CS_PORT, CH455H_CS_PIN);
}
static void CH455H_WriteByte(uint8_t u8Data)
{
GPIO_ResetPins(CH455H_CS_PORT, CH455H_CS_PIN);
for (uint32_t i = 0; i < 8; i++)
{
GPIO_WritePins(CH455H_DI_PORT, CH455H_DI_PIN, (u8Data & 0x80) ? PIN_HIGH : PIN_LOW);
u8Data <<= 1;
GPIO_ResetPins(CH455H_CLK_PORT, CH455H_CLK_PIN);
GPIO_SetPins(CH455H_CLK_PORT, CH455H_CLK_PIN);
}
GPIO_SetPins(CH455H_CS_PORT, CH455H_CS_PIN);
}
static void DIGIT_Display(uint8_t u8Digit1, uint8_t u8Digit2, uint8_t u8Digit3)
{
static uint8_t s_u8DigitIndex = 0;
switch (s_u8DigitIndex)
{
case 0:
GPIO_ResetPins(DIGIT1_PORT, DIGIT1_PIN);
GPIO_SetPins(DIGIT2_PORT, DIGIT2_PIN);
GPIO_SetPins(DIGIT3_PORT, DIGIT3_PIN);
break;
case 1:
GPIO_SetPins(DIGIT1_PORT, DIGIT1_PIN);
GPIO_ResetPins(DIGIT2_PORT, DIGIT2_PIN);
GPIO_SetPins(DIGIT3_PORT, DIGIT3_PIN);
break;
case 2:
GPIO_SetPins(DIGIT1_PORT, DIGIT1_PIN);
GPIO_SetPins(DIGIT2_PORT, DIGIT2_PIN);
GPIO_ResetPins(DIGIT3_PORT, DIGIT3_PIN);
break;
default:
break;
}
switch (s_u8DigitIndex)
{
case 0:
LED_On(LED_PORT, LED_PIN);
GPIO_SetPins(CH455H_DI_PORT, CH455H_DI_PIN);
CH455H_WriteByte(u8Digit1);
LED_Off(LED_PORT, LED_PIN);
break;
case 1:
LED_On(LED_PORT, LED_PIN);
GPIO_SetPins(CH455H_DI_PORT, CH455H_DI_PIN);
CH455H_WriteByte(u8Digit2);
LED_Off(LED_PORT, LED_PIN);
break;
case 2:
LED_On(LED_PORT, LED_PIN);
GPIO_SetPins(CH455H_DI_PORT, CH455H_DI_PIN);
CH455H_WriteByte(u8Digit3);
LED_Off(LED_PORT, LED_PIN);
break;
default:
break;
}
s_u8DigitIndex++;
if (s_u8DigitIndex >= 3)
{
s_u8DigitIndex = 0;
}
}
static void KEY_Scan(void)
{
if (!GPIO_ReadPins(KEY1_PORT, KEY1_PIN))
{
s_u8LedState ^= 0x01;
}
if (!GPIO_ReadPins(KEY2_PORT, KEY2_PIN))
{
s_u8LedState ^= 0x02;
}
if (!GPIO_ReadPins(KEY3_PORT, KEY3_PIN))
{
s_u8LedState ^= 0x04;
}
if (!GPIO_ReadPins(KEY4_PORT, KEY4_PIN))
{
s_u8LedState ^= 0x08;
}
if (!GPIO_ReadPins(KEY5_PORT, KEY5_PIN))
{
s_u8LedState ^= 0x10;
}
}
int main(void)
{
/* 系统时钟初始化 */
CLK_HSEInit(CLK_HSE_ON);
CLK_SetSysclkDiv(CLK_SYSCLK_DIV1);
CLK_SetHCLK(CLK_HCLK_CLKSYS);
CLK_SetSysclkSrc(CLK_SYSCLKSOURCE_HSE);
CLK_SetPCLKDiv(CLK_PCLK_DIV1);
/* 初始化LED、按键、数码管和CH455H */
LED_Init();
KEY_Init();
DIGIT_Init();
CH455H_Init();
/* 主循环 */
while (1)
{
/* 扫描按键 */
KEY_Scan();
/* 显示数字 */
DIGIT_Display(s_u8Digit1, s_u8Digit2, s_u8Digit3);
/* 更新数字 */
s_u8Digit3++;
if (s_u8Digit3 > 9)
{
s_u8Digit3 = 0;
s_u8Digit2++;
if (s_u8Digit2 > 9)
{
s_u8Digit2 = 0;
s_u8Digit1++;
if (s_u8Digit1 > 9)
{
s_u8Digit1 = 0;
}
}
}
}
}
```
在上面的代码中,LED和按键分别使用了GPIO控制,数码管使用了HC455H控制。主循环中扫描按键、显示数字并更新数字。如果按键被按下,相应的LED将会翻转。
阅读全文