首先呢!好记性不如烂笔头,随着工作了块 1 年、逐渐发现了代码积累的重要性

那么从今天、记录整理自己写的一些驱动

Led.c

#include "led.h"
//***********************************************************//
//					 led 呼吸灯
// 通过 Led 这个结构体便可以快捷的改变呼吸灯的闪烁速率
//Led.Control.Set_Speed (10);// 例如这样
//                                                flechazo.mba
//***********************************************************//
Led_Struct Led;
/***********************************************************
*@fuction	:vledInit
*@brief		:led 呼吸灯的初始化
*@param		:void
*@return	:void
*@author	:flechazo (flechazo.mba)
*@date		:2023-05-21
***********************************************************/
void vledInit(void)
{
    /* RCU */
    rcu_periph_clock_enable(LED_RCU_GPIOx);
    /* GPIO */
    gpio_init(LED_GPIOx, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, LED_GPIO_PIN_x);
    /* TIMER */
    timer_oc_parameter_struct timer_ocintpara;
    timer_parameter_struct timer_initpara;
    rcu_periph_clock_enable(LED_RCU_TIMERx);
    timer_deinit(LED_TIMERx);
    /* LED_TIMERx configuration */
    timer_initpara.prescaler         = 0;
    timer_initpara.alignedmode       = TIMER_COUNTER_EDGE;
    timer_initpara.counterdirection  = TIMER_COUNTER_UP;
    timer_initpara.period            = LED_SPEED_COMPARENUMBER;
    timer_initpara.clockdivision     = TIMER_CKDIV_DIV1;
    timer_initpara.repetitioncounter = 0;
    timer_init(LED_TIMERx, &timer_initpara);
    timer_ocintpara.outputstate  = TIMER_CCX_ENABLE;
    timer_ocintpara.outputnstate = TIMER_CCXN_ENABLE;
    timer_ocintpara.ocpolarity   = TIMER_OC_POLARITY_HIGH;
    timer_ocintpara.ocnpolarity  = TIMER_OCN_POLARITY_HIGH;
    timer_ocintpara.ocidlestate  = TIMER_OC_IDLE_STATE_LOW;
    timer_ocintpara.ocnidlestate = TIMER_OCN_IDLE_STATE_LOW;
    timer_channel_output_config(LED_TIMERx, LED_TIMER_CH_x, &timer_ocintpara);
    timer_channel_output_pulse_value_config(LED_TIMERx, LED_TIMER_CH_x, Led.config.pwmdata);//pwmdata
    timer_channel_output_mode_config(LED_TIMERx, LED_TIMER_CH_x, TIMER_OC_MODE_PWM0);
    timer_channel_output_shadow_config(LED_TIMERx, LED_TIMER_CH_x, TIMER_OC_SHADOW_DISABLE);
    timer_primary_output_config(LED_TIMERx, ENABLE);
    nvic_irq_enable(TIMER2_IRQn, 2, 0);
    timer_interrupt_enable(LED_TIMERx, TIMER_INT_UP);
    timer_auto_reload_shadow_enable(LED_TIMERx);
    timer_enable(LED_TIMERx);
    /* Init */
    Led.config.ledSpeed = LED_SPEED_USER;
    Led.Control.Set_Speed = vledSetSpeed;
}
/***********************************************************
*@fuction	:vledToggle
*@brief		:led 的状态反转
*@param		:bool 类型的状态 status
*@return	:void
*@author	:flechazo (flechazo.mba)
*@date		:2023-05-21
***********************************************************/
void vledToggle(bool status)
{
    status ? gpio_bit_reset(LED_GPIOx, LED_GPIO_PIN_x) : gpio_bit_set(LED_GPIOx, LED_GPIO_PIN_x);
}
/***********************************************************
*@fuction	:vledSetSpeed
*@brief		: 通过这个函数即可方便的改变呼吸灯的速度
*@param		:speed 速度
*@return	:void
*@author	:flechazo (flechazo.mba)
*@date		:2023-05-21
***********************************************************/
void vledSetSpeed(uint32_t speed)
{
    Led.config.ledSpeed = speed;
}
/***********************************************************
*@fuction	:vledWork
*@brief		:Led 的比较计算
*@param		:void
*@return	:void
*@author	:flechazo (flechazo.mba)
*@date		:2023-05-21
***********************************************************/
void vledWork(void)
{
    if(!Led.pwmflag)
    {
        if(Led.config.pwmdata >= LED_SPEED_COMPARENUMBER)
        {
            if((++Led.config.SpeedCount) >= (2000 * Led.config.ledSpeed))
            {
                Led.config.SpeedCount = 0;
                Led.pwmflag = true;
            }
        }
        else
        {
            if((++Led.config.SpeedCount) >= (Led.config.ledSpeed))
            {
                Led.config.pwmdata++;
                Led.config.SpeedCount = 0;
            }
        }
    }
    else
    {
        if(Led.config.pwmdata <= 0)
        {
            if((++Led.config.SpeedCount) >= (1000 * Led.config.ledSpeed))
            {
                Led.config.SpeedCount = 0;
                Led.pwmflag = false;
            }
        }
        else
        {
            if((++Led.config.SpeedCount) >= (Led.config.ledSpeed))
            {
                Led.config.pwmdata--;
                Led.config.SpeedCount = 0;
            }
        }
    }
}
void TIMER2_IRQHandler(void)
{
    timer_flag_clear(LED_TIMERx, TIMER_FLAG_UP); 
    vledWork();
    timer_channel_output_pulse_value_config(LED_TIMERx, LED_TIMER_CH_x, Led.config.pwmdata);
}

Led.h

#ifndef __LED_H
#define __LED_H
#include "gd32f30x.h"
#include <stdbool.h>
#define LED_RCU_GPIOx RCU_GPIOB
#define LED_GPIOx GPIOB
#define LED_GPIO_PIN_x GPIO_PIN_0
#define LED_RCU_TIMERx RCU_TIMER2
#define LED_TIMERx TIMER2
#define LED_TIMER_CH_x TIMER_CH_2
#define LED_SPEED_COMPARENUMBER 0x7FF
enum{
	LED_SPEED_USER = 4,
	LED_SPEED_TEST = 2,
	LED_SPEED_ADMIN = 0,
};
typedef struct
{
	uint32_t pwmdata;
	uint32_t ledTime;
	uint32_t ledSpeed;
	uint32_t SpeedCount;
} ledConfig_struct;
typedef struct
{
    void (*Set_Speed)(uint32_t Speed);
} ledControl_struct;
typedef struct{
	bool pwmflag;
	ledConfig_struct config;
	ledControl_struct Control;
}Led_Struct;
void vledInit(void);
void vledToggle(bool status);
void vledSetSpeed(uint32_t speed);
#endif   //__LED_H
更新于 阅读次数

请我喝[茶]~( ̄▽ ̄)~*

flechazo 微信支付

微信支付

flechazo 支付宝

支付宝

flechazo 贝宝

贝宝