可实现多个无源蜂鸣器常响、响的次数、响数的时长可控。
H文件
#ifndef __BSP_BEEP_H
#define __BSP_BEEP_H
#define BEEP_FRE 4000 //频率
#define BEEP_DUTY 20 //占空比 30 == 30%,精确到个位
#define BEEP_NUM 1 /*蜂鸣器的个数*/
typedef void (*pBeepPwm)(MCU_BIT bOpenOrClose,uint16_t u16Fre,uint8_t u8Duty);
typedef struct
{
uint8_t u8CtrStates; /*bit0: 1=运行, 0=停止
bit1: 1=长响, 0=由时间控制次数
bit2: 1=响, 0=不响,运行过程中的状态
*/
uint16_t u16BeepRingCnt; /*响的次数,响一次的时间等于u16BeepRingOnMs+u8BeepRingOffMs*/
uint16_t u16BeepRingOnMs; /*响的时间,单位ms,步进值是调用bsp_beepRunLoop()的间隔时间,这里是10ms*/
uint16_t u16BeepRingOffMs; /*不响时间*/
uint16_t u16MsCnt; /*时间计时,不用理会*/
pBeepPwm beepPwm; /*pwm的回调函数*/
}st_Beep_t;
void bsp_beepInit(void);
void app_link_bsp_beepRun_ToLoop(void);
void app_beep1Set(MCU_BIT bOpenOrClose, MCU_BIT bAllTimeOn, uint16_t u16BeepRingCnt, uint16_t u16BeepRingOnMs, uint16_t u16BeepRingOffMs);
#endif
c文件
/*
*********************************************************************************************************
*
* 模块名称 : 无源蜂鸣器驱动库
* 文件名称 : bsp_beep.c
* 版 本 : V1.0
* 说 明 : 可实现多个无源蜂鸣器常响、响的次数、响数的时长可控。常用
* 修改记录 :
* 版本号 日期 作者 说明
* V1.0 2019-08-29 徐
*********************************************************************************************************
*/
#include "bsp.h"
static st_Beep_t g_st_beep[BEEP_NUM]; /*定义蜂鸣器结构体*/
static st_bspGpio_t st_beepPowerGpio;
static uint16_t s_g_u16PwmArr = 0;
static uint16_t s_g_u16PwmFre = BEEP_FRE;
/*
*********************************************************************************************************
* 函 数 名: bsp_beepGpioInit
* 功能说明: 引脚初始化
* 形 参:无
* 返 回 值: 无
*********************************************************************************************************
*/
static void bsp_beepGpioInit(void)
{
stc_gpio_config_t stcTIMxPort;
DDL_ZERO_STRUCT(stcTIMxPort);
Sysctrl_SetPeripheralGate(SysctrlPeripheralGpio, TRUE);
stcTIMxPort.enDir = GpioDirOut;
stcTIMxPort.enPuPd = GpioPd;
///< 端口驱动能力配置->高驱动能力
//stcTIMxPort.enDrv = GpioDrvH;
st_beepPowerGpio.enPort = GpioPortB;
st_beepPowerGpio.enPin = GpioPin3;
st_beepPowerGpio.enAf = GpioAf0;
Gpio_Init(st_beepPowerGpio.enPort,st_beepPowerGpio.enPin, &stcTIMxPort);
Gpio_SetAfMode(st_beepPowerGpio.enPort, st_beepPowerGpio.enPin,st_beepPowerGpio.enAf);
Gpio_WriteOutputIO(st_beepPowerGpio.enPort, st_beepPowerGpio.enPin,1);
}
/*
*********************************************************************************************************
* 函 数 名: bsp_beep1Pwm
* 功能说明: 蜂鸣器1 PWM设置
* 形 参:bOpenOrClose:1=开PWM,0=关PWM。 u16Fre:PWM频率。 u8Duty:占空比
* 返 回 值: 无
*********************************************************************************************************
*/
static void bsp_beep1Pwm(MCU_BIT bOpenOrClose,uint16_t u16Fre,uint8_t u8Duty)
{
if(s_g_u16PwmFre != u16Fre)
{
s_g_u16PwmFre = u16Fre;
s_g_u16PwmArr = bsp_pcaPwmInit(s_g_u16PwmFre, 0);
}
if(!bOpenOrClose)
{
Gpio_WriteOutputIO(st_beepPowerGpio.enPort, st_beepPowerGpio.enPin,0); /*关闭蜂鸣器电源*/
bsp_pcaSetPwm(s_g_u16PwmArr, 0, Module0);
Pca_Stop();
}
else
{
Gpio_WriteOutputIO(st_beepPowerGpio.enPort, st_beepPowerGpio.enPin,1); /*开启蜂鸣器电源*/
Pca_Run();
bsp_pcaSetPwm(s_g_u16PwmArr, u8Duty, Module0);
}
}
/*
*********************************************************************************************************
* 函 数 名: bsp_beepInit
* 功能说明: 蜂鸣器初始化,初始化引脚和结构体。
* 形 参:无
* 返 回 值: 无
*********************************************************************************************************
*/
void bsp_beepInit(void)
{
bsp_beepGpioInit();
s_g_u16PwmArr = bsp_pcaPwmInit(s_g_u16PwmFre, 50);
g_st_beep[0].beepPwm = bsp_beep1Pwm;
}
/*
*********************************************************************************************************
* 函 数 名: app_link_bsp_beepSet
* 功能说明: 设置蜂鸣器状态。
* 形 参:*p_st_beep:要设置的蜂鸣器结构体。 bOpenOrClose:1=开PWM,0=关PWM。 bAllTimeOn:1=常响,0=不常响。
* u16BeepRingCnt:响的次数。 u16BeepRingOnMs:单次响的时长。 u16BeepRingOffMs:单次不响的时长
* 返 回 值: 无
*********************************************************************************************************
*/
static void app_link_bsp_beepSet(st_Beep_t *p_st_beep,MCU_BIT bOpenOrClose, MCU_BIT bAllTimeOn, uint16_t u16BeepRingCnt, uint16_t u16BeepRingOnMs, uint16_t u16BeepRingOffMs)
{
if(bOpenOrClose)
{ /*运行*/
SET_BIT(p_st_beep->u8CtrStates, 0); /*标志运行状态*/
if(bAllTimeOn)
{
SET_BIT(p_st_beep->u8CtrStates, 1); /*标志常响状态*/
}
else
{
p_st_beep->u16BeepRingCnt = u16BeepRingCnt;
p_st_beep->u16BeepRingOnMs = u16BeepRingOnMs;
p_st_beep->u16BeepRingOffMs = u16BeepRingOffMs;
p_st_beep->u16MsCnt = 0;
CLEAR_BIT(p_st_beep->u8CtrStates, 1);
SET_BIT(p_st_beep->u8CtrStates, 2); /*设置状态为正在响*/
}
}
else
{
CLEAR_BIT(p_st_beep->u8CtrStates, 0);
}
p_st_beep->beepPwm(bOpenOrClose,s_g_u16PwmFre,BEEP_DUTY);
}
/*
*********************************************************************************************************
* 函 数 名: app_link_bsp_beepRun_ToLoop
* 功能说明: 所有蜂鸣器的功能执行函数,放在定时器中循环调用,10ms中断
* 形 参:无
* 返 回 值: 无
*********************************************************************************************************
*/
void app_link_bsp_beepRun_ToLoop(void)
{
uint8_t i;
for(i=0; i<BEEP_NUM; i++)
{
if(IS_BIT(g_st_beep[i].u8CtrStates, 0)) /*蜂鸣器要运行*/
{
if(!IS_BIT(g_st_beep[i].u8CtrStates, 1)) /*蜂鸣器不是长响*/
{
if(g_st_beep[i].u16BeepRingCnt > 0) /*响的次数还没有结束*/
{
g_st_beep[i].u16MsCnt += 10;
if(IS_BIT(g_st_beep[i].u8CtrStates, 2)) /*正在响*/
{
if(g_st_beep[i].u16BeepRingOnMs <= g_st_beep[i].u16MsCnt)
{
CLEAR_BIT(g_st_beep[i].u8CtrStates, 2); /*设置下个状态为不响*/
g_st_beep[i].u16MsCnt = 0;
g_st_beep[i].beepPwm(0,s_g_u16PwmFre,BEEP_DUTY); //关闭声音
}
}
else //不响
{
if(g_st_beep[i].u16BeepRingOffMs <= g_st_beep[i].u16MsCnt)
{
SET_BIT(g_st_beep[i].u8CtrStates, 2); /*设置下个状态为响*/
--g_st_beep[i].u16BeepRingCnt; /*响的次数--*/
g_st_beep[i].u16MsCnt = 0;
if(g_st_beep[i].u16BeepRingCnt == 0)
{
g_st_beep[i].beepPwm(0,s_g_u16PwmFre,BEEP_DUTY); //关闭声音
}
else
{
g_st_beep[i].beepPwm(1,s_g_u16PwmFre,BEEP_DUTY); //开声音
}
}
}
}
}
}
}
}
/*
*********************************************************************************************************
* 函 数 名: app_beep1Set
* 功能说明: 设置蜂鸣器1状态
* 形 参:bOpenOrClose:1=开PWM,0=关PWM。 bAllTimeOn:1=常响,0=不常响。 u16BeepRingCnt:响的次数。
* u16BeepRingOnMs:单次响的时长。 u16BeepRingOffMs:单次不响的时长
* 返 回 值: 无
*********************************************************************************************************
*/
void app_beep1Set(MCU_BIT bOpenOrClose, MCU_BIT bAllTimeOn, uint16_t u16BeepRingCnt, uint16_t u16BeepRingOnMs, uint16_t u16BeepRingOffMs)
{
app_link_bsp_beepSet(&g_st_beep[0], bOpenOrClose, bAllTimeOn, u16BeepRingCnt, u16BeepRingOnMs, u16BeepRingOffMs);
}