37 lines
1.2 KiB
C
37 lines
1.2 KiB
C
/* Includes ----------------------------------------------------------------- */
|
|
#include "bsp\buzzer.h"
|
|
|
|
#include <main.h>
|
|
#include <tim.h>
|
|
|
|
/* Private define ----------------------------------------------------------- */
|
|
/* Private macro ------------------------------------------------------------ */
|
|
/* Private typedef ---------------------------------------------------------- */
|
|
/* Private variables -------------------------------------------------------- */
|
|
/* Private function -------------------------------------------------------- */
|
|
/* Exported functions ------------------------------------------------------- */
|
|
int8_t BSP_Buzzer_Start(void) {
|
|
if (HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_4) == HAL_OK) return BSP_OK;
|
|
return BSP_ERR;
|
|
}
|
|
|
|
/**
|
|
* @brief 设置蜂鸣器频率和占空比
|
|
* @param freq
|
|
* @param duty_cycle
|
|
* @return
|
|
*/
|
|
int8_t BSP_Buzzer_Set(float freq, float duty_cycle) {
|
|
uint16_t pulse = (uint16_t)(duty_cycle * (float)UINT16_MAX);
|
|
__HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_4, pulse);
|
|
|
|
pulse = (uint16_t)freq;
|
|
__HAL_TIM_PRESCALER(&htim3, pulse);
|
|
return BSP_OK;
|
|
}
|
|
|
|
int8_t BSP_Buzzer_Stop(void) {
|
|
if (HAL_TIM_PWM_Stop(&htim3, TIM_CHANNEL_4) == HAL_OK) return BSP_OK;
|
|
return BSP_ERR;
|
|
}
|