一、原理图:
图下是蜂鸣器使用的引脚 PB4
二、部分代码:
1.deep.h:
#ifndef __DEEP_H
#define __DEEP_H
/* Includes ------------------------------------------------------------------*/
#include "stm32f10x.h"
#define deep_on() GPIO_ResetBits(GPIOB,GPIO_Pin_4); // 开启蜂鸣器
#define deep_off() GPIO_SetBits(GPIOB,GPIO_Pin_4); //关闭蜂鸣器
void deep_Init(void);
#endif
2.deep.c:
#include"deep.h"
#include "stm32f10x.h"
void deep_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE); //使能PB
GPIO_InitStruct.GPIO_Mode=GPIO_Mode_Out_PP; //推挽输出
GPIO_InitStruct.GPIO_Pin=GPIO_Pin_4;
GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitStruct);
}
3. main.c:
#include "stm32f10x.h"
#include "lcd.h"
#include "buzzer.h"
void Delay_Ms(u32 nTime);
int main(void)
{
SysTick_Config(SystemCoreClock/1000);
Delay_Ms(200);
STM3210B_LCD_Init();
deep_Init();
deep_on(); //GPIO_ResetBits(GPIOB,GPIO_Pin_4);
Delay_Ms(500);
deep_off(); //GPIO_SetBits(GPIOB,GPIO_Pin_4);
while(1)
{
}
}
void Delay_Ms(u32 nTime)
{
TimingDelay = nTime;
while(TimingDelay != 0);
}
总结:
这里对蓝桥杯嵌入式的的蜂鸣器主要代码做了部分描述,考察对引脚的基本使用。
文章若出现什么问题,谢谢及时反馈。
点个赞在走吧!!!!