蜂鸣器硬件连接
硬件解释 IO口电平不足以驱动蜂鸣器,需接一个放大电路,因为IO口复位后默认状况浮空(电平处于不确定状态可能是1,可能是0),蜂鸣器有可能误响,所以接一个下拉电阻,将干扰小电流下拉到地。
(ps:关于IO口电流大小的博客
https://blog.csdn.net/wuhuijun165/article/details/85686864)
程序步骤
BEEP.c
void BEEP_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;//结构体
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE);//配置时钟
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;//F8
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;//输出
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//推挽
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;//下拉
GPIO_Init(GPIOF, &GPIO_InitStructure);//GPIO初始化
GPIO_ResetBits(GPIOF,GPIO_Pin_8);//拉低,初始不响
}
main()函数
GPIO_ResetBits(GPIOF,GPIO_Pin_8);//不响
delay_ms(300);
GPIO_SetBits(GPIOF,GPIO_Pin_8); //响
delay_ms(300);