2 STM32单片机-蜂鸣器驱动

系列文章目录



前言

在各种单片机中,都离不开蜂鸣器。蜂鸣器可以作为一种简单方便地报警手段。在随之而来的智能物联中,蜂鸣器需要将报警状态上报到云端,此时就需要保存蜂鸣器的开关状态。本文为了一劳永逸,决定作出一个将来永不再更新的蜂鸣器驱动,将来所需要的改变只需要改变板子支持文件,不再关注蜂鸣器驱动文件本身,可以方便的进行移植使用。所提供的函数支持大部分场景使用。


1 硬件连接

硬件连接如图所示:
在这里插入图片描述
BEEP连接到了单片机的PB8引脚,通过控制这个引脚的高低电平,就可以控制蜂鸣器发音。

2 目录结构

在这里插入图片描述


3 软件编写

3.1 main.c

#include "board_config.h"

/************************************************
 蜂鸣器驱动
 实验现象:蜂鸣器响灭交替。
 
 淘宝店铺:https://shop475501589.taobao.com/?spm=pc_detail.29232929/evo365560b447259.shop_block.dshopinfo.5dd97dd6JvMuG3
 咸鱼店铺:https://www.goofish.com/personal?spm=a21ybx.item.itemHeader.1.c17a3da6hy8k28&userId=3890583014
 哔哩哔哩:https://space.bilibili.com/482024430?spm_id_from=333.788.upinfo.detail.click
 作者:胜磊电子
************************************************/

/************************************* 宏定义 *******************************************************/


/*********************************** 局部函数 *******************************************************/

/*
************************************************************
*	函数名称:	main
*
*	函数功能:	
*
*	入口参数:	无
*
*	返回参数:	0
*
*	说明:
************************************************************
*/
int main(void)
{
	// 初始化所有外设
    BOARD_InitAll();
    
    while (1) {
        // 主循环
		// 使用板级定义的LED对象
		// 使用蜂鸣器
		Beep_On(&BOARD_BEEP);
		
		for(uint32_t i=0;i<500000;i++);
		
		// 使用蜂鸣器
		Beep_Off(&BOARD_BEEP);
		
		for(uint32_t i=0;i<500000;i++);
    }
}

3.2 beep_driver

3.2.1 beep_driver.c

#include "beep_driver.h"

/************************************************
 淘宝店铺:https://shop475501589.taobao.com/?spm=pc_detail.29232929/evo365560b447259.shop_block.dshopinfo.5dd97dd6JvMuG3
 咸鱼店铺:https://www.goofish.com/personal?spm=a21ybx.item.itemHeader.1.c17a3da6hy8k28&userId=3890583014
 哔哩哔哩:https://space.bilibili.com/482024430?spm_id_from=333.788.upinfo.detail.click
 作者:胜磊电子
************************************************/

/**
 * @brief  初始化蜂鸣器
 * @param  beep: 蜂鸣器结构体指针
 * @param  GPIOx: GPIO端口
 * @param  GPIO_Pin: GPIO引脚
 * @retval 无
 */
void Beep_Init(Beep_TypeDef* beep, GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin) {
    GPIO_InitTypeDef GPIO_InitStructure;
    uint32_t RCC_APB2Periph_GPIOx = 0;
    
    // 根据GPIO端口确定RCC时钟
    if (GPIOx == GPIOA) {
        RCC_APB2Periph_GPIOx = RCC_APB2Periph_GPIOA;
    } else if (GPIOx == GPIOB) {
        RCC_APB2Periph_GPIOx = RCC_APB2Periph_GPIOB;
    } else if (GPIOx == GPIOC) {
        RCC_APB2Periph_GPIOx = RCC_APB2Periph_GPIOC;
    } else if (GPIOx == GPIOD) {
        RCC_APB2Periph_GPIOx = RCC_APB2Periph_GPIOD;
    } else if (GPIOx == GPIOE) {
        RCC_APB2Periph_GPIOx = RCC_APB2Periph_GPIOE;
    }
    
    // 使能GPIO时钟
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOx, ENABLE);
    
    // 配置GPIO为推挽输出
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOx, &GPIO_InitStructure);
    
    // 初始化蜂鸣器结构体
    beep->GPIOx = GPIOx;
    beep->GPIO_Pin = GPIO_Pin;
    beep->state = BEEP_OFF;
    
    // 默认关闭蜂鸣器
    GPIO_ResetBits(GPIOx, GPIO_Pin);
}

/**
 * @brief  打开蜂鸣器
 * @param  beep: 蜂鸣器结构体指针
 * @retval 无
 */
void Beep_On(Beep_TypeDef* beep) {
    GPIO_SetBits(beep->GPIOx, beep->GPIO_Pin);
    beep->state = BEEP_ON;
}

/**
 * @brief  关闭蜂鸣器
 * @param  beep: 蜂鸣器结构体指针
 * @retval 无
 */
void Beep_Off(Beep_TypeDef* beep) {
    GPIO_ResetBits(beep->GPIOx, beep->GPIO_Pin);
    beep->state = BEEP_OFF;
}

/**
 * @brief  切换蜂鸣器状态
 * @param  beep: 蜂鸣器结构体指针
 * @retval 无
 */
void Beep_Toggle(Beep_TypeDef* beep) {
    if (beep->state == BEEP_ON) {
        Beep_Off(beep);
    } else {
        Beep_On(beep);
    }
}

/**
 * @brief  获取蜂鸣器状态
 * @param  beep: 蜂鸣器结构体指针
 * @retval 蜂鸣器状态
 */
Beep_State Beep_GetState(Beep_TypeDef* beep) {
    return beep->state;
}

3.2.2 beep_driver.h

#ifndef BEEP_DRIVER_H
#define BEEP_DRIVER_H

#include "system_config.h"

/************************************************
 淘宝店铺:https://shop475501589.taobao.com/?spm=pc_detail.29232929/evo365560b447259.shop_block.dshopinfo.5dd97dd6JvMuG3
 咸鱼店铺:https://www.goofish.com/personal?spm=a21ybx.item.itemHeader.1.c17a3da6hy8k28&userId=3890583014
 哔哩哔哩:https://space.bilibili.com/482024430?spm_id_from=333.788.upinfo.detail.click
 作者:胜磊电子
************************************************/

// 蜂鸣器状态枚举
typedef enum {
    BEEP_OFF = 0,
    BEEP_ON = 1
} Beep_State;

// 蜂鸣器结构体定义
typedef struct {
    GPIO_TypeDef* GPIOx;     // GPIO端口
    uint16_t GPIO_Pin;       // GPIO引脚
    Beep_State state;        // 蜂鸣器当前状态
} Beep_TypeDef;

// 函数声明
void Beep_Init(Beep_TypeDef* beep, GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);
void Beep_On(Beep_TypeDef* beep);
void Beep_Off(Beep_TypeDef* beep);
void Beep_Toggle(Beep_TypeDef* beep);
Beep_State Beep_GetState(Beep_TypeDef* beep);

#endif /* BEEP_DRIVER_H */  

3.3 board_config

3.3.1 board_config.c

#include "board_config.h"

/************************************************
 淘宝店铺:https://shop475501589.taobao.com/?spm=pc_detail.29232929/evo365560b447259.shop_block.dshopinfo.5dd97dd6JvMuG3
 咸鱼店铺:https://www.goofish.com/personal?spm=a21ybx.item.itemHeader.1.c17a3da6hy8k28&userId=3890583014
 哔哩哔哩:https://space.bilibili.com/482024430?spm_id_from=333.788.upinfo.detail.click
 作者:胜磊电子
************************************************/

// 定义蜂鸣器对象
Beep_TypeDef BOARD_BEEP;

void BOARD_InitBeep(void) {
    // 初始化蜂鸣器 (PB8)
    Beep_Init(&BOARD_BEEP, GPIOB, GPIO_Pin_8);
}

void BOARD_InitAll(void) {
    // 初始化所有外设
    BOARD_InitBeep();
}

3.3.2 board_config.h

#ifndef BOARD_CONFIG_H
#define BOARD_CONFIG_H

#include "beep_driver.h"

/************************************************
 淘宝店铺:https://shop475501589.taobao.com/?spm=pc_detail.29232929/evo365560b447259.shop_block.dshopinfo.5dd97dd6JvMuG3
 咸鱼店铺:https://www.goofish.com/personal?spm=a21ybx.item.itemHeader.1.c17a3da6hy8k28&userId=3890583014
 哔哩哔哩:https://space.bilibili.com/482024430?spm_id_from=333.788.upinfo.detail.click
 作者:胜磊电子
************************************************/

// 导出蜂鸣器对象供外部使用
extern Beep_TypeDef BOARD_BEEP;

// 板子初始化函数
void BOARD_InitAll(void);

void BOARD_InitBeep(void);

#endif /* BOARD_CONFIG_H */    

3.4 utils

3.4.1 system_config.h

#ifndef SYSTEM_CONFIG_H
#define SYSTEM_CONFIG_H

// 包含所有必要的STM32标准库头文件
#include "stm32f10x.h"
#include "stm32f10x_gpio.h"
#include "stm32f10x_rcc.h"

// 其他常用头文件
#include <stdint.h>
#include <stdbool.h>

#endif /* SYSTEM_CONFIG_H */    

总结

通过这样封装,将来只需要将重点放在board_config里面就可以完成不同板子之间的移植了。不需要再关心蜂鸣器驱动的实现。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值