STM32学习笔记:蜂鸣器实验
蜂鸣器实现原理和led点亮大同小异,所以就不重新编写了。
一.所使用的函数
1.时钟使能函数
RCC_APB2PeriphClockCmd(uint32_t RCC_APB2Periph, FunctionalState NewState)
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
2.引脚初始化函数
GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct)
GPIO_Init(GPIOB,&GPIO_InitStructure);
3.对IO口置1
GPIO_SetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
GPIO_SetBits(GPIOB,GPIO_Pin_8);
4.对IO口置0
GPIO_ResetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
GPIO_ResetBits(GPIOB,GPIO_Pin_8);
二.硬件部分
蜂鸣器有两种有源蜂鸣器和无源蜂鸣器。有源和无源这里的“源”不是指电源,而是指震荡源。也就是说,有源蜂鸣器内部带震荡源,所以只要一通电就会叫。而无源内部不带震荡源,所以如果用直流信号无法令其鸣叫。必须用2K~5K的方波去驱动它。
三.代码编写过程整理
1.编写beep.h头文件
#ifndef __BEEP_H
#define __BEEP_H
void beep_Iint(void);
#endif
2.编写beep.c文件
在该文件中引用了beep.h的头文件,先声明了 void beep_Iint(void) 这个函数,然后对该函数进行定义
#include "beep.h"
#include "stm32f10x.h"
void beep_Iint(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_8;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitStructure);
GPIO_SetBits(GPIOB,GPIO_Pin_8);
}
在这部分编写代码的时候,首先运用到的是rcc头文件中的使能时钟的函数,基本上所有的程序在开始都需要使能时钟,然后就是对时钟进行初始化,在go to definition之后,我们发现它的入口第二个条件是一个结构体,我们就定义了一个指针GPIO_InitStructure指向GPIO_InitTypeDef ,GPIO_InitTypeDef是一个结构体变量,该变量在Stm32f10x_gpio.h头文件中被定义,原型如下:
typedef struct
{
uint16_t GPIO_Pin;
GPIOSpeed_TypeDef GPIO_Speed;
GPIOMode_TypeDef GPIO_Mode;
}GPIO_InitTypeDef;
所以我们分别要对这三个结构体成员进行赋值:
首先,第一个变量为GPIO_Pin是一个16为的无符号数(uint),该数只有16位,每一位代表一个引脚,如果想要要配置某一个端口的某一个引脚只需要把相应的位置1就可以了。在STM32的固件库中有如下引脚号定义:
#define GPIO_Pin_0 ((uint16_t)0x0001)
#define GPIO_Pin_1 ((uint16_t)0x0002)
#define GPIO_Pin_2 ((uint16_t)0x0004)
#define GPIO_Pin_3 ((uint16_t)0x0008)
#define GPIO_Pin_4 ((uint16_t)0x0010)
#define GPIO_Pin_5 ((uint16_t)0x0020)
#define GPIO_Pin_6 ((uint16_t)0x0040)
#define GPIO_Pin_7 ((uint16_t)0x0080)
#define GPIO_Pin_8 ((uint16_t)0x0100)
#define GPIO_Pin_9 ((uint16_t)0x0200)
#define GPIO_Pin_10 ((uint16_t)0x0400)
#define GPIO_Pin_11 ((uint16_t)0x0800)
#define GPIO_Pin_12 ((uint16_t)0x1000)
#define GPIO_Pin_13 ((uint16_t)0x2000)
#define GPIO_Pin_14 ((uint16_t)0x4000)
#define GPIO_Pin_15 ((uint16_t)0x8000)
#define GPIO_Pin_All ((uint16_t)0xFFFF)
其次, GPIOSpeed_TypeDef是一个枚举变量,用来存储GPIO的速度,其定义如下:
typedef enum
{
GPIO_Speed_10MHz = 1,
GPIO_Speed_2MHz,
GPIO_Speed_50MHz
}GPIOSpeed_TypeDef;
它有三种取值,也就是说GPIO的速度有三种取值,枚举变量为1时GPIO的速度对应10MHz,枚举变量为2时GPIO的速度对应2MHz,枚举变量为3时GPIO的速度对应50MHz,总结如下图:
GPIO的速度 | 枚举变量的取值 |
---|
10MHz | 1 |
2MHz | 2 |
50MHz | 3 |
最后, GPIOMode_TypeDef也是一个枚举变量,用来存储GPIO的工作模式,其定义如下:
typedef enum
{ GPIO_Mode_AIN = 0x0,
GPIO_Mode_IN_FLOATING = 0x04,
GPIO_Mode_IPD = 0x28,
GPIO_Mode_IPU = 0x48,
GPIO_Mode_Out_OD = 0x14,
GPIO_Mode_Out_PP = 0x10,
GPIO_Mode_AF_OD = 0x1C,
GPIO_Mode_AF_PP = 0x18
}GPIOMode_TypeDef;
在对结构体赋值完之后,我们对led的IO口进行置1,主要是为了让led保持熄灭的状态,在这里我们用到了GPIO_SetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)这个函数:
GPIO_SetBits(GPIOB,GPIO_Pin_8);
3.编写main.c文件
#include "stm32f10x.h"
#include "beep.h"
#include "delay.h"
int main (void)
{
beep_Iint();
delay_init();
while(1)
{
GPIO_SetBits(GPIOB,GPIO_Pin_5);
delay_ms(300);
GPIO_ResetBits(GPIOB,GPIO_Pin_5);
delay_ms(300);
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)