直接上代码
beep.c
#include "beep.h"
#include "delay.h"
void BEEP_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(BEEP_RCC, ENABLE); //使能端口时钟
GPIO_InitStructure.GPIO_Pin = BEEP_PIN; //设置输出引脚
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//速度50MHz
GPIO_Init(BEEP_GPIO_PORT, &GPIO_InitStructure); //初始化GPIO
}
void Passive_BEEP_Run(void) //运行 无源蜂鸣器
{
u16 i = 0;
for(i=0;i<5000;i++){
BEEP = 0;
delay_us(100);
BEEP = 1;
delay_us(100);
}
}
beep.h
#ifndef __BEEP_H
#define __BEEP_H
#include "sys.h"
#define BEEP PBout(13)
/* , 用户只需要修改下面代码即可任意改变相应的引脚*/
#define BEEP_GPIO_PORT GPIOB /* GPIO端口 */
#define BEEP_RCC RCC_APB2Periph_GPIOB /* GPIO端口时钟 */
#define BEEP_PIN GPIO_Pin_13
void BEEP_GPIO_Init(void);
#endif
main.c
#include "beep.h"
int main(){
BEEP_GPIO_Init();
Passive_BEEP_Run();
}
也就是给它个pwm,注意delay_us的时间