beep.c文件下代码:
#include "beep.h"
//初始化PF8为输出口
//BEEP IO 初始化
void BEEP_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE);//使能 GPIOF时钟
//初始化蜂鸣器对应引脚 GPIOF8
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;//普通输出模式
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;//下拉
GPIO_Init(GPIOF, &GPIO_InitStructure);//初始化 GPIO
GPIO_ResetBits(GPIOF,GPIO_Pin_8); //蜂鸣器对应引脚GPIOF8 拉低,
}
这段代码 仅包含1个函数: void BEEP_Init(void),该函数的作用就是使能PORTF的时钟,然后调用GPIO_Init函数, 配置PF8为推挽输出。 IO口的初始化跟上一讲跑马灯实验非常类似,这里我们就不做过多讲解。
保存 beep.c代码,然后我们按同样