前两节学习的led的点亮以及实现流水灯操作,接下来学习蜂鸣器模块的使用

1.蜂鸣器模块的认识

2.蜂鸣器的使用原理

PNP三极管驱动电路:基级给低电平,三极管导通,基级给高电平,三极管截止

NPN三极管驱动电路:高电平触发

3.接线图

4.蜂鸣器代码(三短一长)

#include "stm32f10x.h"                  // Device header
#include "Delay.h"

int main(void)
{
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
	
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; //A15,B3,B43个IO口需要避免,因为这三个是JTAG调试端口
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOB, &GPIO_InitStructure);
	
//0响1不响(三短一长)	
while (1)
	{
		GPIO_ResetBits(GPIOB, GPIO_Pin_12);//0
		Delay_ms(100);
		GPIO_SetBits(GPIOB, GPIO_Pin_12);//1
		Delay_ms(100);
		GPIO_ResetBits(GPIOB, GPIO_Pin_12);//0
		Delay_ms(100);
		GPIO_SetBits(GPIOB, GPIO_Pin_12);//1
		Delay_ms(700);
	}
}

作者:轩宇

物联沃分享整理
物联沃-IOTWORD物联网 » 使用STM32控制蜂鸣器

发表回复