https://blog.csdn.net/mygod2008ok/article/details/106890382
#include "BSP_pwm.h"
#include "stm32f0xx_hal_conf.h"
#include "stm_log.h"
#define BUZZER_BEEP (uint32_t)(244 - 1) /* Period Value */
#define PWM_PRESCALER_VALUE (FREQ_VALUE/1000000-1)
TIM_HandleTypeDef buzzer_TimHandle;
TIM_OC_InitTypeDef buzzer_sConfig;
static uint8_t s_beep_status = BEEP_STOP;
/**
* @brief 蜂鸣器PWM初时化
*/
static void BSP_buzzer_beep_init(void)
{
buzzer_TimHandle.Instance = TIM16;
__HAL_RCC_TIM16_CLK_ENABLE();
buzzer_TimHandle.Init.Prescaler = 48-1;
buzzer_TimHandle.Init.Period = BUZZER_BEEP;
buzzer_TimHandle.Init.ClockDivision = 0;
buzzer_TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP;
buzzer_TimHandle.Init.RepetitionCounter = 0;
buzzer_TimHandle.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
HAL_StatusTypeDef err_code = HAL_TIM_PWM_Init(&buzzer_TimHandle);
APP_ERROR_CHECK(err_code);
/*##-2- Configure the PWM channels #########################################*/
/* Common configuration for all channels */
buzzer_sConfig.OCMode = TIM_OCMODE_PWM1;
buzzer_sConfig.OCPolarity = TIM_OCPOLARITY_HIGH;
buzzer_sConfig.OCFastMode = TIM_OCFAST_DISABLE;
buzzer_sConfig.OCNPolarity = TIM_OCNPOLARITY_HIGH;
buzzer_sConfig.OCNIdleState = TIM_OCNIDLESTATE_RESET;
buzzer_sConfig.OCIdleState = TIM_OCIDLESTATE_RESET;
/* Set the pulse value for channel 1 */
buzzer_sConfig.Pulse = BUZZER_BEEP/2;
err_code = HAL_TIM_PWM_ConfigChannel(&buzzer_TimHandle, &buzzer_sConfig, TIM_CHANNEL_1);
APP_ERROR_CHECK(err_code);
}
/**
* @brief 启动蜂鸣器
*/
void start_buzzer_beep_sound(void)
{
if(s_beep_status == BEEP_STOP)
{
s_beep_status = BEEP_START;
}
}
/**
* @brief 启动buzzer pwm
*/
static void buzzer_pwm_start(void)
{
BSP_buzzer_beep_init();
GPIO_InitTypeDef GPIO_InitStruct;
/*##-1- Enable peripherals and GPIO Clocks #################################*/
/* TIMx Peripheral clock enable */
/* Enable GPIO Channels Clock */
__HAL_RCC_GPIOB_CLK_ENABLE();
/* Configure PB.04 (TIM3_Channel1), PB.05 (TIM3_Channel2), PB.00 (TIM3_Channel3),
PB.01 (TIM3_Channel4) in output, push-pull, alternate function mode
*/
/* Common configuration for all channels */
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF2_TIM16;
GPIO_InitStruct.Pin = GPIO_PIN_8;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
HAL_TIM_PWM_Start(&buzzer_TimHandle, TIM_CHANNEL_1);
}
/**
* @brief 停止buzzer pwm
*/
static void buzzer_pwm_stop(void)
{
HAL_TIM_PWM_Stop(&buzzer_TimHandle, TIM_CHANNEL_1);
HAL_TIM_PWM_DeInit(&buzzer_TimHandle);
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.Pin = GPIO_PIN_8;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
HAL_GPIO_WritePin(GPIOB,GPIO_PIN_8,GPIO_PIN_RESET);
}
/**
* @brief 蜂鸣器响应时长处理
* @note
* - 此函数在25HZ中调用
*/
void buzzer_beep_sound_handler(void)
{
static uint8_t cnt;
switch(s_beep_status)
{
case BEEP_START:
{
buzzer_pwm_start();
s_beep_status = BEEP_SOUNDING;
cnt = 0;
break;
}
case BEEP_SOUNDING:
if(++cnt >= 10)
{
s_beep_status = BEEP_STOP;
buzzer_pwm_stop();
}
break;
default:
break;
}
}
#define BUZZER_BEEP (uint32_t)(244 - 1)
buzzer_TimHandle.Init.Prescaler = 48-1;
buzzer_TimHandle.Init.ClockDivision = 0;
buzzer_TimHandle.Init.Period = BUZZER_BEEP;
buzzer_TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP;
buzzer_TimHandle.Init.RepetitionCounter = 0;
buzzer_TimHandle.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
#ifndef __BSP_PWM_H_
#define __BSP_PWM_H_
#include "main.h"
#ifdef __cplusplus
extern "C" {
#endif
typedef enum
{
BEEP_START = 1,
BEEP_SOUNDING,
BEEP_STOP,
}E_BUZZER_STATUS;
void start_buzzer_beep_sound(void);
void buzzer_beep_sound_handler(void);
#ifdef __cplusplus
}
#endif
#endif
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
RTT_INIT();
HAL_Init();
SystemClock_Config();
BSP_adc_init();
BSP_wdt_init(5000);
delay_init();
MX_RTC_Init();
BSP_start_adc_count(8);
start_buzzer_beep_sound();
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
if(s_wakeup_flag) //任务处理模式
{
BSP_wdt_feed();
tick_25hz_handler();
tick_1hz_handler();
}
else // 省电模式
{
}
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
/**
* @brief 25Hz handler
*
*/
static void tick_25hz_handler(void)
{
if((s_wakeup_flag & TICK_FOR_25HZ) == 0)
return;
s_wakeup_flag &= CLEAR_TICK_FOR_25HZ;
//####################################################################################
//---------TODO this to add 25hz event handler-----------
BSP_adc_convert_handler();
buzzer_beep_sound_handler();
}