01-C语言底层驱动:呼吸灯类驱动

工作中使用到呼吸灯效果,就写了个驱动,记录一下,方便以后使用。

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);

			}
		}
	}
}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值