硬件
正点原子探索者STM32F407ZGT6
软件
1、Keil uVision5
代码
1、beep.c中关键代码
void BEEP_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
//使能GPIOF时钟
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF,ENABLE);
//初始化蜂鸣器对应引脚GPIOF8
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
//普通输出模式
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
//推挽输出
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
//100MHz
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
//下拉
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
//初始化GPIO
GPIO_Init(GPIOF,&GPIO_InitStructure);
//关闭蜂鸣器:蜂鸣器对应引脚GPIOF8拉低
GPIO_ResetBits(GPIOF,GPIO_Pin_8);
}
2、main.c中关键代码
int main(void)
{
delay_init(168); //初始化延时函数
LED_Init(); //初始化LED端口
BEEP_Init(); //初始化蜂鸣器端口
while(1)
{
// DS0拉低,亮 等同LED0=0;
GPIO_ResetBits(GPIOF,GPIO_Pin_9);
// DS1拉低,亮
LED1=0;
//BEEP引脚拉高, 等同BEEP=1;
GPIO_SetBits(GPIOF,GPIO_Pin_8);
//延时300ms
delay_ms(300);
// DS0拉高,灭 等同LED0=1;
GPIO_SetBits(GPIOF,GPIO_Pin_9);
// DS1拉高,灭
LED1=1;
//BEEP引脚拉低, 等同BEEP=0;
GPIO_ResetBits(GPIOF,GPIO_Pin_8);
//延时300ms
delay_ms(300);
}
}
代码下载
嵌入式软件开发 STM32F407 蜂鸣器 LED灯 标准库版