有源蜂鸣器控制更为简单,类似LED;
无源蜂鸣器相对复杂一点,需要一定的脉冲;
改变无源蜂鸣器的音调,可以通过改变频率->周期来改变。
改变声音大小,通过调节占空比.
void Buzzer_Init()
{
/* enable gpiob clock */
RCC_APB2PeriphClockCmd(BUZZER_PORT_RCC, ENABLE);
/* init gpioc */
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = BUZZER_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
int main(void)
{
/* USARTx configured as follow:
- BaudRate = 115200 baud
- Word Length = 8 Bits
- One Stop Bit
- No parity
- Hardware flow control disabled (RTS and CTS signals)
- Receive and transmit enabled
*/
u16 nTimes;
SysTick_Init(72);
LED_Init();
Buzzer_Init();
/* Infinite loop */
while (1)
{
nTimes ++;
// if (nTimes % 10 == 0)
// {
// buzzer = !buzzer;
// }
//
// if (nTimes % 20000 == 0)
// {
// led_0 = !led_0;
// }
buzzer = 1;
delay_us(150);
buzzer = 0;
delay_us(50);
}
}