分享

STM32CubeMX学习教程之三:GPIO输入之利用SysTick中断给按键去抖

 Allen 图书馆 2019-06-17

本文主要讨论软件去抖。实现方法是通过SysTick中断每1ms对按键进行扫描,当检测到连续的稳定无抖动电平信号(长度可设置)之后,才进行相应的逻辑操作。

软件版本:
STM32CubeMX V4.25.0  
System Workbench V2.4

硬件:OneNet 麒麟座V2.3

在STM32CubeMX中新建项目,选择正确的MCU型号

然后设置RCC和SYS,然后根据板子实际情况设置时钟(麒麟座外部晶振是12M,STM32F103x的最高主频是72M)

然后设置GPIO_Output (连接到LED) 和GPIO_Input(连接到按键)。注意上一篇文章里面按键连接的引脚设置为外部中断模式,这里只需要设置为GPIO_Input就可以了。

GPIO_Output的具体设置如下:

GPIO_Input设置如下

这里按键我用了SW1/2/3/4。

同样修改

Project - setting ,ToolChain/IDE选择 SW4STM32

还要勾选这里

然后生成代码,打开项目。

编辑stm32f1xx_it.h,函数声明那里增加一行 :

void Key_Scan(void);

然后编辑stm32f1xx_it.c 增加如下代码:

  1. /* USER CODE BEGIN 0 */
  2. uint8_t sw1Count,sw2Count,sw3Count,sw4Count;
  3. uint8_t pushFlag1,pushFlag2,pushFlag3,pushFlag4;
  4. extern uint8_t swState1,swState2,swState3,swState4;
  5. void Key_Scan(void)
  6. {
  7. /*检测SW1是否按下 */
  8. if( HAL_GPIO_ReadPin(SW1_GPIO_Port,SW1_Pin) == GPIO_PIN_RESET )
  9. {
  10. sw1Count++; //SW1键按下,计数sw1Count加1
  11. if(sw1Count>=32) //1MS中断一次,sw1Count大于等于32,即按键已按下32ms
  12. {
  13. if(pushFlag1==0) //判断有没有重按键,1为有,0为没有
  14. {
  15. swState1=1; //设置按键标志
  16. sw1Count=0;
  17. pushFlag1=1; //设置重按键标志
  18. }
  19. else
  20. sw1Count=0;
  21. }
  22. else
  23. swState1=0;
  24. }
  25. else //无按键按下
  26. {
  27. sw1Count=0; //清零sw1Count
  28. swState1=0; //清除按键标志
  29. pushFlag1=0; //清除重按键标志
  30. }
  31. /*检测SW2是否按下 */
  32. if( HAL_GPIO_ReadPin(SW2_GPIO_Port,SW2_Pin) == GPIO_PIN_RESET )
  33. {
  34. sw2Count++; //SW2键按下,计数sw2Count加1
  35. if(sw2Count>=32) //1MS中断一次,sw2Count大于等于32,即按键已按下32ms
  36. {
  37. if(pushFlag2==0) //判断有没有重按键,1为有,0为没有
  38. {
  39. swState2=1; //设置按键标志
  40. sw2Count=0;
  41. pushFlag2=1; //设置重按键标志
  42. }
  43. else
  44. sw2Count=0;
  45. }
  46. else
  47. swState2=0;
  48. }
  49. else //无按键按下
  50. {
  51. sw2Count=0; //清零sw2Count
  52. swState2=0; //清除按键标志
  53. pushFlag2=0; //清除重按键标志
  54. }
  55. /*检测SW3是否按下 */
  56. if( HAL_GPIO_ReadPin(SW3_GPIO_Port,SW3_Pin) == GPIO_PIN_RESET )
  57. {
  58. sw3Count++; //SW3键按下,计数sw3Count加1
  59. if(sw3Count>=32) //1MS中断一次,sw3Count大于等于32,即按键已按下32ms
  60. {
  61. if(pushFlag3==0) //判断有没有重按键,1为有,0为没有
  62. {
  63. swState3=1; //设置按键标志
  64. sw3Count=0;
  65. pushFlag3=1; //设置重按键标志
  66. }
  67. else
  68. sw3Count=0;
  69. }
  70. else
  71. swState3=0;
  72. }
  73. else //无按键按下
  74. {
  75. sw3Count=0; //清零sw3Count
  76. swState3=0; //清除按键标志
  77. pushFlag3=0; //清除重按键标志
  78. }
  79. /*检测SW4是否按下 */
  80. if( HAL_GPIO_ReadPin(SW4_GPIO_Port,SW4_Pin) == GPIO_PIN_RESET )
  81. {
  82. sw4Count++; //SW4键按下,计数sw4Count加1
  83. if(sw4Count>=32) //1MS中断一次,sw4Count大于等于32,即按键已按下32ms
  84. {
  85. if(pushFlag4==0) //判断有没有重按键,1为有,0为没有
  86. {
  87. swState4=1; //设置按键标志
  88. sw4Count=0;
  89. pushFlag4=1; //设置重按键标志
  90. }
  91. else
  92. sw4Count=0;
  93. }
  94. else
  95. swState4=0;
  96. }
  97. else //无按键按下
  98. {
  99. sw4Count=0; //清零sw4Count
  100. swState4=0; //清除按键标志
  101. pushFlag4=0; //清除重按键标志
  102. }
  103. }
  104. /* USER CODE END 0 */

然后在SysTick中断处理函数增加一行 void Key_Scan(void);, 代码如下:

  1. /**
  2. * @brief This function handles System tick timer.
  3. */
  4. void SysTick_Handler(void)
  5. {
  6. /* USER CODE BEGIN SysTick_IRQn 0 */
  7. Key_Scan();
  8. /* USER CODE END SysTick_IRQn 0 */
  9. HAL_IncTick();
  10. HAL_SYSTICK_IRQHandler();
  11. /* USER CODE BEGIN SysTick_IRQn 1 */
  12. /* USER CODE END SysTick_IRQn 1 */
  13. }

 在gpio.c 中增加如下两处代码:

  1. /* USER CODE BEGIN 1 */
  2. GPIO_TypeDef* GPIO_PORT[] = {LED1_GPIO_Port,
  3. LED2_GPIO_Port,
  4. LED3_GPIO_Port,
  5. LED4_GPIO_Port};
  6. const uint16_t GPIO_PIN[] = {LED1_Pin,
  7. LED2_Pin,
  8. LED3_Pin,
  9. LED4_Pin};
  10. /* USER CODE END 1 */
  1. /* USER CODE BEGIN 2 */
  2. void LED_Toggle(Led_TypeDef Led)
  3. {
  4. HAL_GPIO_TogglePin(GPIO_PORT[Led], GPIO_PIN[Led]);
  5. }
  6. /* USER CODE END 2 */

然后编辑main.c,增加如下两处代码:

  1. /* USER CODE BEGIN 0 */
  2. uint8_t swState1,swState2,swState3,swState4;
  3. /* USER CODE END 0 */
  1. /* USER CODE BEGIN WHILE */
  2. while (1)
  3. {
  4. if ( swState1 == 1 )
  5. {
  6. LED_Toggle(LED1);
  7. // HAL_GPIO_TogglePin(LED1_GPIO_Port,LED1_Pin);
  8. HAL_Delay(1);
  9. }
  10. if ( swState2 == 1 )
  11. {
  12. LED_Toggle(LED2);
  13. // HAL_GPIO_TogglePin(LED2_GPIO_Port,LED2_Pin);
  14. HAL_Delay(1);
  15. }
  16. if ( swState3 == 1 )
  17. {
  18. LED_Toggle(LED3);
  19. // HAL_GPIO_TogglePin(LED3_GPIO_Port,LED3_Pin);
  20. HAL_Delay(1);
  21. }
  22. if ( swState4 == 1 )
  23. {
  24. LED_Toggle(LED4);
  25. // HAL_GPIO_TogglePin(LED4_GPIO_Port,LED4_Pin);
  26. HAL_Delay(1);
  27. }
  28. /* USER CODE END WHILE */

注意 如下两个语句是等效的,我注释掉了HAL_GPIO_TogglePin()。因为我们使用枚举重新定义了LED状态切换的函数LED_Toggle()。

  1. 1. LED_Toggle(LED1);
  2. 2. HAL_GPIO_TogglePin(LED1_GPIO_Port,LED1_Pin);

然后右键点击项目,选择Properties, Run-Debug Settings, 点击右侧的New,在弹出对话框中选择Ac6 STM32 Debugging。

然后任务栏上点击Run图,当然会报错的,原因请查看另一篇我的博客(https://blog.csdn.net/toopoo/article/details/79680323),所以需要右键点击  项目名Run.cfg ,给它改个名字,

然后右键点击项目树里面的项目名称,选择“Propeties”,然后在Run/Debug Settings-选择项目名-Edit-Main-C/C++Application那里点击“Search Project”,然后选择出现的默认的elf文件:

然后在Debugger-User Defined-Browse 那里选择你自己改名的配置文件:

然后右键点击那个新的cfg文件,选择"Open With - Text Editor", 进行如下更改:

source [find interface/stlink.cfg] 更改为 source [find interface/stlink-v2.cfg]

reset_config srst_only srst_nogate connect_assert_srst 这一行改为 reset_config none 

然后再Run一下,就可以了。

就实现四个按键分别控制LED的开关切换并实现了软件去抖,不会产生误动作了。

本文参考了如下文章:

http://www./study/article-630-1.html

https://blog.csdn.net/yongyooh/article/details/21877227

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多