当前位置:首页 > 单片机 > 单片机
[导读] 在实际的项目开发过程中,常常会遇到硬件电路的修改,然后修改的部分就需要修改驱动程序。想这样需求该来该去是程序员们最烦闷的事情(重复劳动痛不欲生啊~)。为了避免或减少重复劳动,就需要在程序的

在实际的项目开发过程中,常常会遇到硬件电路的修改,然后修改的部分就需要修改驱动程序。想这样需求该来该去是程序员们最烦闷的事情(重复劳动痛不欲生啊~)。为了避免或减少重复劳动,就需要在程序的架构上下功夫。接下来以最常见的无源蜂鸣器驱动程序为例,进行程序结构设计。



1,开发环境

1,固件库:STM32F4xx_DSP_StdPeriph_Lib_V1.8.0

2,编译器:ARMCC V5.06

3,IDE:Keil uVision5

4,操作系统:Windows 10 专业版



2,程序源码

BUZZ.h文件

/**

******************************************************************************

* @file BUZZ.h

* @author XinLi

* @version v1.0

* @date 24-October-2017

* @brief Header file for BUZZ.c module.

******************************************************************************

* @attention

*

*

Copyright © 2017 XinLi

*

* This program is free software: you can redistribute it and/or modify

* it under the terms of the GNU General Public License as published by

* the Free Software Foundation, either version 3 of the License, or

* (at your option) any later version.

*

* This program is distributed in the hope that it will be useful,

* but WITHOUT ANY WARRANTY; without even the implied warranty of

* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the

* GNU General Public License for more details.

*

* You should have received a copy of the GNU General Public License

* along with this program. If not, see .

*

******************************************************************************

*/

#ifndef __BUZZ_H

#define __BUZZ_H

#ifdef __cplusplus

extern "C" {

#endif

/* Header includes -----------------------------------------------------------*/

#include "stm32f4xx.h"

/* Macro definitions ---------------------------------------------------------*/

#define BUZZ_RCC_AHB1Periph_GPIO RCC_AHB1Periph_GPIOB

#define BUZZ_GPIO GPIOB

#define BUZZ_GPIO_Pin GPIO_Pin_8

#define BUZZ_GPIO_PinSource GPIO_PinSource8

#define BUZZ_GPIO_AF_TIM GPIO_AF_TIM4

#define BUZZ_RCC_APB1Periph_TIM RCC_APB1Periph_TIM4

#define BUZZ_TIM TIM4

#define BUZZ_TIM_Prescaler (83) /*!< Clock divided to 1MHz. */

#define BUZZ_TIM_Period (249) /*!< 250us timer interrupt. */

#define BUZZ_TIM_OCPolarity TIM_OCPolarity_High

#define BUZZ_TIM_OCInit TIM_OC3Init

#define BUZZ_TIM_OCPreloadConfig TIM_OC3PreloadConfig

#define BUZZ_TIM_SetCompare TIM_SetCompare3

#define BUZZ_TIM_IRQn TIM4_IRQn

#define BUZZ_TIM_IRQHandler TIM4_IRQHandler

#define BUZZ_TIM_IRQ_PreemptionPriority (0)

#define BUZZ_TIM_IRQ_SubPriority (0)

/* Type definitions ----------------------------------------------------------*/

typedef enum

{

BUZZ_Off = 0,

BUZZ_Ring = 1,

BUZZ_Drip = 2,

BUZZ_Warning = 3,

BUZZ_Danger = 4

}BUZZ_Status;

/* Variable declarations -----------------------------------------------------*/

/* Variable definitions ------------------------------------------------------*/

/* Function declarations -----------------------------------------------------*/

void BUZZ_SetStatus(BUZZ_Status status);

BUZZ_Status BUZZ_GetStatus(void);

/* Function definitions ------------------------------------------------------*/

#ifdef __cplusplus

}

#endif

#endif /* __BUZZ_H */


BUZZ.c文件

/**

******************************************************************************

* @file BUZZ.c

* @author XinLi

* @version v1.0

* @date 24-October-2017

* @brief BUZZ module driver.

******************************************************************************

* @attention

*

*

Copyright © 2017 XinLi

*

* This program is free software: you can redistribute it and/or modify

* it under the terms of the GNU General Public License as published by

* the Free Software Foundation, either version 3 of the License, or

* (at your option) any later version.

*

* This program is distributed in the hope that it will be useful,

* but WITHOUT ANY WARRANTY; without even the implied warranty of

* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the

* GNU General Public License for more details.

*

* You should have received a copy of the GNU General Public License

* along with this program. If not, see .

*

******************************************************************************

*/

/* Header includes -----------------------------------------------------------*/

#include "BUZZ.h"

#include

/* Macro definitions ---------------------------------------------------------*/

/* Type definitions ----------------------------------------------------------*/

/* Variable declarations -----------------------------------------------------*/

/* Variable definitions ------------------------------------------------------*/

static __IO BUZZ_Status buzzStatus = BUZZ_Off;

static __IO int buzzCount = 0;

/* Function declarations -----------------------------------------------------*/

static void BUZZ_PwmInit(void);

static void BUZZ_NvicInit(void);

static void BUZZ_Init(void);

/* Function definitions ------------------------------------------------------*/

/**

* @brief Buzz pwm initializes.

* @param None.

* @return None.

*/

static void BUZZ_PwmInit(void)

{

GPIO_InitTypeDef GPIO_InitStructure = {0};

TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure = {0};

TIM_OCInitTypeDef TIM_OCInitStructure = {0};

RCC_AHB1PeriphClockCmd(BUZZ_RCC_AHB1Periph_GPIO, ENABLE);

RCC_APB1PeriphClockCmd(BUZZ_RCC_APB1Periph_TIM, ENABLE);

GPIO_InitStructure.GPIO_Pin = BUZZ_GPIO_Pin;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;

GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;

GPIO_Init(BUZZ_GPIO, &GPIO_InitStructure);

GPIO_PinAFConfig(BUZZ_GPIO, BUZZ_GPIO_PinSource, BUZZ_GPIO_AF_TIM);

TIM_TimeBaseStructure.TIM_Prescaler = BUZZ_TIM_Prescaler;

TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

TIM_TimeBaseStructure.TIM_Period = BUZZ_TIM_Period;

TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;

TIM_TimeBaseInit(BUZZ_TIM, &TIM_TimeBaseStructure);

TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;

TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;

TIM_OCInitStructure.TIM_Pulse = 0;

TIM_OCInitStructure.TIM_OCPolarity = BUZZ_TIM_OCPolarity;

BUZZ_TIM_OCInit(BUZZ_TIM, &TIM_OCInitStructure);

BUZZ_TIM_OCPreloadConfig(BUZZ_TIM, TIM_OCPreload_Enable);

TIM_ARRPreloadConfig(BUZZ_TIM, ENABLE);

TIM_ITConfig(BUZZ_TIM, TIM_IT_Update, ENABLE);

TIM_Cmd(BUZZ_TIM, ENABLE);

}

/**

* @brief Buzz nvic initializes.

* @param None.

* @return None.

*/

static void BUZZ_NvicInit(void)

{

本站声明: 本文章由作者或相关机构授权发布,目的在于传递更多信息,并不代表本站赞同其观点,本站亦不保证或承诺内容真实性等。需要转载请联系该专栏作者,如若文章内容侵犯您的权益,请及时联系本站删除。
换一批
延伸阅读

Sept. 10, 2025 ---- 根据TrendForce集邦咨询最新发布的《全球电动车逆变器市场数据》,2025年第二季受惠纯电动车(BEV)销售成长,全球电动车(注1)牵引逆变器装机量达766万台,年增19%。...

关键字: 增程式电动车 SiC 牵引逆变器

Sept. 8, 2025 ---- 根据TrendForce集邦咨询最新调查,2025年第二季NVIDIA(英伟达) Blackwell平台规模化出货,以及北美CSP业者持续扩大布局General Server(通用型...

关键字: SSD DDR4 服务器

Sept. 4, 2025 ---- Apple(苹果)即将发布iPhone 17、iPhone 17 Air(暂名)、iPhone 17 Pro及Pro Max四款旗舰新机,除了外观辨识度升级,处理器性能、散热和拍摄功...

关键字: iPhone 16 A19处理器 折叠机

Sept. 3, 2025 ---- 根据TrendForce集邦咨询最新发布的《2025近眼显示市场趋势与技术分析》报告,2025年随着国际品牌陆续推出AR眼镜原型,以及Meta预计在近期发布AR眼镜Celeste,市...

关键字: AR眼镜 OLED

Sept. 2, 2025 ---- TrendForce集邦咨询表示,2025年第二季DRAM产业因一般型DRAM (Conventional DRAM)合约价上涨、出货量显著增长,加上HBM出货规模扩张,整体营收为3...

关键字: DRAM 智能手机 ASP

Sept. 1, 2025 ---- 根据TrendForce集邦咨询最新调查,2025年第二季因中国市场消费补贴引发的提前备货效应,以及下半年智能手机、笔电/PC、Server新品所需带动,整体晶圆代工产能利用率与出货...

关键字: 晶圆代工 智能手机 笔电

Aug. 28, 2025 ---- 根据TrendForce集邦咨询最新调查,2025年第二季NAND Flash产业虽面临平均销售价格(ASP)小幅下滑,所幸原厂减产策略缓解供需失衡,叠加中、美两大市场政策推动,整体...

关键字: NAND Flash SSD AI

Aug. 26, 2025 ---- NVIDIA(英伟达)近日推出的Jetson Thor被视为机器人的物理智慧核心,以Blackwell GPU、128 GB记忆体堆叠出2070 FP4 TFLOPS AI算力,是前...

关键字: 机器人 大型语言模型 AI算力

Aug. 21, 2025 ---- 根据TrendForce集邦咨询最新液冷产业研究,随着NVIDIA GB200 NVL72机柜式服务器于2025年放量出货,云端业者加速升级AI数据中心架构,促使液冷技术从早期试点迈...

关键字: AI 数据中心 服务器

除了充电电路外,锂电池的放电过程也需要保护。锂电池的放电电压不能低于3.0V,否则电池寿命会大幅缩短。为了实现这一保护,工程师们设计了DW01芯片与8205 MOS管的电路组合。DW01芯片能够监控锂电池的放电电压和电流...

关键字: 锂电池 电池
关闭