OpenEdv-开源电子网

 找回密码
 立即注册
正点原子全套STM32/Linux/FPGA开发资料,上千讲STM32视频教程免费下载...
查看: 2304|回复: 0

STM32F1位操作使蜂鸣器发声

[复制链接]

28

主题

28

帖子

0

精华

初级会员

Rank: 2

积分
116
金钱
116
注册时间
2019-10-16
在线时间
18 小时
发表于 2019-10-28 19:02:43 | 显示全部楼层 |阅读模式
使能IO口时钟。调用函数RCC_APB2PeriphColckCmd();
     不同的IO组,调用的时钟使能函数不一样。
初始化IO口模式。调用函数BEEP_Init();
操作IO口,输出高低电平。
beep.h
  1. #ifndef __BEEP_H
  2. #define __BEEP_H
  3. #include "sys.h"

  4. #define BEEP PBout(8)
  5. void BEEP_Init(void);

  6. #endif
复制代码

beep.c
  1. #include "beep.h"
  2. #include "stm32f10x.h"

  3. void BEEP_Init()
  4. {
  5.        
  6.         GPIO_InitTypeDef GPIO_InitStructure;
  7.        
  8.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOE,ENABLE);//使能GPIOA和GPIOB
  9.                                                                                                                                                                                                                                                                                                          //| 既是逻辑运算符也是位运算符;|| 只是逻辑运算符
  10.                                                                                                                                                                                                                                                                                                                 //| 不具有短路效果,即左边true,右边还会执行;
  11.         GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
  12.         GPIO_InitStructure.GPIO_Pin=GPIO_Pin_8;
  13.         GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
  14.         GPIO_Init(GPIOB,&GPIO_InitStructure);         //根据参数初始化CPIOB.8
  15.   GPIO_SetBits(GPIOB,GPIO_Pin_8);               //输出0,关闭蜂鸣器
  16. }
复制代码


main.c
  1. #include "beep.h"
  2. #include "delay.h"


  3. int main(void)
  4. {
  5. delay_init();
  6. BEEP_Init();
  7.        
  8. while(1)
  9. {
  10.         BEEP=1;
  11.         delay_ms(500);
  12.         BEEP=0;
  13.         delay_ms(500);
  14. }
  15. }
复制代码


正点原子逻辑分析仪DL16劲爆上市
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则



关闭

原子哥极力推荐上一条 /2 下一条

正点原子公众号

QQ|手机版|OpenEdv-开源电子网 ( 粤ICP备12000418号-1 )

GMT+8, 2025-6-1 02:21

Powered by OpenEdv-开源电子网

© 2001-2030 OpenEdv-开源电子网

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