当前位置:网站首页>STM32F103 active buzzer driver
STM32F103 active buzzer driver
2022-07-26 20:13:00 【Sell yourself to buy a lens】
The active buzzer is connected to GPIOB Of the 5 foot
buzzer.h file
#ifndef __BUZZER_H
#define __BUZZER_H
#include "sys.h"
#define BUZZERPORT GPIOB // Definition IO Interface
#define BUZZER GPIO_Pin_5 // Definition IO Interface
void BUZZER_Init(void);// initialization
void BUZZER_BEEP1(void);// Let's go
#endif
buzzer.c file
#include "buzzer.h"
#include "delay.h"
void BUZZER_Init(void){
// Interface initialization of buzzer
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = BUZZER; // Select the port number
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; // choice IO How the interface works
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; // Set up IO Interface speed (2/10/50MHz)
GPIO_Init(BUZZERPORT, &GPIO_InitStructure);
GPIO_WriteBit(BUZZERPORT,BUZZER,(BitAction)(1)); // Buzzer interface outputs high level 1
}
void BUZZER_BEEP1(void){
// The buzzer rings
u16 i;
for(i=0;i<200;i++){
GPIO_WriteBit(BUZZERPORT,BUZZER,(BitAction)(0)); // Buzzer interface output 0
delay_us(500); // Time delay
GPIO_WriteBit(BUZZERPORT,BUZZER,(BitAction)(1)); // Buzzer interface outputs high level 1
delay_us(500); // Time delay
}
}
/* choice IO How the interface works : GPIO_Mode_AIN Analog input GPIO_Mode_IN_FLOATING Floating input GPIO_Mode_IPD Drop down input GPIO_Mode_IPU Pull up input GPIO_Mode_Out_PP Push pull output GPIO_Mode_Out_OD Open drain output GPIO_Mode_AF_PP Multiplexing push pull output GPIO_Mode_AF_OD Reuse open drain output */
main.c
/********************************************************************************************* The program name : Buzzer driver ( In the key control LED Add the prompt sound on the basis ) Hardware support : STM32F103C8 External crystal oscillator 8MHz RCC Function to set the dominant frequency 72MHz *********************************************************************************************/
#include "stm32f10x.h" //STM32 The header file
#include "sys.h"
#include "delay.h"
#include "led.h"
#include "key.h"
#include "flash.h"
#include "buzzer.h"
#define FLASH_START_ADDR 0x0801f000 // The starting address for writing
int main (void){
// The main program
u16 a; // Defining variables
// Initializer
RCC_Configuration(); // The clock is set
LED_Init();//LED initialization
KEY_Init();// Key initialization
BUZZER_Init();// Buzzer initialization
BUZZER_BEEP1();// Buzzer sound 1
a = FLASH_R(FLASH_START_ADDR);// Read from the address of the specified page FLASH
GPIO_Write(LEDPORT,a|0xfffc&GPIO_ReadOutputData(LEDPORT)); // Direct numeric operations write variable values to LED(LED stay GPIOB Of the group PB0 and PB1 On )
// Main circulation
while(1){
// Example 4: There is a latch
if(!GPIO_ReadInputDataBit(KEYPORT,KEY1)){
// Read the level of the key interface
delay_ms(20); // Time delay 20ms To shake
if(!GPIO_ReadInputDataBit(KEYPORT,KEY1)){
// Read the level of the key interface
// stay 2 individual LED Binary addition is displayed on the
a++; // Variable plus 1
if(a>3){
// When the variable is greater than 3 Shi Qing 0
a=0;
}
GPIO_Write(LEDPORT,a|0xfffc&GPIO_ReadOutputData(LEDPORT)); // Direct numeric operations write variable values to LED(LED stay GPIOB Of the group PB0 and PB1 On )
BUZZER_BEEP1();// Buzzer sound 1
FLASH_W(FLASH_START_ADDR,a); // Write... From the address of the specified page FLASH
while(!GPIO_ReadInputDataBit(KEYPORT,KEY1)); // Wait for the key to release
}
}
}
}
/* 【 Variable definitions 】 u32 a; // Definition 32 Bit unsigned variable a u16 a; // Definition 16 Bit unsigned variable a u8 a; // Definition 8 Bit unsigned variable a vu32 a; // Definition variable 32 Bit unsigned variable a vu16 a; // Definition variable 16 Bit unsigned variable a vu8 a; // Definition variable 8 Bit unsigned variable a uc32 a; // Define read-only 32 Bit unsigned variable a uc16 a; // Define read-only Of 16 Bit unsigned variable a uc8 a; // Define read-only Of 8 Bit unsigned variable a #define ONE 1 // Macro definition delay_us(1); // Time delay 1 Microsecond delay_ms(1); // Time delay 1 millisecond delay_s(1); // Time delay 1 second */
边栏推荐
- C # use the default transformation method
- C#将PDF文件转成图片
- UE5编辑器Slate快速入门【开篇】
- Record an analysis of a.Net property management background service stuck
- Excel-VBA 快速上手(十二、Like 比较的常见用法)
- 【机器学习】变量间的相关性分析
- regular expression
- Kingbases SQL language reference manual of Jincang database (13. SQL statement: alter synonym to comment)
- 一年卖7亿,德州扒鸡赶考IPO
- 【二叉树】将二叉搜索树变平衡
猜你喜欢

Canvas graphics

并行执行(二)、multiprocessing

How to build a super interface collaboration platform: count the six weapons of apifox

Leetcode daily practice - 26. Delete duplicates in an ordered array

内网渗透学习(二)信息收集

Excel-VBA 快速上手(十、提示框、可输入的弹出框)

一文读懂 .NET 中的高性能队列 Channel

vs如何读取mysql中的数据(顺便通过代码解决了中文乱码问题)

会议OA之会议排座&送审

直播预约有奖| 高级咨询顾问徐雁斐:效能度量如何助力高效精细的外包管理
随机推荐
C#将PDF文件转成图片
Docker使用mysql:5.6和 owncloud 镜像,构建一个个人网盘,安装搭建私有仓库 Harbor
Fasttunnel open source intranet penetration framework
低代码工具有哪些特色?明眼人都能看出来的低代码两大发展轨迹!
Use request header authentication to test API interfaces that need authorization
ES6的方法&类数组转成真正的数组&判断数组的方法
【MySQL】 - 索引原理与使用
Kingbases SQL language reference manual of Jincang database (13. SQL statement: alter synonym to comment)
福建争抢VC/PE
密室逃脱、剧本杀加强监管 重点加强消防安全和未成年人保护
Read the high-performance queue channel in.Net
An open source web drawing board is really convenient
Impersonate authentication
金仓数据库 KingbaseES SQL 语言参考手册 (19. SQL语句: DROP TABLE 到 LOAD)
eadiness probe failed: calico/node is not ready: BIRD is not ready: Error querying BIRD: unable to c
猎聘问卷星,成为微信「寄生虫」
千亿酸奶赛道,乳企巨头和新品牌打响拉锯战
Excel-VBA 快速上手(十、提示框、可输入的弹出框)
一文看懂中国的金融体系
Solution to the third game of 2022 Niuke multi school league