Simple library to generate pulses with predefined time and period, that can be used for control piezoelectronic buzzer with oscillator circuit – active, or without – passive. It useful for debug purposes or notifications in your device.
As you can see this repository use buzzer libary as submodule, so to get all files (including submodule), use following command:
git clone --recurse-submodules https://github.com/Egoruch/Buzzer-STM32-HAL.git
- Add buzzer.c to Src and buzzer.h to Inc.
- Configure required pin as GPIO_Output (left click) and set label (right click): BUZZER.
- Include buzzer.h in main.c:
/* USER CODE BEGIN Includes */
#include "buzzer.h"
/* USER CODE END Includes */
- Open stm32f4xx_it.c and modify SysTick_Handler():
/**
* @brief This function handles System tick timer.
*/
void SysTick_Handler(void)
{
/* USER CODE BEGIN SysTick_IRQn 0 */
HAL_SYSTICK_Callback();
/* USER CODE END SysTick_IRQn 0 */
HAL_IncTick();
/* USER CODE BEGIN SysTick_IRQn 1 */
/* USER CODE END SysTick_IRQn 1 */
}
- Write callback function to execute BUZZER_Handler() every 1 ms:
/* USER CODE BEGIN 4 */
/*** SYSTICK (T = 1ms) ***/
/**
* @brief SYSTICK callback.
* @retval None
*/
void HAL_SYSTICK_Callback(void)
{
BUZZER_Handler();
}
/* USER CODE END 4 */
- That's all, now you can test buzzing by Buzzer_Go!
/* USER CODE BEGIN 2 */
Buzzer_Go(TBUZ_100, TICK_2);
HAL_Delay(2000);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
Buzzer_Go(TBUZ_50, TICK_4);
HAL_Delay(2000);
/* USER CODE END WHILE */