HAL STM32通过multi_button库处理按键事件

这篇具有很好参考价值的文章主要介绍了HAL STM32通过multi_button库处理按键事件。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

HAL STM32通过multi_button库处理按键事件

  • 📍作者:0x1abin的multi_button库:https://github.com/0x1abin/MultiButton

📘MultiButton简介

MultiButton 是一个小巧简单易用的事件驱动型按键驱动模块,可无限量扩展按键,按键事件的回调异步处理方式可以简化你的程序结构,去除冗余的按键处理硬编码,让你的按键业务逻辑更清晰。

  • 🔖 该库驱动代码纯C语言实现,可以移植到任意需要使用的地方。

🔑使用方法

  1. 先申请一个按键结构
struct Button button1;
  1. 初始化按键对象,绑定按键的GPIO电平读取接口read_button_pin() ,后一个参数设置有效触发电平。
button_init(&button1, read_button_pin, 0, 0);//绑定按键,读取按键引脚状态,有效触发电平、按键ID
  1. 注册按键事件
button_attach(&button1, SINGLE_CLICK, Callback_SINGLE_CLICK_Handler);
button_attach(&button1, DOUBLE_CLICK, Callback_DOUBLE_Click_Handler);


4. 启动按键文章来源地址https://www.toymoban.com/news/detail-828526.html

button_start(&button1);
  1. 设置一个5ms间隔的定时器循环调用后台处理函数
while(1) {
    ...
    if(timer_ticks == 5) {
        timer_ticks = 0;

        button_ticks();
    }
}

🛠按键引脚配置

  • 🌿通过STM32CubeMX,将按键引脚配置成上拉,那么有效触发电平就需要设置为0.
    HAL STM32通过multi_button库处理按键事件,STM32CubeMX自动配置工程系列,stm32,按键处理,multi_button

📗通过按键事件查询,获取按键状态

  • 🌿在滴答定时器中断函数(SysTick_Handler)中添加button_tick();
/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2024 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "usart.h"
#include "usb_device.h"
#include "gpio.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "stdio.h"
#include <stdarg.h>
#include "usbd_cdc_if.h"
#include "multi_button.h"

/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/

/* USER CODE BEGIN PV */
struct Button btn1;
/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
void usb_printf(const char *fmt, ...)
{
    char buf[128];//自定义缓冲区大小
    va_list args;
    va_start(args, fmt);
    vsnprintf(buf, sizeof(buf), fmt, args);
    va_end(args);
    CDC_Transmit_FS((uint8_t *)buf, strlen(buf));
}



void button_tick(void)
{
    static uint8_t tickstart = 0;
    tickstart++;
    if(tickstart < 5)
        return;
    tickstart = 0;
    button_ticks();
}
//按键状态读取接口  按键输入模式 ReadInputDataBit
uint8_t Read_Button_GPIO(uint8_t button_id)
{
    // you can share the GPIO read function with multiple Buttons
    switch(button_id) {
        case 1:
            return HAL_GPIO_ReadPin(KEY1_GPIO_Port, KEY1_Pin);
				case 2:
            return HAL_GPIO_ReadPin(KEY2_GPIO_Port, KEY2_Pin);
        default:
            return 0;
    }
}


/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{
    /* USER CODE BEGIN 1 */
    uint32_t TimerUART;
    static PressEvent btn1_event_val;


    /* USER CODE END 1 */

    /* MCU Configuration--------------------------------------------------------*/

    /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
    HAL_Init();

    /* USER CODE BEGIN Init */

    /* USER CODE END Init */

    /* Configure the system clock */
    SystemClock_Config();

    /* USER CODE BEGIN SysInit */

    /* USER CODE END SysInit */

    /* Initialize all configured peripherals */
    MX_GPIO_Init();
    MX_USART1_UART_Init();
    MX_USB_DEVICE_Init();
    /* USER CODE BEGIN 2 */
    uint32_t Main_Fosc = HAL_RCC_GetSysClockFreq();
    printf("Main_Fosc:%dHz \r\n", Main_Fosc);
    TimerUART = HAL_GetTick();
    usb_printf("STM32F427 SysClockFreq:%d \r\n", Main_Fosc);
    button_init(&btn1, Read_Button_GPIO, 0, 1);
    button_start(&btn1);
  
    /* USER CODE END 2 */

    /* Infinite loop */
    /* USER CODE BEGIN WHILE */
    while(1) {
        /* USER CODE END WHILE */

        /* USER CODE BEGIN 3 */
        if((HAL_GetTick() - TimerUART) > 1000) {
            HAL_GPIO_TogglePin(LED1_GPIO_Port, LED1_Pin | LED2_Pin | LED3_Pin); //翻转电平,LED翻转
            printf("STM32F427 SysClockFreq:%d \r\n", Main_Fosc);
            usb_printf("STM32F427 SysClockFreq:%d \r\n", Main_Fosc);
            TimerUART = HAL_GetTick();
        }
        if(btn1_event_val != get_button_event(&btn1)) {
            btn1_event_val = get_button_event(&btn1);

            if(btn1_event_val == PRESS_DOWN) {
                // printf("STM32F427 KEY1 PRESS_DOWN \r\n");
                usb_printf("STM32F427 KEY1 PRESS_DOWN \r\n");
            } else if(btn1_event_val == PRESS_UP) {
                // printf("STM32F427 KEY1 PRESS_UP \r\n");
                usb_printf("STM32F427 KEY1 PRESS_UP \r\n");
            } else if(btn1_event_val == LONG_PRESS_HOLD) {
                // printf("STM32F427 LONG_PRESS_HOLD\r\n"/);
                usb_printf("STM32F427 LONG_PRESS_HOLD\r\n");
            }
        }
    }
    /* USER CODE END 3 */
}

/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{
    RCC_OscInitTypeDef RCC_OscInitStruct = {0};
    RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

    /** Configure the main internal regulator output voltage
    */
    __HAL_RCC_PWR_CLK_ENABLE();
    __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE3);

    /** Initializes the RCC Oscillators according to the specified parameters
    * in the RCC_OscInitTypeDef structure.
    */
    RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
    RCC_OscInitStruct.HSEState = RCC_HSE_ON;
    RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
    RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
    RCC_OscInitStruct.PLL.PLLM = 16;
    RCC_OscInitStruct.PLL.PLLN = 384;
    RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV4;
    RCC_OscInitStruct.PLL.PLLQ = 8;
    if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
        Error_Handler();
    }

    /** Initializes the CPU, AHB and APB buses clocks
    */
    RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK
                                  | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
    RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
    RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
    RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
    RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;

    if(HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_3) != HAL_OK) {
        Error_Handler();
    }
}

/* USER CODE BEGIN 4 */

/* USER CODE END 4 */

/**
  * @brief  This function is executed in case of error occurrence.
  * @retval None
  */
void Error_Handler(void)
{
    /* USER CODE BEGIN Error_Handler_Debug */
    /* User can add his own implementation to report the HAL error return state */
    __disable_irq();
    while(1) {
    }
    /* USER CODE END Error_Handler_Debug */
}

#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
    /* USER CODE BEGIN 6 */
    /* User can add his own implementation to report the file name and line number,
       ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
    /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */

📓通过注册按键事件,获取按键状态

/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2024 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "usart.h"
#include "usb_device.h"
#include "gpio.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "stdio.h"
#include <stdarg.h>
#include "usbd_cdc_if.h"
#include "multi_button.h"

/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/

/* USER CODE BEGIN PV */
struct Button btn1;
/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
void BTN1_PRESS_DOWN_Handler(void* btn);
void BTN1_PRESS_UP_Handler(void* btn);
void BTN1_SINGLE_Click_Handler(void* btn);
void BTN1_DOUBLE_Click_Handler(void* btn);
void BTN1_LONG_RRESS_START_Handler(void* btn);
void BTN1_LONG_PRESS_HOLD_Handler(void* btn);
/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
void usb_printf(const char *fmt, ...)
{
    char buf[128];//自定义缓冲区大小
    va_list args;
    va_start(args, fmt);
    vsnprintf(buf, sizeof(buf), fmt, args);
    va_end(args);
    CDC_Transmit_FS((uint8_t *)buf, strlen(buf));
}



void button_tick(void)
{
    static uint8_t tickstart = 0;
    tickstart++;
    if(tickstart < 5)
        return;
    tickstart = 0;
    button_ticks();
}
//按键状态读取接口  按键输入模式 ReadInputDataBit
uint8_t Read_Button_GPIO(uint8_t button_id)
{
    // you can share the GPIO read function with multiple Buttons
    switch(button_id) {
        case 1:
            return HAL_GPIO_ReadPin(KEY1_GPIO_Port, KEY1_Pin);
        case 2:
            return HAL_GPIO_ReadPin(KEY2_GPIO_Port, KEY2_Pin);
        default:
            return 0;
    }
}


/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{
    /* USER CODE BEGIN 1 */
    uint32_t TimerUART;
    // static PressEvent btn1_event_val;


    /* USER CODE END 1 */

    /* MCU Configuration--------------------------------------------------------*/

    /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
    HAL_Init();

    /* USER CODE BEGIN Init */

    /* USER CODE END Init */

    /* Configure the system clock */
    SystemClock_Config();

    /* USER CODE BEGIN SysInit */

    /* USER CODE END SysInit */

    /* Initialize all configured peripherals */
    MX_GPIO_Init();
    MX_USART1_UART_Init();
    MX_USB_DEVICE_Init();
    /* USER CODE BEGIN 2 */
    uint32_t Main_Fosc = HAL_RCC_GetSysClockFreq();
    printf("Main_Fosc:%dHz \r\n", Main_Fosc);
    TimerUART = HAL_GetTick();
    usb_printf("STM32F427 SysClockFreq:%d \r\n", Main_Fosc);
    button_init(&btn1, Read_Button_GPIO, 0, 1);
    // 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_RRESS_START_Handler); //长按开始
    // button_attach(&btn1, LONG_PRESS_HOLD, BTN1_LONG_PRESS_HOLD_Handler);//一直长按状态
    button_start(&btn1);
    /* USER CODE END 2 */

    /* Infinite loop */
    /* USER CODE BEGIN WHILE */
    while(1) {
        /* USER CODE END WHILE */

        /* USER CODE BEGIN 3 */
        if((HAL_GetTick() - TimerUART) > 1000) {
            HAL_GPIO_TogglePin(LED1_GPIO_Port, LED1_Pin | LED2_Pin | LED3_Pin); //翻转电平,LED翻转
            printf("STM32F427 SysClockFreq:%d \r\n", Main_Fosc);
            usb_printf("STM32F427 SysClockFreq:%d \r\n", Main_Fosc);
            TimerUART = HAL_GetTick();
        }

    }
    /* USER CODE END 3 */
}

/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{
    RCC_OscInitTypeDef RCC_OscInitStruct = {0};
    RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

    /** Configure the main internal regulator output voltage
    */
    __HAL_RCC_PWR_CLK_ENABLE();
    __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE3);

    /** Initializes the RCC Oscillators according to the specified parameters
    * in the RCC_OscInitTypeDef structure.
    */
    RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
    RCC_OscInitStruct.HSEState = RCC_HSE_ON;
    RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
    RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
    RCC_OscInitStruct.PLL.PLLM = 16;
    RCC_OscInitStruct.PLL.PLLN = 384;
    RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV4;
    RCC_OscInitStruct.PLL.PLLQ = 8;
    if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
        Error_Handler();
    }

    /** Initializes the CPU, AHB and APB buses clocks
    */
    RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK
                                  | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
    RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
    RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
    RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
    RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;

    if(HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_3) != HAL_OK) {
        Error_Handler();
    }
}

/* USER CODE BEGIN 4 */
void BTN1_PRESS_DOWN_Handler(void* btn)
{
    // printf("STM32F427 KEY1 PRESS_DOWN \r\n");
    usb_printf("STM32F427 KEY1 PRESS_DOWN \r\n");
}

void BTN1_PRESS_UP_Handler(void* btn)
{
    // printf("STM32F427 KEY1 PRESS_UP \r\n");
    usb_printf("STM32F427 KEY1 PRESS_UP \r\n");
}
void BTN1_SINGLE_Click_Handler(void* btn)
{
    // printf("STM32F427 KEY1 SINGLE_Click \r\n");
    usb_printf("STM32F427 KEY1 SINGLE_Click \r\n");
}
void BTN1_DOUBLE_Click_Handler(void* btn)
{
    // printf("STM32F427 KEY1 DOUBLE_Click \r\n");
    usb_printf("STM32F427 KEY1 DOUBLE_Click \r\n");
}
void BTN1_LONG_RRESS_START_Handler(void* btn)
{
    // printf("STM32F427 KEY1 LONG_RRESS_START \r\n");
    usb_printf("STM32F427 KEY1 LONG_RRESS_START \r\n");
}
void BTN1_LONG_PRESS_HOLD_Handler(void* btn)
{
    // printf("STM32F427 KEY1 LONG_PRESS_HOLD \r\n");
    usb_printf("STM32F427 KEY1 LONG_PRESS_HOLD \r\n");
}
/* USER CODE END 4 */

/**
  * @brief  This function is executed in case of error occurrence.
  * @retval None
  */
void Error_Handler(void)
{
    /* USER CODE BEGIN Error_Handler_Debug */
    /* User can add his own implementation to report the HAL error return state */
    __disable_irq();
    while(1) {
    }
    /* USER CODE END Error_Handler_Debug */
}

#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
    /* USER CODE BEGIN 6 */
    /* User can add his own implementation to report the file name and line number,
       ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
    /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */

📝通过注册按键事件,统一查询获取按键状态

/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2024 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "usart.h"
#include "usb_device.h"
#include "gpio.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "stdio.h"
#include <stdarg.h>
#include "usbd_cdc_if.h"
#include "multi_button.h"

/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/

/* USER CODE BEGIN PV */
struct Button btn1;
/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
//void BTN1_PRESS_DOWN_Handler(void* btn);
//void BTN1_PRESS_UP_Handler(void* btn);
//void BTN1_SINGLE_Click_Handler(void* btn);
//void BTN1_DOUBLE_Click_Handler(void* btn);
//void BTN1_LONG_RRESS_START_Handler(void* btn);
//void BTN1_LONG_PRESS_HOLD_Handler(void* btn);
void button_callback(void *btn);//
/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
void usb_printf(const char *fmt, ...)
{
    char buf[128];//自定义缓冲区大小
    va_list args;
    va_start(args, fmt);
    vsnprintf(buf, sizeof(buf), fmt, args);
    va_end(args);
    CDC_Transmit_FS((uint8_t *)buf, strlen(buf));
}



void button_tick(void)
{
    static uint8_t tickstart = 0;
    tickstart++;
    if(tickstart < 5)
        return;
    tickstart = 0;
    button_ticks();
}
//按键状态读取接口  按键输入模式 ReadInputDataBit
uint8_t Read_Button_GPIO(uint8_t button_id)
{
    // you can share the GPIO read function with multiple Buttons
    switch(button_id) {
        case 1:
            return HAL_GPIO_ReadPin(KEY1_GPIO_Port, KEY1_Pin);
        case 2:
            return HAL_GPIO_ReadPin(KEY2_GPIO_Port, KEY2_Pin);
        default:
            return 0;
    }
}


/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{
    /* USER CODE BEGIN 1 */
//    uint32_t TimerUART;
    // static PressEvent btn1_event_val;


    /* USER CODE END 1 */

    /* MCU Configuration--------------------------------------------------------*/

    /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
    HAL_Init();

    /* USER CODE BEGIN Init */

    /* USER CODE END Init */

    /* Configure the system clock */
    SystemClock_Config();

    /* USER CODE BEGIN SysInit */

    /* USER CODE END SysInit */

    /* Initialize all configured peripherals */
    MX_GPIO_Init();
    MX_USART1_UART_Init();
    MX_USB_DEVICE_Init();
    /* USER CODE BEGIN 2 */
    uint32_t Main_Fosc = HAL_RCC_GetSysClockFreq();
    printf("Main_Fosc:%dHz \r\n", Main_Fosc);
//    TimerUART = HAL_GetTick();
    usb_printf("STM32F427 SysClockFreq:%d \r\n", Main_Fosc);
    button_init(&btn1, Read_Button_GPIO, 0, 1);
    // button_attach(&btn1, PRESS_DOWN, button_callback); //按下
    // button_attach(&btn1, PRESS_UP, button_callback);  //按起
    //	button_attach(&btn1, PRESS_REPEAT,button_callback);
    button_attach(&btn1, SINGLE_CLICK, button_callback); //单击
    button_attach(&btn1, DOUBLE_CLICK, button_callback);  //双击
    button_attach(&btn1, LONG_PRESS_START, button_callback); //长按开始
    // button_attach(&btn1, LONG_PRESS_HOLD, button_callback);//一直长按状态
    button_start(&btn1);
    /* USER CODE END 2 */

    /* Infinite loop */
    /* USER CODE BEGIN WHILE */
    while(1) {
        /* USER CODE END WHILE */

        /* USER CODE BEGIN 3 */
//        if((HAL_GetTick() - TimerUART) > 1000) {
//            HAL_GPIO_TogglePin(LED1_GPIO_Port, LED1_Pin | LED2_Pin | LED3_Pin); //翻转电平,LED翻转
//            printf("STM32F427 SysClockFreq:%d \r\n", Main_Fosc);
//            usb_printf("STM32F427 SysClockFreq:%d \r\n", Main_Fosc);
//            TimerUART = HAL_GetTick();
//        }
        /*
        if(btn1_event_val != get_button_event(&btn1)) {
        btn1_event_val = get_button_event(&btn1);

        if(btn1_event_val == PRESS_DOWN) {
        // printf("STM32F427 KEY1 PRESS_DOWN \r\n");
        usb_printf("STM32F427 KEY1 PRESS_DOWN \r\n");
        } else if(btn1_event_val == PRESS_UP) {
        // printf("STM32F427 KEY1 PRESS_UP \r\n");
        usb_printf("STM32F427 KEY1 PRESS_UP \r\n");
        } else if(btn1_event_val == LONG_PRESS_HOLD) {
        // printf("STM32F427 LONG_PRESS_HOLD\r\n"/);
        usb_printf("STM32F427 LONG_PRESS_HOLD\r\n");
        }

        }
        */
    }
    /* USER CODE END 3 */
}

/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{
    RCC_OscInitTypeDef RCC_OscInitStruct = {0};
    RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

    /** Configure the main internal regulator output voltage
    */
    __HAL_RCC_PWR_CLK_ENABLE();
    __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE3);

    /** Initializes the RCC Oscillators according to the specified parameters
    * in the RCC_OscInitTypeDef structure.
    */
    RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
    RCC_OscInitStruct.HSEState = RCC_HSE_ON;
    RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
    RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
    RCC_OscInitStruct.PLL.PLLM = 16;
    RCC_OscInitStruct.PLL.PLLN = 384;
    RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV4;
    RCC_OscInitStruct.PLL.PLLQ = 8;
    if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
        Error_Handler();
    }

    /** Initializes the CPU, AHB and APB buses clocks
    */
    RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK
                                  | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
    RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
    RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
    RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
    RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;

    if(HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_3) != HAL_OK) {
        Error_Handler();
    }
}

/* USER CODE BEGIN 4 */
void BTN1_PRESS_DOWN_Handler(void* btn)
{
    // printf("STM32F427 KEY1 PRESS_DOWN \r\n");
    usb_printf("STM32F427 KEY1 PRESS_DOWN \r\n");
}

void BTN1_PRESS_UP_Handler(void* btn)
{
    // printf("STM32F427 KEY1 PRESS_UP \r\n");
    usb_printf("STM32F427 KEY1 PRESS_UP \r\n");
}
void BTN1_SINGLE_Click_Handler(void* btn)
{
    // printf("STM32F427 KEY1 SINGLE_Click \r\n");
    usb_printf("STM32F427 KEY1 SINGLE_Click \r\n");
}
void BTN1_DOUBLE_Click_Handler(void* btn)
{
    // printf("STM32F427 KEY1 DOUBLE_Click \r\n");
    usb_printf("STM32F427 KEY1 DOUBLE_Click \r\n");
}
void BTN1_LONG_RRESS_START_Handler(void* btn)
{
    // printf("STM32F427 KEY1 LONG_RRESS_START \r\n");
    usb_printf("STM32F427 KEY1 LONG_RRESS_START \r\n");
}
void BTN1_LONG_PRESS_HOLD_Handler(void* btn)
{
    // printf("STM32F427 KEY1 LONG_PRESS_HOLD \r\n");
    usb_printf("STM32F427 KEY1 LONG_PRESS_HOLD \r\n");
}

void button_callback(void *btn)
{
static PressEvent btn1_event_val;
//    uint32_t btn_event_val;    
//    btn_event_val = get_button_event((struct Button *)btn);
//	btn_event_val = get_button_event(&btn1);
if(btn1_event_val != get_button_event(&btn1)) {
			btn1_event_val = get_button_event(&btn1);
    switch(btn1_event_val)
    {
      case PRESS_DOWN:
          printf("---> KEY1 press down! <---\r\n"); 
			usb_printf("---> KEY1 press down! <---\r\n"); 
      break; 

      case PRESS_UP: 
          printf("***> KEY1 press up! <***\r\n");
			usb_printf("---> KEY1 press up! <---\r\n");
      break; 

      case PRESS_REPEAT: 
          printf("---> KEY1 press repeat! <---\r\n");
      break; 

      case SINGLE_CLICK: 
          printf("---> KEY1 single click! <---\r\n");
			usb_printf("---> KEY1 single click! <---\r\n");
      break; 

      case DOUBLE_CLICK: 
          printf("***> KEY1 double click! <***\r\n");
			usb_printf("---> KEY1 double click! <---\r\n");
      break; 

      case LONG_PRESS_START: 
          printf("---> KEY1 long press start! <---\r\n");
					usb_printf("---> KEY1 long press start! <---\r\n");
      break; 

      case LONG_PRESS_HOLD: 
          printf("***> KEY1 long press hold! <***\r\n");
					usb_printf("---> KEY1 press down! <---\r\n");
      break; 
			default:
			break;
	}
}
}
/* USER CODE END 4 */

/**
  * @brief  This function is executed in case of error occurrence.
  * @retval None
  */
void Error_Handler(void)
{
    /* USER CODE BEGIN Error_Handler_Debug */
    /* User can add his own implementation to report the HAL error return state */
    __disable_irq();
    while(1) {
    }
    /* USER CODE END Error_Handler_Debug */
}

#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
    /* USER CODE BEGIN 6 */
    /* User can add his own implementation to report the file name and line number,
       ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
    /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */

📚相关测试工程

链接:https://pan.baidu.com/s/1DRqSkKnj6Kznh3izOLrlWQ?pwd=awf4 
提取码:awf4
链接:https://pan.baidu.com/s/1rviDB1MHQTQZkIBr590wXQ?pwd=11tu 
提取码:11tu
链接:https://pan.baidu.com/s/1GkQE7OZJnBn8t6gNXMahEw?pwd=vhw5 
提取码:vhw5

到了这里,关于HAL STM32通过multi_button库处理按键事件的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • STM32:基于HAL 库的外部中断按键以及消抖

    第一次写博客,有错误与问题欢迎指正。 这部分直接上代码,假设PB5为输入端且上拉,PB11为输出。 在CubeMx中配置完引脚(下降沿触发)并在NVIC中开启外部中断后,我们注意到,上一个程序有5个要素(我写了注释的5个): 中断回调函数HAL_GPIO_EXTI_Callback是一个虚函数,我们

    2024年04月10日
    浏览(46)
  • 蓝桥杯STM32 G431 hal库开发速成——按键+PWM综合案例——按键控制PWM驱动舵机

    适用于学习了TIM输出比较(PWM)跟GPIO输入(按键)的新手作为练习的综合项目! PWM(Pulse Width Modulation,脉冲宽度调制)是一种常用的技术,用于通过调节电信号的脉冲宽度(即脉冲的持续时间)来控制模拟系统的电源。在数字电子系统中,由于只能输出固定的高(通常为

    2024年01月21日
    浏览(57)
  • STM32HAL库定时器中断、按键中断、串口中断、按键消抖和低通滤波算法详解

    本文将详细介绍STM32HAL库中的定时器中断、按键中断、串口中断的使用方法,以及按键消抖和低通滤波算法的实现。希望能对您的STM32开发提供帮助。 目录 1. 定时器中断 2. 按键中断 3. 串口中断 4. 按键消抖 5. 低通滤波算法 一、定时器中断 定时器中断是STM32开发中常用的一种

    2024年02月15日
    浏览(49)
  • 正点原子STM32F103精英版+HAL库实现4×4矩阵按键检测

    首先声明,本人小白一枚,所做的工作都是借鉴网上的大佬+自己摸索,但是都是亲测实际有效的。 因为所需要的功能开发板自带按键不够用,所以购买了4×4矩阵按键,当时购买的时候以为一个按键对应一个IO口,后来发现不是这样的,会浪费太多的IO口,4×4矩阵键盘用8个

    2024年02月06日
    浏览(55)
  • 【stm32】hal库学习笔记-GPIO按键控制LED和蜂鸣器(超详细!)

    注:本学习笔记基于stm32f4系列 使用的开发板为正点原子stmf407ZGT6探索者开发板 GPIO引脚使用时,可输入或输出数字信号 例如: 检测按键输入信号(Read_Pin)输出信号(Write_Pin) 输出信号点亮或熄灭LED GPIO引脚内部结构图 GPIO状态 输入浮空状态 :上拉下拉电阻均不使用(复位时

    2024年01月20日
    浏览(49)
  • STM32-03基于HAL库(CubeMX+MDK+Proteus)输入检测案例(按键控制LED)

    搭建完成开发STM32开发环境之后,开始GPIO的学习。上一节已经测试了使用GPIO的输出模式,实现LED点亮的功能。 本次案例实现GPIO的输入检测功能,实现按键状态检测,通过按键状态控制LED。 需求分析: 接线:PA6-KEY1、PA7-KEY2、PA0-LED1、PA1-LED2; KEY1控制LED1、KEY2控制LED2; 按键闭

    2024年04月23日
    浏览(52)
  • STM32 HAL库实现三位数码管显示(74HC595+按键+蜂鸣器)

    1、加强数码管学习,实现数码显示变量数据(三位数的显示); 2、3位数码+2个按键+蜂鸣器实现模拟电磁炉功率调节及显示; 关于74HC595的STM32F103的驱动以及数码的显示等请查阅我写的上一篇博客:https://blog.csdn.net/luojuan198780/article/details/136653377 实验效果参考下面网址视频:

    2024年03月17日
    浏览(84)
  • STM32之通过按键控制LED灯亮灭

    之前我们已经实现了LED灯的亮灭,我们对LED灯的结构应该有一个大致的了解。接下来我们对按键控制led进行学习。 首先我们要了解一下按键的结构。按键结构如图。  由此可知,此处我们有两种按键,一种是普通的按键KEY0, KEY1(KEY2),他们连接的串口分别是PE4, PE3(PE2),对应

    2024年02月08日
    浏览(50)
  • STM32通过HAL库实现DMX512控制灯

    关于DMX512协议的解释下面这篇文章写的十分详尽: 链接:作者:夏沫の浅雨 通过串口模拟 通过上文我们可以得知,数据帧需要 1位低电平+8位数据位+2位高电平 ,这种格式与串口通信协议的格式可以说是几乎一模一样,因为平日基本上都是使用串口8N1格式。因此,若是不考虑

    2024年04月12日
    浏览(26)
  • STM32(HAL库)通过ADC读取MQ2数据

    目录 1、简介 2、CubeMX初始化配置 2.1 基础配置 2.1.1 SYS配置  2.1.2 RCC配置 2.2 ADC外设配置 2.3 串口外设配置  2.4 项目生成  3、KEIL端程序整合 3.1 串口重映射 3.2 ADC数据采集 3.3 主函数代 3.4 效果展示 本文通过STM32F103C8T6单片机通过HAL库方式对MQ2烟雾传感器进行数据的读取,并通过

    2024年02月16日
    浏览(35)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包