低平蜂鸣器,低电平发声,高电平不发声,
三个排针,VCC接3.3v,GND接地,I/O接A0口,如图:

- #include "stm32f10x.h"
- #include "Delay.h" //delay函数所在头文件
-
- int main(void)
- {
- GPIO_InitTypeDef GPIOInitStruct;
-
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); //设置时钟
-
- GPIOInitStruct.GPIO_Pin = GPIO_Pin_0; //A0
- GPIOInitStruct.GPIO_Mode = GPIO_Mode_Out_PP; //推挽模式
- GPIOInitStruct.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOA, &GPIOInitStruct);
-
- while(1)
- {
- GPIO_SetBits(GPIOA, GPIO_Pin_0); // 不响
- Delay_ms(500); //延时500ms
- GPIO_ResetBits(GPIOA, GPIO_Pin_0); // 响
- Delay_ms(1000); //延时1000ms
- }
- }