历史上的今天
今天是:2025年04月23日(星期三)
2018年04月23日 | STM32F4(BUZZ)
2018-04-23 来源:eefocus
在实际的项目开发过程中,常常会遇到硬件电路的修改,然后修改的部分就需要修改驱动程序。想这样需求该来该去是程序员们最烦闷的事情(重复劳动痛不欲生啊~)。为了避免或减少重复劳动,就需要在程序的架构上下功夫。接下来以最常见的无源蜂鸣器驱动程序为例,进行程序结构设计。
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)
{
NVIC_InitTypeDef NVIC_InitStructure = {0};
NVIC_InitStructure.NVIC_IRQChannel = BUZZ_TIM_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = BUZZ_TIM_IRQ_PreemptionPriority;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = BUZZ_TIM_IRQ_SubPriority;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
/**
* @brief Buzz initializes.
* @param None.
* @return None.
*/
static void BUZZ_Init(void)
{
static bool init_flag = false;
if(init_flag == false)
{
init_flag = true;
BUZZ_PwmInit();
BUZZ_NvicInit();
}
}
/**
* @brief Set buzz status.
* @param [in] status: Buzz status.
* @return None.
*/
void BUZZ_SetStatus(BUZZ_Status status)
{
if(status != buzzStatus)
{
buzzStatus = status;
buzzCount = 0;
BUZZ_Init();
}
}
/**
* @brief Get buzz status.
* @param None.
* @return Buzz status.
*/
BUZZ_Status BUZZ_GetStatus(void)
{
return buzzStatus;
}
/**
* @brief This function handles TIM Handler.
* @param None.
* @return None.
*/
void BUZZ_TIM_IRQHandler(void)
{
if(TIM_GetITStatus(BUZZ_TIM, TIM_IT_Update) != RESET)
{
TIM_ClearITPendingBit(BUZZ_TIM, TIM_IT_Update);
if(buzzStatus == BUZZ_Off)
{
buzzCount = 0;
BUZZ_TIM_SetCompare(BUZZ_TIM, 0);
}
else if(buzzStatus == BUZZ_Ring)
{
buzzCount = 0;
BUZZ_TIM_SetCompare(BUZZ_TIM, (BUZZ_TIM_Period + 1) / 2);
}
else if(buzzStatus == BUZZ_Drip)
{
if(buzzCount > 1999)
{
if(buzzCount > 3999)
{
buzzCount = 0;
buzzStatus = BUZZ_Off;
BUZZ_TIM_SetCompare(BUZZ_TIM, 0);
}
else
{
buzzCount++;
BUZZ_TIM_SetCompare(BUZZ_TIM, 0);
}
}
else
{
buzzCount++;
BUZZ_TIM_SetCompare(BUZZ_TIM, (BUZZ_TIM_Period + 1) / 2);
}
}
else if(buzzStatus == BUZZ_Warning)
{
if(buzzCount > 1999)
{
if(buzzCount > 3999)
{
buzzCount = 0;
BUZZ_TIM_SetCompare(BUZZ_TIM, (BUZZ_TIM_Period + 1) / 2);
}
else
{
buzzCount++;
BUZZ_TIM_SetCompare(BUZZ_TIM, 0);
}
}
else
{
buzzCount++;
BUZZ_TIM_SetCompare(BUZZ_TIM, (BUZZ_TIM_Period + 1) / 2);
}
}
else
{
if(buzzCount > 999)
{
if(buzzCount > 1999)
{
buzzCount = 0;
BUZZ_TIM_SetCompare(BUZZ_TIM, (BUZZ_TIM_Period + 1) / 2);
}
else
{
buzzCount++;
BUZZ_TIM_SetCompare(BUZZ_TIM, 0);
}
}
else
{
buzzCount++;
BUZZ_TIM_SetCompare(BUZZ_TIM, (BUZZ_TIM_Period + 1) / 2);
}
}
}
}
main.c文件
/**
******************************************************************************
* @file main.c
* @author XinLi
* @version v1.0
* @date 24-October-2017
* @brief Main program body.
******************************************************************************
* @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 "main.h"
#include "Delay.h"
#include "BUZZ.h"
/* Macro definitions ---------------------------------------------------------*/
/* Type definitions ----------------------------------------------------------*/
/* Variable declarations -----------------------------------------------------*/
/* Variable definitions ------------------------------------------------------*/
/* Function declarations -----------------------------------------------------*/
/* Function definitions ------------------------------------------------------*/
/**
* @brief Main program.
* @param None.
* @return None.
*/
int main(void)
{
for(;;)
{
Delay_s(5);
BUZZ_SetStatus(BUZZ_Drip);
Delay_s(5);
BUZZ_SetStatus(BUZZ_Ring);
Delay_s(5);
BUZZ_SetStatus(BUZZ_Warning);
Delay_s(5);
BUZZ_SetStatus(BUZZ_Danger);
Delay_s(5);
BUZZ_SetStatus(BUZZ_Off);
}
}
3,注意
修改接口需要注意,蜂鸣器的驱动方式和使用的TIM。对应修改PWM输出电平和TIM时钟配置函数。
上一篇:STM32F4(读取芯片ID)
下一篇:STM32F4(KEY)
史海拾趣
|
程序的主要目的是将0~0xff这256个数按顺序写入到EEPROM(AT24C16)的内部存储单元中,然后再依次将他们读出,并通过实验室板的串口UART0输出到PC机上运行的超级终端上。 程序如下: #include #include \"2410addr.h\" #include \"2410lib.h\" ...… 查看全部问答> |
|
通过FormatVolumeEx,以FAT32格式化NandFlash后,从SD卡复制文件到格式化的分区中。但是最后对注册表的写操作,在关机重启之前,都存在。一旦关机重启,注册表写操作的数据全部丢失,不知为什么?… 查看全部问答> |
|
一个内存驱动器的源代码 http://www.pudn.com/downloads/sourcecode/windows/vxd/detail1558.html 虚拟串口用VC编译 http://www.pudn.com/downloads170/sourcecode/windows/vxd/detail788464.html 8139网卡驱动源码 http://www.pudn.co ...… 查看全部问答> |
|
单片机C程序的变量都是8位的,现在想传递一个32位或24位的变量参数,怎么传啊? 用指针?还是结构? 麻烦写个简单的程序,如A函数调用B函数传参数。… 查看全部问答> |
|
请教:对ti串口转网口套件重新下载bootloader后产生的问题 对ti串口转网口套件重新下载自己改的bootloader后,再也连不上该套件,也无法重新下载了,哪位知道如何解决呀,谢谢:(… 查看全部问答> |




