你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

STM32F4(KEY)

[复制链接]
XinLiYF 发布时间:2018-3-9 20:32
STM32F4(KEY)
GitHub仓库:http://github.com/XinLiGitHub/STM32F4xx_KEY_Example
PS:博文不再更新,后续更新会在GitHub仓库进行。


       在实际的项目开发过程中,常常会遇到硬件电路的修改,然后修改的部分就需要修改驱动程序。想这样需求该来该去是程序员们最烦闷的事情(重复劳动痛不欲生啊~)。为了避免或减少重复劳动,就需要在程序的架构上下功夫。接下来以最常见的KEY驱动程序为例,进行程序结构设计。


1,开发环境

     1,固件库:STM32F4xx_DSP_StdPeriph_Lib_V1.8.0
     2,编译器:ARMCC V5.06
     3,IDE:Keil uVision5
     4,操作系统:Windows 10 专业版


2,程序源码
      KEY.h文件
  1. /**
  2.   ******************************************************************************
  3.   * @file    KEY.h
  4.   * @author  XinLi
  5.   * @version v1.0
  6.   * @date    24-October-2017
  7.   * @brief   Header file for KEY.c module.
  8.   ******************************************************************************
  9.   * @attention
  10.   *
  11.   * <h2><center>Copyright © 2017 XinLi</center></h2>
  12.   *
  13.   * This program is free software: you can redistribute it and/or modify
  14.   * it under the terms of the GNU General Public License as published by
  15.   * the Free Software Foundation, either version 3 of the License, or
  16.   * (at your option) any later version.
  17.   *
  18.   * This program is distributed in the hope that it will be useful,
  19.   * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20.   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21.   * GNU General Public License for more details.
  22.   *
  23.   * You should have received a copy of the GNU General Public License
  24.   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  25.   *
  26.   ******************************************************************************
  27.   */

  28. #ifndef __KEY_H
  29. #define __KEY_H

  30. #ifdef __cplusplus
  31. extern "C" {
  32. #endif

  33. /* Header includes -----------------------------------------------------------*/
  34. #include "stm32f4xx.h"

  35. /* Macro definitions ---------------------------------------------------------*/
  36. #define KEYn                            (3)

  37. #define KEY1_RCC_AHB1Periph_GPIO        RCC_AHB1Periph_GPIOC
  38. #define KEY1_GPIO                       GPIOC
  39. #define KEY1_GPIO_Pin                   GPIO_Pin_2

  40. #define KEY2_RCC_AHB1Periph_GPIO        RCC_AHB1Periph_GPIOC
  41. #define KEY2_GPIO                       GPIOC
  42. #define KEY2_GPIO_Pin                   GPIO_Pin_3

  43. #define KEY3_RCC_AHB1Periph_GPIO        RCC_AHB1Periph_GPIOC
  44. #define KEY3_GPIO                       GPIOC
  45. #define KEY3_GPIO_Pin                   GPIO_Pin_4

  46. #define KEY_RCC_APB1Periph_TIM          RCC_APB1Periph_TIM2
  47. #define KEY_TIM                         TIM2
  48. #define KEY_TIM_Prescaler               (83)   /*!< Clock divided to 1MHz. */
  49. #define KEY_TIM_Period                  (4999) /*!< 5ms timer interrupt. */
  50. #define KEY_TIM_IRQn                    TIM2_IRQn
  51. #define KEY_TIM_IRQHandler              TIM2_IRQHandler
  52. #define KEY_TIM_IRQ_PreemptionPriority  (0)
  53. #define KEY_TIM_IRQ_SubPriority         (0)

  54. #define KEY_PRESS_STATUS                Bit_SET

  55. /* Type definitions ----------------------------------------------------------*/
  56. typedef enum
  57. {
  58.   KEY_Pin1 = 0,
  59.   KEY_Pin2 = 1,
  60.   KEY_Pin3 = 2
  61. }KEY_Pin;

  62. typedef enum
  63. {
  64.   KEY_NoPress    = 0,
  65.   KEY_ShortPress = 1,
  66.   KEY_LongPress  = 2
  67. }KEY_Status;

  68. /* Variable declarations -----------------------------------------------------*/
  69. /* Variable definitions ------------------------------------------------------*/
  70. /* Function declarations -----------------------------------------------------*/
  71. void KEY_Init(void);
  72. void KEY_DeInit(void);
  73. KEY_Status KEY_GetStatus(KEY_Pin pin);
  74. void KEY_SetShortPressCallback(KEY_Pin pin, void (*fun)(void));
  75. void KEY_SetLongPressCallback(KEY_Pin pin, void (*fun)(void));
  76. void KEY_ClearShortPressCallback(KEY_Pin pin);
  77. void KEY_ClearLongPressCallback(KEY_Pin pin);

  78. /* Function definitions ------------------------------------------------------*/

  79. #ifdef __cplusplus
  80. }
  81. #endif

  82. #endif /* __KEY_H */
复制代码

      KEY.c文件
  1. /**
  2.   ******************************************************************************
  3.   * @file    KEY.c
  4.   * @author  XinLi
  5.   * @version v1.0
  6.   * @date    24-October-2017
  7.   * @brief   KEY module driver.
  8.   ******************************************************************************
  9.   * @attention
  10.   *
  11.   * <h2><center>Copyright © 2017 XinLi</center></h2>
  12.   *
  13.   * This program is free software: you can redistribute it and/or modify
  14.   * it under the terms of the GNU General Public License as published by
  15.   * the Free Software Foundation, either version 3 of the License, or
  16.   * (at your option) any later version.
  17.   *
  18.   * This program is distributed in the hope that it will be useful,
  19.   * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20.   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21.   * GNU General Public License for more details.
  22.   *
  23.   * You should have received a copy of the GNU General Public License
  24.   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  25.   *
  26.   ******************************************************************************
  27.   */

  28. /* Header includes -----------------------------------------------------------*/
  29. #include "KEY.h"
  30. #include <string.h>

  31. /* Macro definitions ---------------------------------------------------------*/
  32. /* Type definitions ----------------------------------------------------------*/
  33. /* Variable declarations -----------------------------------------------------*/
  34. /* Variable definitions ------------------------------------------------------*/
  35. static      GPIO_TypeDef *KEY_GPIO[KEYn]                     = {KEY1_GPIO, KEY2_GPIO, KEY3_GPIO};
  36. static      uint16_t      KEY_GPIO_Pin[KEYn]                 = {KEY1_GPIO_Pin, KEY2_GPIO_Pin, KEY3_GPIO_Pin};
  37. static      uint32_t      KEY_RCC_AHB1Periph_GPIO[KEYn]      = {KEY1_RCC_AHB1Periph_GPIO, KEY2_RCC_AHB1Periph_GPIO, KEY3_RCC_AHB1Periph_GPIO};
  38. static __IO int           keyInputHighCount[KEYn]            = {0};
  39. static __IO int           keyInputLowCount[KEYn]             = {0};
  40. static __IO KEY_Status    keyStatus[KEYn]                    = {KEY_NoPress, KEY_NoPress, KEY_NoPress};
  41. static __IO KEY_Status    keyGetStatus[KEYn]                 = {KEY_NoPress, KEY_NoPress, KEY_NoPress};
  42. static __IO void        (*keyShortPressCallback[KEYn])(void) = {NULL};
  43. static __IO void        (*keyLongPressCallback[KEYn])(void)  = {NULL};

  44. /* Function declarations -----------------------------------------------------*/
  45. /* Function definitions ------------------------------------------------------*/

  46. /**
  47.   * @brief  Key initializes.
  48.   * @param  None.
  49.   * @return None.
  50.   */
  51. void KEY_Init(void)
  52. {
  53.   for(int i = 0; i < KEYn; i++)
  54.   {
  55.     GPIO_InitTypeDef GPIO_InitStructure = {0};
  56.    
  57.     RCC_AHB1PeriphClockCmd(KEY_RCC_AHB1Periph_GPIO[i], ENABLE);
  58.    
  59.     GPIO_InitStructure.GPIO_Pin   = KEY_GPIO_Pin[i];
  60.     GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_IN;
  61.     GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  62.     GPIO_Init(KEY_GPIO[i], &GPIO_InitStructure);
  63.    
  64.     keyInputHighCount[i]     = 0;
  65.     keyInputLowCount[i]      = 0;
  66.     keyStatus[i]             = KEY_NoPress;
  67.     keyGetStatus[i]          = KEY_NoPress;
  68.     keyShortPressCallback[i] = NULL;
  69.     keyLongPressCallback[i]  = NULL;
  70.   }
  71.   
  72.   TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure = {0};
  73.   
  74.   RCC_APB1PeriphClockCmd(KEY_RCC_APB1Periph_TIM, ENABLE);
  75.   
  76.   TIM_TimeBaseInitStructure.TIM_Prescaler     = KEY_TIM_Prescaler;
  77.   TIM_TimeBaseInitStructure.TIM_CounterMode   = TIM_CounterMode_Up;
  78.   TIM_TimeBaseInitStructure.TIM_Period        = KEY_TIM_Period;
  79.   TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
  80.   TIM_TimeBaseInit(KEY_TIM, &TIM_TimeBaseInitStructure);
  81.   
  82.   TIM_ClearFlag(KEY_TIM, TIM_FLAG_Update);
  83.   TIM_ITConfig(KEY_TIM, TIM_IT_Update, ENABLE);
  84.   TIM_Cmd(KEY_TIM, ENABLE);
  85.   
  86.   NVIC_InitTypeDef NVIC_InitStructure = {0};
  87.   
  88.   NVIC_InitStructure.NVIC_IRQChannel                   = KEY_TIM_IRQn;
  89.   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = KEY_TIM_IRQ_PreemptionPriority;
  90.   NVIC_InitStructure.NVIC_IRQChannelSubPriority        = KEY_TIM_IRQ_SubPriority;
  91.   NVIC_InitStructure.NVIC_IRQChannelCmd                = ENABLE;
  92.   NVIC_Init(&NVIC_InitStructure);
  93. }

  94. /**
  95.   * @brief  Key de-initializes.
  96.   * @param  None.
  97.   * @return None.
  98.   */
  99. void KEY_DeInit(void)
  100. {
  101.   for(int i = 0; i < KEYn; i++)
  102.   {
  103.     keyInputHighCount[i]     = 0;
  104.     keyInputLowCount[i]      = 0;
  105.     keyStatus[i]             = KEY_NoPress;
  106.     keyGetStatus[i]          = KEY_NoPress;
  107.     keyShortPressCallback[i] = NULL;
  108.     keyLongPressCallback[i]  = NULL;
  109.   }
  110.   
  111.   TIM_DeInit(KEY_TIM);
  112.   RCC_APB1PeriphClockCmd(KEY_RCC_APB1Periph_TIM, DISABLE);
  113.   
  114.   NVIC_InitTypeDef NVIC_InitStructure = {0};
  115.   
  116.   NVIC_InitStructure.NVIC_IRQChannel                   = KEY_TIM_IRQn;
  117.   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = KEY_TIM_IRQ_PreemptionPriority;
  118.   NVIC_InitStructure.NVIC_IRQChannelSubPriority        = KEY_TIM_IRQ_SubPriority;
  119.   NVIC_InitStructure.NVIC_IRQChannelCmd                = DISABLE;
  120.   NVIC_Init(&NVIC_InitStructure);
  121. }

  122. /**
  123.   * @brief  Get key status.
  124.   * @param  [in] pin: That key.
  125.   * @return Key status.
  126.   */
  127. KEY_Status KEY_GetStatus(KEY_Pin pin)
  128. {
  129.   KEY_Status status = KEY_NoPress;
  130.   
  131.   if(keyGetStatus[pin] != KEY_NoPress)
  132.   {
  133.     status            = keyGetStatus[pin];
  134.     keyGetStatus[pin] = KEY_NoPress;
  135.   }
  136.   
  137.   return status;
  138. }

  139. /**
  140.   * @brief  Set key short press callback.
  141.   * @param  [in] pin: That key.
  142.   * @param  [in] fun: Function pointer.
  143.   * @return None.
  144.   */
  145. void KEY_SetShortPressCallback(KEY_Pin pin, void (*fun)(void))
  146. {
  147.   keyShortPressCallback[pin] = (__IO void (*)(void))fun;
  148. }

  149. /**
  150.   * @brief  Set key long press callback.
  151.   * @param  [in] pin: That key.
  152.   * @param  [in] fun: Function pointer.
  153.   * @return None.
  154.   */
  155. void KEY_SetLongPressCallback(KEY_Pin pin, void (*fun)(void))
  156. {
  157.   keyLongPressCallback[pin] = (__IO void (*)(void))fun;
  158. }

  159. /**
  160.   * @brief  Clear key short press callback.
  161.   * @param  [in] pin: That key.
  162.   * @param  [in] fun: Function pointer.
  163.   * @return None.
  164.   */
  165. void KEY_ClearShortPressCallback(KEY_Pin pin)
  166. {
  167.   keyShortPressCallback[pin] = NULL;
  168. }

  169. /**
  170.   * @brief  Clear key long press callback.
  171.   * @param  [in] pin: That key.
  172.   * @param  [in] fun: Function pointer.
  173.   * @return None.
  174.   */
  175. void KEY_ClearLongPressCallback(KEY_Pin pin)
  176. {
  177.   keyLongPressCallback[pin] = NULL;
  178. }

  179. /**
  180.   * @brief  This function handles TIM Handler.
  181.   * @param  None.
  182.   * @return None.
  183.   */
  184. void KEY_TIM_IRQHandler(void)
  185. {
  186.   if(TIM_GetITStatus(KEY_TIM, TIM_IT_Update) != RESET)
  187.   {
  188.     TIM_ClearITPendingBit(KEY_TIM, TIM_IT_Update);
  189.    
  190.     for(int i = 0; i < KEYn; i++)
  191.     {
  192.       if(GPIO_ReadInputDataBit(KEY_GPIO[i], KEY_GPIO_Pin[i]) == KEY_PRESS_STATUS)
  193.       {
  194.         keyInputLowCount[i]++;
  195.         
  196.         if(keyInputLowCount[i] > 1)
  197.         {
  198.           keyInputHighCount[i] = 0;
  199.         }
  200.         
  201.         if(keyInputLowCount[i] > 100)
  202.         {
  203.           keyInputLowCount[i] = 0;
  204.           keyStatus[i]        = KEY_LongPress;
  205.           keyGetStatus[i]     = KEY_LongPress;
  206.          
  207.           if(keyLongPressCallback[i] != NULL)
  208.           {
  209.             keyLongPressCallback[i]();
  210.           }
  211.         }
  212.       }
  213.       else
  214.       {
  215.         keyInputHighCount[i]++;
  216.         
  217.         if(keyInputHighCount[i] > 1)
  218.         {
  219.           if((keyStatus[i] == KEY_NoPress) && (keyInputLowCount[i] > 1))
  220.           {
  221.             keyInputHighCount[i] = 0;
  222.             keyInputLowCount[i]  = 0;
  223.             keyStatus[i]         = KEY_ShortPress;
  224.             keyGetStatus[i]      = KEY_ShortPress;
  225.             
  226.             if(keyShortPressCallback[i] != NULL)
  227.             {
  228.               keyShortPressCallback[i]();
  229.             }
  230.           }
  231.           else
  232.           {
  233.             keyInputHighCount[i] = 0;
  234.             keyInputLowCount[i]  = 0;
  235.             keyStatus[i]         = KEY_NoPress;
  236.           }
  237.         }
  238.       }
  239.     }
  240.   }
  241. }
复制代码

      main.c文件
  1. /**
  2.   ******************************************************************************
  3.   * @file    main.c
  4.   * @author  XinLi
  5.   * @version v1.0
  6.   * @date    24-October-2017
  7.   * @brief   Main program body.
  8.   ******************************************************************************
  9.   * @attention
  10.   *
  11.   * <h2><center>Copyright © 2017 XinLi</center></h2>
  12.   *
  13.   * This program is free software: you can redistribute it and/or modify
  14.   * it under the terms of the GNU General Public License as published by
  15.   * the Free Software Foundation, either version 3 of the License, or
  16.   * (at your option) any later version.
  17.   *
  18.   * This program is distributed in the hope that it will be useful,
  19.   * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20.   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21.   * GNU General Public License for more details.
  22.   *
  23.   * You should have received a copy of the GNU General Public License
  24.   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  25.   *
  26.   ******************************************************************************
  27.   */

  28. /* Header includes -----------------------------------------------------------*/
  29. #include "main.h"
  30. #include "KEY.h"

  31. /* Macro definitions ---------------------------------------------------------*/
  32. /* Type definitions ----------------------------------------------------------*/
  33. /* Variable declarations -----------------------------------------------------*/
  34. /* Variable definitions ------------------------------------------------------*/
  35. static __IO int key1ShortPressCount = 0;
  36. static __IO int key1LongPressCount  = 0;
  37. static __IO int key2ShortPressCount = 0;
  38. static __IO int key2LongPressCount  = 0;

  39. /* Function declarations -----------------------------------------------------*/
  40. static void key1ShortPressCallback(void);
  41. static void key1LongPressCallback(void);

  42. /* Function definitions ------------------------------------------------------*/

  43. /**
  44.   * @brief  Main program.
  45.   * @param  None.
  46.   * @return None.
  47.   */
  48. int main(void)
  49. {
  50.   KEY_Init();
  51.   KEY_SetShortPressCallback(KEY_Pin1, key1ShortPressCallback);
  52.   KEY_SetLongPressCallback(KEY_Pin1, key1LongPressCallback);
  53.   
  54.   for(;;)
  55.   {
  56.     KEY_Status status = KEY_GetStatus(KEY_Pin2);
  57.    
  58.     if(status != KEY_NoPress)
  59.     {
  60.       if(status == KEY_ShortPress)
  61.       {
  62.         key2ShortPressCount++;
  63.       }
  64.       else
  65.       {
  66.         key2LongPressCount++;
  67.       }
  68.     }
  69.   }
  70. }

  71. /**
  72.   * @brief  Key 1 short press callback.
  73.   * @param  None.
  74.   * @return None.
  75.   */
  76. static void key1ShortPressCallback(void)
  77. {
  78.   key1ShortPressCount++;
  79. }

  80. /**
  81.   * @brief  Key 1 long press callback.
  82.   * @param  None.
  83.   * @return None.
  84.   */
  85. static void key1LongPressCallback(void)
  86. {
  87.   key1LongPressCount++;
  88. }
复制代码

3,注意

       修改接口需要注意,按键的检测电平、使用的TIM和增减按键的个数。对应修改KEY_PRESS_STATUS宏、TIM时钟配置函数和源文件的变量定义。

收藏 评论1 发布时间:2018-3-9 20:32

举报

1个回答
XinLiYF 回答时间:2018-3-10 11:02:07
刚刚通过审核,自己顶一下
关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32N6 AI生态系统
STM32MCU,MPU高性能GUI
ST ACEPACK电源模块
意法半导体生物传感器
STM32Cube扩展软件包
关注我们
st-img 微信公众号
st-img 手机版