STM32F103 HAL状态机实现按键消抖,处理按键单击,双击,三击,长按事件,开启定时器中断处理

上传者: zlkk00 | 上传时间: 2025-06-13 11:20:37 | 文件大小: 705KB | 文件类型: ZIP
在嵌入式系统开发领域,STM32微控制器以其高性能、灵活性和丰富的功能而广受欢迎,特别是STM32F103系列。在用户交互设计中,按键是最基本的输入设备之一,而如何高效准确地处理按键事件,包括消抖、单击、双击、三击和长按,是软件开发的关键点。状态机作为一种描述系统行为的设计模式,特别适合处理这类输入事件。 状态机的实现方式很多,本文将探讨如何使用STM32的HAL(硬件抽象层)库来实现一个状态机,以处理按键的不同操作状态。按键消抖是一个必须解决的问题。在实际电路中,按键由于机械特性,在接触时会产生抖动,这会在电气上造成多次触发。通过软件消抖,即在检测到按键状态改变后,延时一小段时间(比如50ms),再次确认按键状态,从而确保检测到的状态是稳定的。 接下来,单击、双击、三击和长按的区分需要对按键的时间间隔进行精确的计时。这通常涉及到定时器中断的使用。通过设置定时器中断,在一定时间间隔内检测按键状态,可以准确判断用户操作。例如,如果检测到按键被按下后,在预定时间内没有再次检测到按键动作,则认为是单击事件;如果在第二个预定时间内检测到按键再次被按下,则认为是双击事件;同样地,三次按键动作则对应为三击事件。长按事件则通常是检测到按键持续被按下的时间超过某个阈值。 在STM32F103的HAL库中,定时器和中断的配置相对简单。需要初始化定时器,设置合适的时钟源和预分频值,从而得到需要的中断触发频率。然后,在中断服务函数中实现按键状态的检查逻辑,根据按键状态的持续时间来触发相应的事件处理函数。 此外,在实现时还要考虑系统的响应效率和实时性。例如,为了避免单击事件被误判为长按,应确保在检测到长按之前,单击事件的逻辑已经处理完毕;同时,避免在处理长按逻辑时,错过对单击和双击的检测。 在代码实现上,状态机的主体结构需要定义多个状态,如等待按键按下、等待单击确认、等待第二次按下、等待第三次按下、长按处理等。每个状态对应一个处理函数,用于执行该状态下应有的逻辑。状态转换的触发条件基于按键事件和定时器中断的返回结果。 根据实际应用需求,还可能需要对状态机进行优化,比如引入防抖时间和多级按键响应逻辑,以提高系统的稳定性和用户体验。通过合理设计状态机和利用STM32F103的HAL库,可以有效地处理各种按键事件,并在嵌入式系统中实现复杂的用户交互逻辑。

文件下载

资源详情

[{"title":"( 95 个子文件 705KB ) STM32F103 HAL状态机实现按键消抖,处理按键单击,双击,三击,长按事件,开启定时器中断处理","children":[{"title":"STM32F103 HAL状态机实现按键消抖,处理按键单击,双击,三击,长按事件,开启定时器中断处理","children":[{"title":"Drivers","children":[{"title":"CMSIS","children":[{"title":"LICENSE.txt <span style='color:#111;'> 11.29KB </span>","children":null,"spread":false},{"title":"Include","children":[{"title":"core_armv8mbl.h <span style='color:#111;'> 95.70KB </span>","children":null,"spread":false},{"title":"core_cm1.h <span style='color:#111;'> 42.44KB </span>","children":null,"spread":false},{"title":"core_cm0plus.h <span style='color:#111;'> 49.28KB </span>","children":null,"spread":false},{"title":"cmsis_armcc.h <span style='color:#111;'> 27.55KB </span>","children":null,"spread":false},{"title":"core_cm0.h <span style='color:#111;'> 41.22KB </span>","children":null,"spread":false},{"title":"cmsis_version.h <span style='color:#111;'> 1.68KB </span>","children":null,"spread":false},{"title":"mpu_armv8.h <span style='color:#111;'> 10.70KB </span>","children":null,"spread":false},{"title":"core_cm4.h <span style='color:#111;'> 120.71KB </span>","children":null,"spread":false},{"title":"core_sc300.h <span style='color:#111;'> 107.93KB </span>","children":null,"spread":false},{"title":"cmsis_iccarm.h <span style='color:#111;'> 27.75KB </span>","children":null,"spread":false},{"title":"core_cm3.h <span style='color:#111;'> 109.23KB </span>","children":null,"spread":false},{"title":"core_armv8mml.h <span style='color:#111;'> 164.96KB </span>","children":null,"spread":false},{"title":"mpu_armv7.h <span style='color:#111;'> 11.44KB </span>","children":null,"spread":false},{"title":"cmsis_gcc.h <span style='color:#111;'> 60.88KB </span>","children":null,"spread":false},{"title":"core_cm23.h <span style='color:#111;'> 102.18KB </span>","children":null,"spread":false},{"title":"core_cm7.h <span style='color:#111;'> 145.11KB </span>","children":null,"spread":false},{"title":"core_sc000.h <span style='color:#111;'> 46.14KB </span>","children":null,"spread":false},{"title":"core_cm33.h <span style='color:#111;'> 171.48KB </span>","children":null,"spread":false},{"title":"tz_context.h <span style='color:#111;'> 2.69KB </span>","children":null,"spread":false},{"title":"cmsis_compiler.h <span style='color:#111;'> 8.80KB </span>","children":null,"spread":false},{"title":"cmsis_armclang.h <span style='color:#111;'> 55.17KB </span>","children":null,"spread":false}],"spread":false},{"title":"Device","children":[{"title":"ST","children":[{"title":"STM32F1xx","children":[{"title":"Source","children":[{"title":"Templates","children":null,"spread":false}],"spread":true},{"title":"LICENSE.txt <span style='color:#111;'> 369B </span>","children":null,"spread":false},{"title":"Include","children":[{"title":"stm32f1xx.h <span style='color:#111;'> 9.87KB </span>","children":null,"spread":false},{"title":"stm32f103xb.h <span style='color:#111;'> 823.13KB </span>","children":null,"spread":false},{"title":"system_stm32f1xx.h <span style='color:#111;'> 1.72KB </span>","children":null,"spread":false}],"spread":true}],"spread":true}],"spread":true}],"spread":true}],"spread":true},{"title":"STM32F1xx_HAL_Driver","children":[{"title":"LICENSE.txt <span style='color:#111;'> 377B </span>","children":null,"spread":false},{"title":"Src","children":[{"title":"stm32f1xx_hal_exti.c <span style='color:#111;'> 14.70KB </span>","children":null,"spread":false},{"title":"stm32f1xx_hal_flash_ex.c <span style='color:#111;'> 34.97KB </span>","children":null,"spread":false},{"title":"stm32f1xx_hal_dma.c <span style='color:#111;'> 26.78KB </span>","children":null,"spread":false},{"title":"stm32f1xx_hal_pwr.c <span style='color:#111;'> 20.26KB </span>","children":null,"spread":false},{"title":"stm32f1xx_hal_tim_ex.c <span style='color:#111;'> 78.03KB </span>","children":null,"spread":false},{"title":"stm32f1xx_hal_rcc.c <span style='color:#111;'> 47.61KB </span>","children":null,"spread":false},{"title":"stm32f1xx_hal_tim.c <span style='color:#111;'> 237.95KB </span>","children":null,"spread":false},{"title":"stm32f1xx_hal_gpio.c <span style='color:#111;'> 20.40KB </span>","children":null,"spread":false},{"title":"stm32f1xx_hal_rcc_ex.c <span style='color:#111;'> 29.17KB </span>","children":null,"spread":false},{"title":"stm32f1xx_hal_cortex.c <span style='color:#111;'> 18.25KB </span>","children":null,"spread":false},{"title":"stm32f1xx_hal_gpio_ex.c <span style='color:#111;'> 3.93KB </span>","children":null,"spread":false},{"title":"stm32f1xx_hal.c <span style='color:#111;'> 19.78KB </span>","children":null,"spread":false},{"title":"stm32f1xx_hal_uart.c <span style='color:#111;'> 132.08KB </span>","children":null,"spread":false},{"title":"stm32f1xx_hal_flash.c <span style='color:#111;'> 28.43KB </span>","children":null,"spread":false}],"spread":false},{"title":"Inc","children":[{"title":"stm32f1xx_hal_flash.h <span style='color:#111;'> 8.61KB </span>","children":null,"spread":false},{"title":"stm32f1xx_hal_gpio_ex.h <span style='color:#111;'> 34.33KB </span>","children":null,"spread":false},{"title":"stm32f1xx_ll_cortex.h <span style='color:#111;'> 23.63KB </span>","children":null,"spread":false},{"title":"stm32f1xx_ll_pwr.h <span style='color:#111;'> 12.29KB </span>","children":null,"spread":false},{"title":"stm32f1xx_hal_pwr.h <span style='color:#111;'> 11.26KB </span>","children":null,"spread":false},{"title":"stm32f1xx_ll_usart.h <span style='color:#111;'> 97.13KB </span>","children":null,"spread":false},{"title":"stm32f1xx_hal_gpio.h <span style='color:#111;'> 11.30KB </span>","children":null,"spread":false},{"title":"stm32f1xx_ll_tim.h <span style='color:#111;'> 159.81KB </span>","children":null,"spread":false},{"title":"stm32f1xx_hal_def.h <span style='color:#111;'> 7.25KB </span>","children":null,"spread":false},{"title":"stm32f1xx_hal_tim.h <span style='color:#111;'> 119.79KB </span>","children":null,"spread":false},{"title":"stm32f1xx_ll_bus.h <span style='color:#111;'> 43.96KB </span>","children":null,"spread":false},{"title":"Legacy","children":[{"title":"stm32_hal_legacy.h <span style='color:#111;'> 230.20KB </span>","children":null,"spread":false}],"spread":false},{"title":"stm32f1xx_ll_rcc.h <span style='color:#111;'> 82.01KB </span>","children":null,"spread":false},{"title":"stm32f1xx_ll_dma.h <span style='color:#111;'> 74.44KB </span>","children":null,"spread":false},{"title":"stm32f1xx_hal_rcc_ex.h <span style='color:#111;'> 96.97KB </span>","children":null,"spread":false},{"title":"stm32f1xx_ll_gpio.h <span style='color:#111;'> 86.02KB </span>","children":null,"spread":false},{"title":"stm32f1xx_ll_exti.h <span style='color:#111;'> 30.47KB </span>","children":null,"spread":false},{"title":"stm32f1xx_hal_uart.h <span style='color:#111;'> 44.58KB </span>","children":null,"spread":false},{"title":"stm32f1xx_hal_flash_ex.h <span style='color:#111;'> 35.17KB </span>","children":null,"spread":false},{"title":"stm32f1xx_hal_dma_ex.h <span style='color:#111;'> 12.10KB </span>","children":null,"spread":false},{"title":"stm32f1xx_hal_rcc.h <span style='color:#111;'> 65.13KB </span>","children":null,"spread":false},{"title":"stm32f1xx_hal.h <span style='color:#111;'> 10.83KB </span>","children":null,"spread":false},{"title":"stm32f1xx_ll_system.h <span style='color:#111;'> 22.91KB </span>","children":null,"spread":false},{"title":"stm32f1xx_ll_utils.h <span style='color:#111;'> 8.40KB </span>","children":null,"spread":false},{"title":"stm32f1xx_hal_dma.h <span style='color:#111;'> 17.34KB </span>","children":null,"spread":false},{"title":"stm32f1xx_hal_cortex.h <span style='color:#111;'> 16.57KB </span>","children":null,"spread":false},{"title":"stm32f1xx_hal_tim_ex.h <span style='color:#111;'> 9.35KB </span>","children":null,"spread":false},{"title":"stm32f1xx_hal_exti.h <span style='color:#111;'> 11.54KB </span>","children":null,"spread":false}],"spread":false}],"spread":true}],"spread":true},{"title":"Core","children":[{"title":"Src","children":[{"title":"system_stm32f1xx.c <span style='color:#111;'> 14.23KB </span>","children":null,"spread":false},{"title":"stm32f1xx_hal_msp.c <span style='color:#111;'> 2.27KB </span>","children":null,"spread":false},{"title":"stm32f1xx_it.c <span style='color:#111;'> 5.59KB </span>","children":null,"spread":false},{"title":"tim.c <span style='color:#111;'> 2.91KB </span>","children":null,"spread":false},{"title":"gpio.c <span style='color:#111;'> 1.74KB </span>","children":null,"spread":false},{"title":"main.c <span style='color:#111;'> 5.23KB </span>","children":null,"spread":false},{"title":"usart.c <span style='color:#111;'> 3.26KB </span>","children":null,"spread":false}],"spread":true},{"title":"Inc","children":[{"title":"tim.h <span style='color:#111;'> 1.31KB </span>","children":null,"spread":false},{"title":"gpio.h <span style='color:#111;'> 1.28KB </span>","children":null,"spread":false},{"title":"usart.h <span style='color:#111;'> 1.33KB </span>","children":null,"spread":false},{"title":"main.h <span style='color:#111;'> 2.17KB </span>","children":null,"spread":false},{"title":"stm32f1xx_hal_conf.h <span style='color:#111;'> 15.35KB </span>","children":null,"spread":false},{"title":"stm32f1xx_it.h <span style='color:#111;'> 1.91KB </span>","children":null,"spread":false}],"spread":true}],"spread":true},{"title":"Key","children":[{"title":"key.c <span style='color:#111;'> 4.80KB </span>","children":null,"spread":false},{"title":".vscode","children":[{"title":"settings.json <span style='color:#111;'> 67B </span>","children":null,"spread":false}],"spread":true},{"title":"key.h <span style='color:#111;'> 1.29KB </span>","children":null,"spread":false}],"spread":true},{"title":"button.ioc <span style='color:#111;'> 4.81KB </span>","children":null,"spread":false},{"title":".mxproject <span style='color:#111;'> 7.59KB </span>","children":null,"spread":false},{"title":"MDK-ARM","children":[{"title":"RTE","children":[{"title":"_button","children":[{"title":"RTE_Components.h <span style='color:#111;'> 336B </span>","children":null,"spread":false}],"spread":true}],"spread":true},{"title":"button.uvprojx <span style='color:#111;'> 19.25KB </span>","children":null,"spread":false},{"title":"DebugConfig","children":[{"title":"button_STM32F103CB_1.0.0.dbgconf <span style='color:#111;'> 2.35KB </span>","children":null,"spread":false}],"spread":true},{"title":"button.uvguix.Administrator <span style='color:#111;'> 175.28KB </span>","children":null,"spread":false},{"title":"button","children":[{"title":"button.hex <span style='color:#111;'> 35.63KB </span>","children":null,"spread":false}],"spread":true},{"title":"button.uvoptx <span style='color:#111;'> 16.27KB </span>","children":null,"spread":false},{"title":"startup_stm32f103xb.s <span style='color:#111;'> 12.06KB </span>","children":null,"spread":false},{"title":"button.uvguix.Tom <span style='color:#111;'> 89.44KB </span>","children":null,"spread":false}],"spread":true}],"spread":true}],"spread":true}]

评论信息

免责申明

【只为小站】的资源来自网友分享,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,【只为小站】 无法对用户传输的作品、信息、内容的权属或合法性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论 【只为小站】 经营者是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。
本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二条之规定,若资源存在侵权或相关问题请联系本站客服人员,zhiweidada#qq.com,请把#换成@,本站将给予最大的支持与配合,做到及时反馈和处理。关于更多版权及免责申明参见 版权及免责申明