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

STM32F4(BUZZ)

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

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


1,开发环境

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

2,程序源码
      BUZZ.h文件
  1. /**
  2.   ******************************************************************************
  3.   * @file    BUZZ.h
  4.   * @author  XinLi
  5.   * @version v1.0
  6.   * @date    24-October-2017
  7.   * @brief   Header file for BUZZ.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 __BUZZ_H
  29. #define __BUZZ_H

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

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

  35. /* Macro definitions ---------------------------------------------------------*/
  36. #define BUZZ_RCC_AHB1Periph_GPIO         RCC_AHB1Periph_GPIOB
  37. #define BUZZ_GPIO                        GPIOB
  38. #define BUZZ_GPIO_Pin                    GPIO_Pin_8

  39. #define BUZZ_GPIO_PinSource              GPIO_PinSource8
  40. #define BUZZ_GPIO_AF_TIM                 GPIO_AF_TIM4

  41. #define BUZZ_RCC_APB1Periph_TIM          RCC_APB1Periph_TIM4
  42. #define BUZZ_TIM                         TIM4

  43. #define BUZZ_TIM_Prescaler               (83)  /*!< Clock divided to 1MHz. */
  44. #define BUZZ_TIM_Period                  (249) /*!< 250us timer interrupt. */

  45. #define BUZZ_TIM_OCPolarity              TIM_OCPolarity_High

  46. #define BUZZ_TIM_OCInit                  TIM_OC3Init
  47. #define BUZZ_TIM_OCPreloadConfig         TIM_OC3PreloadConfig

  48. #define BUZZ_TIM_SetCompare              TIM_SetCompare3

  49. #define BUZZ_TIM_IRQn                    TIM4_IRQn
  50. #define BUZZ_TIM_IRQHandler              TIM4_IRQHandler
  51. #define BUZZ_TIM_IRQ_PreemptionPriority  (0)
  52. #define BUZZ_TIM_IRQ_SubPriority         (0)

  53. /* Type definitions ----------------------------------------------------------*/
  54. typedef enum
  55. {
  56.   BUZZ_Off     = 0,
  57.   BUZZ_Ring    = 1,
  58.   BUZZ_Drip    = 2,
  59.   BUZZ_Warning = 3,
  60.   BUZZ_Danger  = 4
  61. }BUZZ_Status;

  62. /* Variable declarations -----------------------------------------------------*/
  63. /* Variable definitions ------------------------------------------------------*/
  64. /* Function declarations -----------------------------------------------------*/
  65. void BUZZ_SetStatus(BUZZ_Status status);
  66. BUZZ_Status BUZZ_GetStatus(void);

  67. /* Function definitions ------------------------------------------------------*/

  68. #ifdef __cplusplus
  69. }
  70. #endif

  71. #endif /* __BUZZ_H */
复制代码

      BUZZ.c文件
  1. /**
  2.   ******************************************************************************
  3.   * @file    BUZZ.c
  4.   * @author  XinLi
  5.   * @version v1.0
  6.   * @date    24-October-2017
  7.   * @brief   BUZZ 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 "BUZZ.h"
  30. #include <stdbool.h>

  31. /* Macro definitions ---------------------------------------------------------*/
  32. /* Type definitions ----------------------------------------------------------*/
  33. /* Variable declarations -----------------------------------------------------*/
  34. /* Variable definitions ------------------------------------------------------*/
  35. static __IO BUZZ_Status buzzStatus = BUZZ_Off;
  36. static __IO int         buzzCount  = 0;

  37. /* Function declarations -----------------------------------------------------*/
  38. static void BUZZ_PwmInit(void);
  39. static void BUZZ_NvicInit(void);
  40. static void BUZZ_Init(void);

  41. /* Function definitions ------------------------------------------------------*/

  42. /**
  43.   * @brief  Buzz pwm initializes.
  44.   * @param  None.
  45.   * @return None.
  46.   */
  47. static void BUZZ_PwmInit(void)
  48. {
  49.   GPIO_InitTypeDef        GPIO_InitStructure    = {0};
  50.   TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure = {0};
  51.   TIM_OCInitTypeDef       TIM_OCInitStructure   = {0};
  52.   
  53.   RCC_AHB1PeriphClockCmd(BUZZ_RCC_AHB1Periph_GPIO, ENABLE);
  54.   RCC_APB1PeriphClockCmd(BUZZ_RCC_APB1Periph_TIM, ENABLE);
  55.   
  56.   GPIO_InitStructure.GPIO_Pin   = BUZZ_GPIO_Pin;
  57.   GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF;
  58.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  59.   GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  60.   GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_UP;
  61.   GPIO_Init(BUZZ_GPIO, &GPIO_InitStructure);
  62.   
  63.   GPIO_PinAFConfig(BUZZ_GPIO, BUZZ_GPIO_PinSource, BUZZ_GPIO_AF_TIM);
  64.   
  65.   TIM_TimeBaseStructure.TIM_Prescaler     = BUZZ_TIM_Prescaler;
  66.   TIM_TimeBaseStructure.TIM_CounterMode   = TIM_CounterMode_Up;
  67.   TIM_TimeBaseStructure.TIM_Period        = BUZZ_TIM_Period;
  68.   TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
  69.   TIM_TimeBaseInit(BUZZ_TIM, &TIM_TimeBaseStructure);
  70.   
  71.   TIM_OCInitStructure.TIM_OCMode      = TIM_OCMode_PWM1;
  72.   TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  73.   TIM_OCInitStructure.TIM_Pulse       = 0;
  74.   TIM_OCInitStructure.TIM_OCPolarity  = BUZZ_TIM_OCPolarity;
  75.   BUZZ_TIM_OCInit(BUZZ_TIM, &TIM_OCInitStructure);
  76.   
  77.   BUZZ_TIM_OCPreloadConfig(BUZZ_TIM, TIM_OCPreload_Enable);
  78.   TIM_ARRPreloadConfig(BUZZ_TIM, ENABLE);
  79.   TIM_ITConfig(BUZZ_TIM, TIM_IT_Update, ENABLE);
  80.   TIM_Cmd(BUZZ_TIM, ENABLE);
  81. }

  82. /**
  83.   * @brief  Buzz nvic initializes.
  84.   * @param  None.
  85.   * @return None.
  86.   */
  87. static void BUZZ_NvicInit(void)
  88. {
  89.   NVIC_InitTypeDef NVIC_InitStructure = {0};
  90.   
  91.   NVIC_InitStructure.NVIC_IRQChannel                   = BUZZ_TIM_IRQn;
  92.   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = BUZZ_TIM_IRQ_PreemptionPriority;
  93.   NVIC_InitStructure.NVIC_IRQChannelSubPriority        = BUZZ_TIM_IRQ_SubPriority;
  94.   NVIC_InitStructure.NVIC_IRQChannelCmd                = ENABLE;
  95.   NVIC_Init(&NVIC_InitStructure);
  96. }

  97. /**
  98.   * @brief  Buzz initializes.
  99.   * @param  None.
  100.   * @return None.
  101.   */
  102. static void BUZZ_Init(void)
  103. {
  104.   static bool init_flag = false;
  105.   
  106.   if(init_flag == false)
  107.   {
  108.     init_flag = true;
  109.    
  110.     BUZZ_PwmInit();
  111.     BUZZ_NvicInit();
  112.   }
  113. }

  114. /**
  115.   * @brief  Set buzz status.
  116.   * @param  [in] status: Buzz status.
  117.   * @return None.
  118.   */
  119. void BUZZ_SetStatus(BUZZ_Status status)
  120. {
  121.   if(status != buzzStatus)
  122.   {
  123.     buzzStatus = status;
  124.     buzzCount  = 0;
  125.     BUZZ_Init();
  126.   }
  127. }

  128. /**
  129.   * @brief  Get buzz status.
  130.   * @param  None.
  131.   * @return Buzz status.
  132.   */
  133. BUZZ_Status BUZZ_GetStatus(void)
  134. {
  135.   return buzzStatus;
  136. }

  137. /**
  138.   * @brief  This function handles TIM Handler.
  139.   * @param  None.
  140.   * @return None.
  141.   */
  142. void BUZZ_TIM_IRQHandler(void)
  143. {
  144.   if(TIM_GetITStatus(BUZZ_TIM, TIM_IT_Update) != RESET)
  145.   {
  146.     TIM_ClearITPendingBit(BUZZ_TIM, TIM_IT_Update);
  147.    
  148.     if(buzzStatus == BUZZ_Off)
  149.     {
  150.       buzzCount = 0;
  151.       BUZZ_TIM_SetCompare(BUZZ_TIM, 0);
  152.     }
  153.     else if(buzzStatus == BUZZ_Ring)
  154.     {
  155.       buzzCount = 0;
  156.       BUZZ_TIM_SetCompare(BUZZ_TIM, (BUZZ_TIM_Period + 1) / 2);
  157.     }
  158.     else if(buzzStatus == BUZZ_Drip)
  159.     {
  160.       if(buzzCount > 1999)
  161.       {
  162.         if(buzzCount > 3999)
  163.         {
  164.           buzzCount  = 0;
  165.           buzzStatus = BUZZ_Off;
  166.           BUZZ_TIM_SetCompare(BUZZ_TIM, 0);
  167.         }
  168.         else
  169.         {
  170.           buzzCount++;
  171.           BUZZ_TIM_SetCompare(BUZZ_TIM, 0);
  172.         }
  173.       }
  174.       else
  175.       {
  176.         buzzCount++;
  177.         BUZZ_TIM_SetCompare(BUZZ_TIM, (BUZZ_TIM_Period + 1) / 2);
  178.       }
  179.     }
  180.     else if(buzzStatus == BUZZ_Warning)
  181.     {
  182.       if(buzzCount > 1999)
  183.       {
  184.         if(buzzCount > 3999)
  185.         {
  186.           buzzCount = 0;
  187.           BUZZ_TIM_SetCompare(BUZZ_TIM, (BUZZ_TIM_Period + 1) / 2);
  188.         }
  189.         else
  190.         {
  191.           buzzCount++;
  192.           BUZZ_TIM_SetCompare(BUZZ_TIM, 0);
  193.         }
  194.       }
  195.       else
  196.       {
  197.         buzzCount++;
  198.         BUZZ_TIM_SetCompare(BUZZ_TIM, (BUZZ_TIM_Period + 1) / 2);
  199.       }
  200.     }
  201.     else
  202.     {
  203.       if(buzzCount > 999)
  204.       {
  205.         if(buzzCount > 1999)
  206.         {
  207.           buzzCount = 0;
  208.           BUZZ_TIM_SetCompare(BUZZ_TIM, (BUZZ_TIM_Period + 1) / 2);
  209.         }
  210.         else
  211.         {
  212.           buzzCount++;
  213.           BUZZ_TIM_SetCompare(BUZZ_TIM, 0);
  214.         }
  215.       }
  216.       else
  217.       {
  218.         buzzCount++;
  219.         BUZZ_TIM_SetCompare(BUZZ_TIM, (BUZZ_TIM_Period + 1) / 2);
  220.       }
  221.     }
  222.   }
  223. }
复制代码

      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 "Delay.h"
  31. #include "BUZZ.h"

  32. /* Macro definitions ---------------------------------------------------------*/
  33. /* Type definitions ----------------------------------------------------------*/
  34. /* Variable declarations -----------------------------------------------------*/
  35. /* Variable definitions ------------------------------------------------------*/
  36. /* Function declarations -----------------------------------------------------*/
  37. /* Function definitions ------------------------------------------------------*/

  38. /**
  39.   * @brief  Main program.
  40.   * @param  None.
  41.   * @return None.
  42.   */
  43. int main(void)
  44. {
  45.   for(;;)
  46.   {
  47.     Delay_s(5);
  48.     BUZZ_SetStatus(BUZZ_Drip);
  49.     Delay_s(5);
  50.     BUZZ_SetStatus(BUZZ_Ring);
  51.     Delay_s(5);
  52.     BUZZ_SetStatus(BUZZ_Warning);
  53.     Delay_s(5);
  54.     BUZZ_SetStatus(BUZZ_Danger);
  55.     Delay_s(5);
  56.     BUZZ_SetStatus(BUZZ_Off);
  57.   }
  58. }
复制代码

3,注意

       修改接口需要注意,蜂鸣器的驱动方式和使用的TIM。对应修改PWM输出电平和TIM时钟配置函数。

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

举报

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