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

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

Buzzer.c

#include "stdio.h"
#include "stdlib.h"
#include "systick.h"
#include "../User/BSP/BUZZER/buzzer.h"
#include "../User/BSP/TIMER1/timer.h"
//***********************************************************//
//						buzzer 蜂鸣器
// 通过 Buzzer 这个结构体便可以快捷的控制蜂鸣器
//1、将 Buzzer.control.Ring () 这个函数放在 while 循环中
//2、在任何地方 Buzzer.control.SetRing (), 这里两个参数、次数、速度
// 本蜂鸣器驱动支持 BuzzerRingGroup 组次数和速度、采用插入的方式,可同时
// 配置多组不同的次数和速度
//                                               flechazo.mba
//***********************************************************//
Buzzer_Struct Buzzer;
uint32_t BuzzerInsertRing[2][BuzzerRingGroup] = {0};
/***********************************************************
*@fuction	:vbuzzerSetRing
*@brief		: 可以设置蜂鸣器响的次数和速度、采用插入的方式、最多
					 可以插入 BuzzerRingGroup 组响声音
*@param		:count speed
*@return	:void
*@author	:flechazo 更多例程请访问 (flechazo.mba)
*@date		:2023-05-21
***********************************************************/
void vbuzzerSetRing(uint32_t count, uint32_t speed)
{
    vbuzzerCopyArray_32(Buzzer.config.Info.buzzerTime, &BuzzerInsertRing[0][1], BuzzerRingGroup - 1);
    vbuzzerCopyArray_32(Buzzer.config.Info.buzzerSpeed, &BuzzerInsertRing[1][1], BuzzerRingGroup - 1);
    BuzzerInsertRing[0][0] = (count + 1) * 2;
    BuzzerInsertRing[1][0] = speed;
    vbuzzerCopyArray_32(BuzzerInsertRing[0], Buzzer.config.Info.buzzerTime, BuzzerRingGroup);
    vbuzzerCopyArray_32(BuzzerInsertRing[1], Buzzer.config.Info.buzzerSpeed, BuzzerRingGroup);
    Buzzer.config.Info.group++;
}
bool buzzerRingFlag = true;
/***********************************************************
*@fuction	:vbuzzerRing
*@brief		: 蜂鸣器响的主体、需要放在 while 循环中
*@param		:--
*@return	:void
*@author	:flechazo 更多例程请访问 (flechazo.mba)
*@date		:2023-05-21
***********************************************************/
void vbuzzerRing(void)
{
    if((Buzzer.control.enable) && (Buzzer.config.Info.group > 0))
    {
        if(Buzzer.config.Info.buzzerTime[Buzzer.config.Info.group - 1] > 1)
        {
            if(vtimerIsTimeOut_ms(&Buzzer.config.Info.buzzerRingTime, Buzzer.config.Info.buzzerSpeed[Buzzer.config.Info.group - 1]))
            {
                //Toggle
                buzzerRingFlag ? gpio_bit_set(BUZZER_GPIOx, BUZZER_GPIO_PINx) : gpio_bit_reset(BUZZER_GPIOx, BUZZER_GPIO_PINx);
                buzzerRingFlag = !buzzerRingFlag;
                //Time
                Buzzer.config.Info.buzzerTime[Buzzer.config.Info.group - 1]--;
                Buzzer.config.Info.buzzerRingTime = vtimerGetCurrentTime_ms();
            }
            else
            {
            }
        }
        else if(Buzzer.config.Info.buzzerTime[Buzzer.config.Info.group - 1] == 1)
        {
            gpio_bit_reset(BUZZER_GPIOx, BUZZER_GPIO_PINx);
            Buzzer.config.Info.buzzerTime[Buzzer.config.Info.group - 1]--;
        }
        else
        {
            Buzzer.config.Info.group--;
        }
    }
}
/***********************************************************
*@fuction	:vbuzzerInit
*@brief		:
*@param		:--
*@return	:void
*@author	:flechazo 更多例程请访问 (flechazo.mba)
*@date		:2023-05-21
***********************************************************/
void vbuzzerInit(void)
{
    rcu_periph_clock_enable(RCU_GPIOB);
    gpio_init(BUZZER_GPIOx, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, BUZZER_GPIO_PINx);
    gpio_bit_reset(BUZZER_GPIOx, BUZZER_GPIO_PINx);
    Buzzer.control.enable = true;
    Buzzer.control.Ring = vbuzzerRing;
    Buzzer.control.SetRing = vbuzzerSetRing;
    //		rcu_periph_clock_enable(RCU_GPIOB);
    //    gpio_init(BUZZER_GPIOx, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, BUZZER_GPIO_PINx);
    //
    //    timer_oc_parameter_struct timer_ocintpara;
    //    timer_parameter_struct timer_initpara;
    //    rcu_periph_clock_enable(RCU_TIMER9);
    //    timer_deinit(TIMER9);
    //    /* LED_TIMERx configuration */
    //    timer_initpara.prescaler         = 0;
    //    timer_initpara.alignedmode       = TIMER_COUNTER_EDGE;
    //    timer_initpara.counterdirection  = TIMER_COUNTER_UP;
    //    timer_initpara.period            = 0x7FF;
    //    timer_initpara.clockdivision     = TIMER_CKDIV_DIV1;
    //    timer_initpara.repetitioncounter = 0;
    //    timer_init(TIMER9, &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(TIMER9, TIMER_CH_0, &timer_ocintpara);
    //    timer_channel_output_pulse_value_config(TIMER9, TIMER_CH_0, 0);//pwmdata
    //    timer_channel_output_mode_config(TIMER9, TIMER_CH_0, TIMER_OC_MODE_PWM0);
    //    timer_channel_output_shadow_config(TIMER9, TIMER_CH_0, TIMER_OC_SHADOW_DISABLE);
    //    timer_primary_output_config(TIMER9, ENABLE);
    //		nvic_irq_enable(TIMER2_IRQn, 1, 0);
    //		timer_interrupt_enable(TIMER9,TIMER_INT_UP);
    //    timer_auto_reload_shadow_enable(TIMER9);
    //    timer_enable(TIMER9);
    //
}
void vbuzzerCopyArray_32(uint32_t *sourcearray, uint32_t *targetarray, uint32_t length)
{
    for(uint32_t i = 0; i < length; i++)
    {
        targetarray[i] = sourcearray[i];
    }
}

Buzzer.h

#ifndef __BUZZER_H
#define __BUZZER_H
#include "gd32f30x.h"
#include <stdbool.h>
#define BUZZER_RCU_GPIOx     RCU_GPIOB
#define BUZZER_GPIOx         GPIOB
#define BUZZER_GPIO_PINx     GPIO_PIN_8
#define BuzzerRingGroup 10
typedef union
{
    struct
    {
				uint32_t pwmdata;
				uint32_t group;
				uint32_t buzzerTime[BuzzerRingGroup];
				uint32_t buzzerSpeed[BuzzerRingGroup];
				uint32_t buzzerMode;
				uint32_t buzzerRingTime;
    } Info;
    uint32_t Byte[(BuzzerRingGroup*2)+4];
} Buzzer_union;
typedef struct
{
	bool enable;
	void (*Ring)(void);
	void (*SetRing)(uint32_t time,uint32_t speed);
}Buzzer_Control;
typedef struct
{
	Buzzer_union config;
	Buzzer_Control control;
}Buzzer_Struct;
void vbuzzerInit(void);
void vbuzzerSetRing(uint32_t count,uint32_t speed);
void vbuzzerRing(void);
void vbuzzerCopyArray_32(uint32_t *sourcearray,uint32_t *targetarray,uint32_t length);
#endif
更新于 阅读次数

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

flechazo 微信支付

微信支付

flechazo 支付宝

支付宝

flechazo 贝宝

贝宝