蓝桥杯_嵌入式:蜂鸣器(库函数)


一、原理图:

图下是蜂鸣器使用的引脚 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);	
}

总结:

这里对蓝桥杯嵌入式的的蜂鸣器主要代码做了部分描述,考察对引脚的基本使用。

文章若出现什么问题,谢谢及时反馈。


点个赞在走吧!!!!

  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值