STM32蜂鸣器-库函数

版权声明:博主的博客不值钱随便转载但要注明出处 https://blog.csdn.net/easylovecsdn/article/details/81812453

需要注意的是,使用了库函数的模板就不能胡乱套用寄存器的操作,之前还想放个大招结果出丑了,这里的实验要求是绿灯亮,蜂鸣器响,红灯亮,蜂鸣器停止,蜂鸣器响起间隔半秒,led的文件我这里就不给出了,主要放出beep的文件吧,这里操作的是PF8口。

1.beep.h

#ifndef __BEEP_H
#define __BEEP_H
#include "sys.h"

#define BEEP PFout(8)

void beep_init(void);

#endif

2.beep.c

#include "beep.h"

void beep_init()
{
    GPIO_InitTypeDef GPIO_InitStructure;

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 ;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;

    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE);
    GPIO_Init(GPIOF, &GPIO_InitStructure);
    GPIO_SetBits(GPIOF, GPIO_Pin_8);
}

3.main.c

#include "stm32f4xx.h"
#include "usart.h"
#include "delay.h"
#include "led.h"
#include "beep.h"


int main(void)
{
	uart_init(115200);
	delay_init(84);
	
  led_init();
  beep_init();

  while(1){
    GPIO_ResetBits(GPIOF, GPIO_Pin_9);
    GPIO_SetBits(GPIOF, GPIO_Pin_10);
    GPIO_ResetBits(GPIOF, GPIO_Pin_8);
    delay_ms(500);

    GPIO_SetBits(GPIOF, GPIO_Pin_9);
    GPIO_ResetBits(GPIOF, GPIO_Pin_10);
    GPIO_SetBits(GPIOF, GPIO_Pin_8);
    delay_ms(500);

	}
}

猜你喜欢

转载自blog.csdn.net/easylovecsdn/article/details/81812453