[其他] STM32蜂鸣器的使用

[复制链接]
 楼主| 王派oo 发表于 2023-6-29 19:58 | 显示全部楼层 |阅读模式
硬件介绍

一般stm32开发板上通过io口来连接蜂鸣器的使用,因此可以直接类比点亮led灯的使用开发


## 软件设计

beep.h


  1. #ifndef _beep_H
  2. #define _beep_H


  3. #include "system.h"
  4. #define BEEP_PORT_RCC  RCC_APB2Periph_GPIOB
  5. #define BEEP_PIN   GPIO_Pin_8
  6. #define BEEP_PORT   GPIOB
  7. void BEEP_Init();
  8. #define BEEP PBout(8)


  9. #endif

    1. #ifndef _beep_H
    2. #define _beep_H


    3. #include "system.h"
    4. #define BEEP_PORT_RCC  RCC_APB2Periph_GPIOB
    5. #define BEEP_PIN   GPIO_Pin_8
    6. #define BEEP_PORT   GPIOB
    7. void BEEP_Init();
    8. #define BEEP PBout(8)


    9. #endif




 楼主| 王派oo 发表于 2023-6-29 19:58 | 显示全部楼层
beep.c
  1. #include "beep.h"


  2. void BEEP_Init()
  3. {
  4.         GPIO_InitTypeDef GPIO_InitStructure;
  5.        
  6.         RCC_APB2PeriphClockCmd(BEEP_PORT_RCC, ENABLE);
  7.        
  8.         GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
  9.         GPIO_InitStructure.GPIO_Pin=BEEP_PIN;
  10.         GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
  11.         GPIO_Init(BEEP_PORT,&GPIO_InitStructure);
  12.         GPIO_ResetBits(BEEP_PORT,BEEP_PIN);

  13. }
 楼主| 王派oo 发表于 2023-6-29 19:59 | 显示全部楼层
main.c

  1. #include "stm32f10x.h"
  2. #include "led.h"
  3. #include "system.h"
  4. #include "SysTick.h"
  5. #include "beep.h"
  6. int main()
  7. {
  8.   SysTick_Init(72);
  9.         BEEP_Init();
  10.         LED_Init();
  11.         while(1)
  12.         {
  13.                 LED1=!LED1;//灯来回闪烁
  14.                 BEEP=!BEEP;//随着灯闪烁发声
  15.                 delay_ms(500);
  16.        
  17.         }
  18. }
您需要登录后才可以回帖 登录 | 注册

本版积分规则

19

主题

308

帖子

0

粉丝
快速回复 返回顶部 返回列表