【ACM32G103RCT6开发板测评】+ MultiButton按键测试
[复制链接]
本帖最后由 TL-LED 于 2024-1-12 18:16 编辑
测试下按键输入测试。
一、硬件电路
开发板上有一路用户按键,对应MCU的PC13端口
二、MultiButton
2.1、简介
MultiButton 是一个小巧简单易用的事件驱动型按键驱动模块,可无限量扩展按键,按键事件的回调异步处理方式可以简化你的程序结构,去除冗余的按键处理硬编码,让你的按键业务逻辑更清晰。
2.2、源码下载
地址:
三、移植代码
3.1、添加源码到工程项目
3.2、key.c
#include "main.h"
enum Button_IDs {
btn1_id,
btn2_id,
};
struct Button btn1;
uint8_t read_button_GPIO(uint8_t button_id)
{
// you can share the GPIO read function with multiple Buttons
switch(button_id)
{
case btn1_id:
return HAL_GPIO_ReadPin(B2_GPIO_PORT, B2_PIN);
default:
return 0;
}
}
void BTN1_PRESS_DOWN_Handler(void* btn)
{
printf("B2 DOWN! \r\n");
}
void BTN1_PRESS_UP_Handler(void* btn)
{
printf("B2 UP! \r\n");
}
void BTN1_PRESS_REPEAT_Handler(void* btn)
{
printf("B2 PRESS_REPEAT! \r\n");
}
void BTN1_SINGLE_Click_Handler(void* btn)
{
printf("B2 SINGLE_ClICK! \r\n");
}
void BTN1_DOUBLE_Click_Handler(void* btn)
{
printf("B2 DOUBLE_ClICK! \r\n");
}
void BTN1_LONG_PRESS_START_Handler(void* btn)
{
printf("B2 LONG_PRESS_START! \r\n");
}
void BTN1_LONG_PRESS_HOLD_Handler(void* btn)
{
printf("B2 LONG_PRESS_HOLD! \r\n");
}
void init_key(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
B2_GPIO_CLK_ENABLE();
GPIO_InitStruct.Pin = B2_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Drive = GPIO_DRIVE_LEVEL3;
GPIO_InitStruct.Alternate = GPIO_FUNCTION_0;
HAL_GPIO_Init(B2_GPIO_PORT, &GPIO_InitStruct);
button_init(&btn1, read_button_GPIO, 0, btn1_id);
button_attach(&btn1, PRESS_DOWN, BTN1_PRESS_DOWN_Handler);
button_attach(&btn1, PRESS_UP, BTN1_PRESS_UP_Handler);
button_attach(&btn1, PRESS_REPEAT, BTN1_PRESS_REPEAT_Handler);
button_attach(&btn1, SINGLE_CLICK, BTN1_SINGLE_Click_Handler);
button_attach(&btn1, DOUBLE_CLICK, BTN1_DOUBLE_Click_Handler);
button_attach(&btn1, LONG_PRESS_START, BTN1_LONG_PRESS_START_Handler);
button_attach(&btn1, LONG_PRESS_HOLD, BTN1_LONG_PRESS_HOLD_Handler);
button_start(&btn1);
}
3.3、main.c
#include "main.h"
int main(void)
{
HAL_Init();
SystemClock_Config();
usart_init(115200);
printf("\r\n\r\n====== ACM32G103 MCU is runing ======\r\nSysCoreClk: %dHz, HCLK:%dHz\r\nPCLK1:%dHz, PCLK2:%dHz\r\n\r\n",g_SystemCoreClock, HAL_RCC_GetHCLKFreq(),HAL_RCC_GetPCLK1Freq(),HAL_RCC_GetPCLK2Freq());
init_led();
init_tim2();
init_key();
while(1)
{
}
}
3.4、定时器中断
在定时器中断中,调用按键后台处理函数,定时时间5ms
//定时器2初始化
void init_tim2(void)
{
uint32_t timer_clock;
timer_clock = HAL_RCC_GetPCLK1Freq();
if (HAL_RCC_GetHCLKFreq() != timer_clock) // if hclk/pclk != 1, then timer clk = pclk * 2
{
timer_clock = timer_clock << 1;
}
tim2_handler.Instance = TIM2;
tim2_handler.Init.ARRPreLoadEn = TIM_ARR_PRELOAD_ENABLE;
tim2_handler.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
tim2_handler.Init.CounterMode = TIM_COUNTERMODE_UP;
tim2_handler.Init.RepetitionCounter = 0;
tim2_handler.Init.Prescaler = (timer_clock/TIM_CLOCK_FREQ) - 1;
tim2_handler.Init.Period = (TIM_CLOCK_FREQ/1000)*5 - 1; // 5ms
__HAL_RCC_TIM2_RESET();
__HAL_RCC_TIM2_CLK_ENABLE();
NVIC_ClearPendingIRQ(TIM2_IRQn);
NVIC_EnableIRQ(TIM2_IRQn);
HAL_TIMER_MSP_Init(&tim2_handler);
HAL_TIMER_Base_Init(&tim2_handler);
HAL_TIM_ENABLE_IT(&tim2_handler, TIMER_INT_EN_UPD);
HAL_TIMER_Base_Start(tim2_handler.Instance);
}
//中断回调函数
void HAL_TIM_Updeate_Event_Callback(TIM_HandleTypeDef *htim)
{
Timer_Update_Flag = 1;
button_ticks();
//led1_tog();
}
四、测试结果
下载程序后,复位开发板,查看串口输出
操作按键,输出按键的几种状态。
|