Buzzer蜂鸣器驱动

本文介绍了一种将外设驱动如蜂鸣器整合为结构体的编程方法,利用定时器实现自动关闭功能,提供SetRing函数进行灵活控制,包括响声次数和速度设置。作者分享了如何在代码中使用Buzzer模块及其初始化过程。
摘要由CSDN通过智能技术生成

我有一个思想:所有的外设驱动,都搞成一个结构体!

下面这个是蜂鸣器的。相当好用,但是得用到一个定时器。稍后整理完定时器也会发布出来。

这个驱动,我还是挺喜欢的,是写蜂鸣器唱歌时突发奇想。因为蜂鸣器需要打开和关闭,这样就得在两个地方执行。就很烦。so,便诞生了这个。弄个定时器让他自己关不就好了吗?

使用也变得超级方便了。

1、将 Buzzer.control.Ring () 这个函数放在 while 循环中

2、在任何地方 Buzzer.control.SetRing (2,4), 这里两个参数、次数、速度

在使用的过程中又发现连续添加响声的问题,于是弄了个数组,逐个响,调用2添加响声。可以同时添加n多组。感觉用起来更加方便了呢。

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
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

FlechazoCLF

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值