This review will give you a test of the use of the buzzer. When testing the buzzer, I also made some very basic mistakes, which caused me to be stuck for a long time before I found the problem and solved it.
First of all, there are two types of buzzers, active buzzers and passive buzzers as shown below;
Active buzzer
The buzzer module is generally composed of a pull-up resistor, a transistor responsible for amplification and a buzzer. The buzzer module used here is powered by 5V. Make sure that the current passing through the buzzer is large enough, otherwise the buzzer cannot be driven.
The main difference between an active buzzer and a passive buzzer is whether there is an oscillation circuit in the buzzer. The sound of a buzzer is generated by the constant change of frequency, which causes the vibration plate to vibrate continuously to produce sound. An active buzzer has an oscillation source inside, so it will sound as soon as it is powered on; while a passive buzzer does not have an oscillation source inside, so it cannot be made to sound if a DC signal is used. It must be driven by a 2K-5K square wave. Therefore, using a passive buzzer will be more complicated when writing code, and you need to write a frequency code yourself.
First we use an active buzzer for demonstration.
First is the configuration of cube. Here we use PA9 port.
Select GPIO output level: High
GPIO mode: Output Push Pull
GPIO Pull-up/Pull-down: Pull-up
Maximum output speed: High
After configuration, you can generate the project file
#include "beep.h"
void BEEP_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOA_CLK_ENABLE();
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_9, GPIO_PIN_SET);
/*Configure GPIO pin : PD2 */
GPIO_InitStruct.Pin = GPIO_PIN_9;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
//BEEP(0); /* 关闭蜂鸣器 */
}
Write a test code in the main program:
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
BEEP_TOGGLE();
delay_ms(1000);
/* USER CODE BEGIN 3 */
/* USER CODE END 3 */
}
The output is inverted every second, and the buzzer output also sounds for one second and stops for one second.
When I was here, I confused the active and passive buzzers. I installed a passive buzzer at first, which made the sound "da-da-da". Because the board does not have an external crystal oscillator, and the clock tree of the L series has an additional MSI clock configuration compared to other models, I once thought it was a problem with the clock tree configuration. It took me a long time to find out that I had installed the wrong buzzer.
But here we also have a problem. The normal frequency band for the passive buzzer to emit sound should be as shown below:
The frequency exchange written in the experiment is too slow and does not meet the conditions for the buzzer to sound. We only need to speed up the frequency to make the passive buzzer sound.