工作中使用到呼吸灯效果,就写了个驱动,记录一下,方便以后使用。
bsp_breathingLamp.H:
#ifndef __BSP_BREATHINGLAMP_H
#define __BSP_BREATHINGLAMP_H
typedef void (*pwm_f) (uint8_t u8Duty);
typedef uint16_t (*lampInit) (uint16_t u16Freq, uint8_t u8Duty);
typedef struct
{
uint8_t u8LampCtr; /*bit0:1=启动呼吸灯效果,0=关闭启动呼吸灯效果
bit1:关闭效果后,led灯状态,1=亮,0=灭
bit2:此时占空比状态,1=增加,0=减小
bit3:led灯的电路连接方式,1=高电平亮,0=低电平亮
bit4:停止呼吸灯效果后,是否已经执行设置led状态的程序。1=执行了
bit4是一个为了增加程序执行效率而设置的位。
*/
int8_t i8Duty;
uint16_t u16Freq; //频率
uint16_t u16Arr;
pwm_f breathingLampSetPwm;
lampInit breathingLampInit;
}st_breathingLamp_t;
#define BREATHING_LAMP_NUM 2 //呼吸灯的个数
extern st_breathingLamp_t st_breathingLamp[BREATHING_LAMP_NUM];
void bsp_breathingLampInit(void);
void bsp_breathingLampLoop(st_breathingLamp_t *st_p_breathingLamp);
void bsp_breathingLampStop(st_breathingLamp_t *st_p_breathingLamp, MCU_BIT bLedOnOrOff);
void bsp_breathingLampStart(st_breathingLamp_t *st_p_breathingLamp);
#endif
bsp_breathingLamp.c:
/*
*********************************************************************************************************
*
* 模块名称 : bsp库
* 文件名称 : bsp.c
* 版 本 : V1.0
* 说 明 : 呼吸灯
*
* 修改记录 :
* 版本号 日期 作者 说明
* V1.0 2019-07-30 徐
*********************************************************************************************************
*/
#include "bsp.h"
st_breathingLamp_t st_breathingLamp[BREATHING_LAMP_NUM];
/*
*********************************************************************************************************
* 函 数 名: app_link_bsp_breathingLampPwmInit
* 功能说明: 呼吸灯PWM初始化函数
* 形 参:
* 返 回 值: arr重载值
*********************************************************************************************************
*/
static uint16_t app_link_bsp_breathingLampPwmInit(uint16_t u16Freq, uint8_t u8Duty)
{
//填写PWM初始化函数
}
/*
*********************************************************************************************************
* 函 数 名: app_link_bsp_breathingLampPwmSet
* 功能说明: 呼吸灯pwm设置函数
* 形 参:无
* 返 回 值: 无
*********************************************************************************************************
*/
static void app_link_bsp_breathingLampPwmSet(uint8_t u8Duty)
{
//填写设置PWM占空比函数
}
void bsp_breathingLampInit(void)
{
st_breathingLamp_t *st_p_breathingLamp;
// stc_gpio_config_t stcTIMxPort;
// ///< 打开GPIO外设时钟门控
// Sysctrl_SetPeripheralGate(SysctrlPeripheralGpio, TRUE);
// DDL_ZERO_STRUCT(stcTIMxPort);
// stcTIMxPort.enDir = GpioDirIn;
// stcTIMxPort.enPuPd = GpioPd;
// ///< 端口开漏输出配置->开漏输出关闭
// stcTIMxPort.enOD = GpioOdDisable;
// ///< 端口输入/输出值寄存器总线控制模式配置->AHB
// stcTIMxPort.enCtrlMode = GpioAHB;
// Gpio_Init(GpioPortD,GpioPin5, &stcTIMxPort);
// // Gpio_SetAfMode(GpioPortD, GpioPin5,st_lightSterilizationGpio.enAf);
st_p_breathingLamp = &st_breathingLamp[0];
st_p_breathingLamp->u8LampCtr = 0;
SET_BIT(st_p_breathingLamp->u8LampCtr,3);
st_p_breathingLamp->i8Duty = 0;
st_p_breathingLamp->u16Freq = 10000;
st_p_breathingLamp->breathingLampSetPwm = app_link_bsp_breathingLampPwmSet;
st_p_breathingLamp->breathingLampInit = app_link_bsp_breathingLampPwmInit;
st_p_breathingLamp->u16Arr = st_p_breathingLamp->breathingLampInit(st_p_breathingLamp->u16Freq, st_p_breathingLamp->i8Duty);
bsp_breathingLampStop(st_p_breathingLamp,0);
}
/*
*********************************************************************************************************
* 函 数 名: bsp_breathingLampStart
* 功能说明: 启动一个呼吸灯效果
* 形 参:*st_p_breathingLamp:某个呼吸灯的结构体地址
* 返 回 值: 无
*********************************************************************************************************
*/
void bsp_breathingLampStart(st_breathingLamp_t *st_p_breathingLamp)
{
SET_BIT(st_p_breathingLamp->u8LampCtr,0);
}
/*
*********************************************************************************************************
* 函 数 名: bsp_breathingLampStop
* 功能说明: 停止一个呼吸灯效果
* 形 参:*st_p_breathingLamp:某个呼吸灯的结构体地址。
* bLedOnOrOff:停止后led的状态,1=亮,0灭
* 返 回 值: 无
*********************************************************************************************************
*/
void bsp_breathingLampStop(st_breathingLamp_t *st_p_breathingLamp, MCU_BIT bLedOnOrOff)
{
if(!bLedOnOrOff)
CLEAR_BIT(st_p_breathingLamp->u8LampCtr,1);
else
SET_BIT(st_p_breathingLamp->u8LampCtr,1);
CLEAR_BIT(st_p_breathingLamp->u8LampCtr,0);
CLEAR_BIT(st_p_breathingLamp->u8LampCtr,4);
}
/*
*********************************************************************************************************
* 函 数 名: bsp_breathingLampLoop
* 功能说明: 定时调用,调用时间不同,可产生不同周期效果的呼吸灯。占空比从0到100,在从100到0,
* 共200个定时时间为一个周期。比如要产生周期为1秒的呼吸灯,定时调用时间=1000/200=5ms。
* 注意:呼吸灯占空比的周期要小于定时调用的时间。
* 形 参:*st_p_breathingLamp:某个呼吸灯的结构体地址
* 返 回 值: 无
*********************************************************************************************************
*/
void bsp_breathingLampLoop(st_breathingLamp_t *st_p_breathingLamp)
{
if(IS_BIT(st_p_breathingLamp->u8LampCtr,0)) //呼吸灯效果开启
{
if(IS_BIT(st_p_breathingLamp->u8LampCtr,2))
{
if(++st_p_breathingLamp->i8Duty >= 100)
{
CLEAR_BIT(st_p_breathingLamp->u8LampCtr,2);
}
}
else
{
if(--st_p_breathingLamp->i8Duty <= 0)
{
SET_BIT(st_p_breathingLamp->u8LampCtr,2);
}
}
st_p_breathingLamp->breathingLampSetPwm(st_p_breathingLamp->i8Duty);
}
else //呼吸灯效果关闭
{
if(!IS_BIT(st_p_breathingLamp->u8LampCtr,4)) //增加一个位判断,避免一直执行下面的程序,做无用功
{
SET_BIT(st_p_breathingLamp->u8LampCtr,4);
if(IS_BIT(st_p_breathingLamp->u8LampCtr,1)) //亮
{
if(IS_BIT(st_p_breathingLamp->u8LampCtr,3)) //判断电路连接方式
st_p_breathingLamp->breathingLampSetPwm(100);
else
st_p_breathingLamp->breathingLampSetPwm(0);
}
else //灭
{
if(IS_BIT(st_p_breathingLamp->u8LampCtr,3)) //判断电路连接方式,高电平亮灯
st_p_breathingLamp->breathingLampSetPwm(0);
else
st_p_breathingLamp->breathingLampSetPwm(100);
}
}
}
}