使能IO口时钟。调用函数RCC_APB2PeriphColckCmd(); 不同的IO组,调用的时钟使能函数不一样。 初始化IO口模式。调用函数BEEP_Init(); 操作IO口,输出高低电平。 beep.h - #ifndef __BEEP_H
- #define __BEEP_H
- #include "sys.h"
- #define BEEP PBout(8)
- void BEEP_Init(void);
- #endif
复制代码
beep.c - #include "beep.h"
- #include "stm32f10x.h"
- void BEEP_Init()
- {
-
- GPIO_InitTypeDef GPIO_InitStructure;
-
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOE,ENABLE);//使能GPIOA和GPIOB
- //| 既是逻辑运算符也是位运算符;|| 只是逻辑运算符
- //| 不具有短路效果,即左边true,右边还会执行;
- GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
- GPIO_InitStructure.GPIO_Pin=GPIO_Pin_8;
- GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
- GPIO_Init(GPIOB,&GPIO_InitStructure); //根据参数初始化CPIOB.8
- GPIO_SetBits(GPIOB,GPIO_Pin_8); //输出0,关闭蜂鸣器
- }
复制代码
main.c - #include "beep.h"
- #include "delay.h"
- int main(void)
- {
- delay_init();
- BEEP_Init();
-
- while(1)
- {
- BEEP=1;
- delay_ms(500);
- BEEP=0;
- delay_ms(500);
- }
- }
复制代码
|