当前位置:网站首页>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 */
边栏推荐
- 金仓数据库 KingbaseES SQL 语言参考手册 (13. SQL语句:ALTER SYNONYM 到 COMMENT)
- 【Pytorch进阶】pytorch模型的保存与使用
- 操作系统常见面试题目总结,含答案
- BluePrism流程业务对象的组件功能介绍-RPA第三章
- Collection of original IOS interview questions
- 数据库笔记(来自老社)
- Digital transformation of enterprises has become a general trend, and it is important to choose the right online collaboration tools
- Leetcode daily practice - 26. Delete duplicates in an ordered array
- 金仓数据库 KingbaseES SQL 语言参考手册 (15. SQL语句:CREATE MATERIALIZED VIEW 到 CREATE SCHEMA)
- cv2.resize()
猜你喜欢

Codeforces Round #810 (Div. 2)(A~C)

Overview of canvas

Decompile jar files (idea environment)

Digital transformation of enterprises has become a general trend, and it is important to choose the right online collaboration tools

Ten sorting details

three.js 给地球加标签和弹窗

几张图帮你捋清“中国金融机构体系”

.NET GC工作流程

C # use the default transformation method

开源闹出乌龙事件,可能你不知道这五种开源协议
随机推荐
靠元宇宙和NFT,天下秀疯狂“割韭菜”?
go+mysql+redis+vue3简单聊室,第5弹:使用消息队列和定时任务同步消息到mysql
Week 6 Convolutional Neural Networks (CNNs)
金融机构盘点
How to wait for the return results of multiple asynchronous tasks synchronously?
安全测试初学体验
numpy.zeros_like
事务回滚,同时记录异常信息
Zhongtian steel uses tdengine in GPS and AIS scheduling
C#异步编程看这篇就够了
一个开源的网页画板,真的太方便了
three.js 制作地球标注的两种方法
20220725树状数组入门反思
金仓数据库 KingbaseES SQL 语言参考手册 (16. SQL语句: CREATE SEQUENCE 到 DELETE)
高瓴加入的PRI,君联、华控、盛世、绿动等百家机构都杀进去了
LeetCode_回溯_中等_216.组合总和 III
N圆最密堆积、最小外接正方形的matlab求解(二维、三维等圆Packing 问题)
模拟身份验证
Leetcode daily practice - 26. Delete duplicates in an ordered array
金仓数据库 KingbaseES SQL 语言参考手册 (20. SQL语句: MERGE 到 VALUES)