一、硬件设计
本项目用到的是一个无源蜂鸣器的模块,接到了STM32的PB7引脚,对应的是定时器4的通道2---TIM4_CH2。
蜂鸣器利用PWM输出播放音乐的相关原理,可以参考以下文章STM32CubeMx使用教程(五)—— 使用PWM控制蜂鸣器演唱孤勇者_stm32控制蜂鸣器唱歌-CSDN博客,在软件部分我将贴出相关的公式,以及具体代码的实现。
二、软件设计
我们通过调整来PSC的值,来修改蜂鸣器输出的频率,从而输出不同的音符。来看一个简单的例子:
根据周期输出的公式:Tout= ((arr+1)*(psc+1))/Tclk, 频率为 F=1/Tout。
如果我们要输出一个低音DO(262hz),Tclk对应的是我们单片机的时钟总线的频率,我们的ARR值配置为1000,那么PSC就该为(72000/262),此时的Tout约为0.0038,而频率=1/周期,所以此时蜂鸣器输出的频率就约为262Hz。接下来看下面Beep.h中这些宏定义就能够明白它们的由来了。
#ifndef __BEEP_H
#define __BEEP_H
#include "main.h"
#define proport 72000 //Tclk/(arr+1)=72000000/(1000)
//根据Tout= ((arr+1)*(psc+1))/Tclk推出psc值就是本句define定义的值,Tout为音调频率131Hz的倒数,Tclk=72MHz
#define L1 ((proport/262)-1)//低调 do 的频率
#define L2 ((proport/296)-1)//低调 re 的频率
#define L3 ((proport/330)-1)//低调 mi 的频率
#define L4 ((proport/349)-1)//低调 fa 的频率
#define L5 ((proport/392)-1)//低调 sol 的频率
#define L6 ((proport/440)-1)//低调 la 的频率
#define L7 ((proport/494)-1)//低调 si 的频率
#define M1 ((proport/523)-1)//中调 do 的频率
#define M2 ((proport/587)-1)//中调 re 的频率
#define M3 ((proport/659)-1)//中调 mi 的频率
#define M4 ((proport/699)-1)//中调 fa 的频率
#define M5 ((proport/784)-1)//中调 sol的频率
#define M6 ((proport/880)-1)//中调 la 的频率
#define M7 ((proport/988)-1)//中调 si 的频率
#define H1 ((proport/1048)-1)//高调 do 的频率
#define H2 ((proport/1176)-1)//高调 re 的频率
#define H3 ((proport/1320)-1)//高调 mi 的频率
#define H4 ((proport/1480)-1)//高调 fa 的频率
#define H5 ((proport/1640)-1)//高调 sol的频率
#define H6 ((proport/1760)-1)//高调 la 的频率
#define H7 ((proport/1976)-1)//高调 si 的频率
#define Z0 0//
void Solitary_brave(void);
#endif
beep.c中的Buzzer_on(uint16_t Frequency, uint16_t Duty):
参数1:Frequency是PSC的值,用于修改蜂鸣器输出的频率,来输出对应的音符。
参数2:Duty是用于改变占空比,修改播放的音量。
void Solitary_brave(void) :
用于播放孤勇者,延时是用于改变音乐的节拍。
#include "beep.h"
extern TIM_HandleTypeDef htim4;
void buzzer_on(uint16_t Frequency, uint16_t Duty)
{
__HAL_TIM_PRESCALER(&htim4, Frequency);
__HAL_TIM_SetCompare(&htim4, TIM_CHANNEL_2, Duty);
}
void buzzer_off(void)
{
__HAL_TIM_SetCompare(&htim4, TIM_CHANNEL_2, 0);
}
/**
* NAME: void Solitary_brave(void)
* FUNCTION : Solitary_brave
*/
void Solitary_brave(void)
{
int16_t solitary_brave[]=
{
M6,50,M7,50,H1,50,H2,50,M7,50,H1,50,H1,100,Z0,10, //爱你孤身走暗巷
H1,50,M7,50,H1,50,H2,50,M7,50,H1,50,H1,100,Z0,10, //爱你不跪的模样
H1,50,H2,50,H3,50,H2,50,H3,50,H2,50,H3,100,H3,50,H3,50,H2,50,H3,100,H5,100,H3,100,Z0,10 //爱你对峙过绝望不肯哭一场
};
int length = sizeof(solitary_brave)/sizeof(solitary_brave[0]);
for(uint8_t i=0;i<(length/2);i++)
{
buzzer_on(solitary_brave[i*2],200);
HAL_Delay(5*solitary_brave[i*2+1]);
}
}
main函数中的代码。
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2022 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "tim.h"
#include "gpio.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "bsp_buzzer.h"
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_TIM4_Init();
/* USER CODE BEGIN 2 */
HAL_TIM_PWM_Start(&htim4,TIM_CHANNEL_1);
HAL_Delay(1000);
Solitary_brave();
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
// Wind_Rises();
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
{
Error_Handler();
}
}
/* USER CODE BEGIN 4 */
/* USER CODE END 4 */
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
__disable_irq();
while (1)
{
}
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
三、实验效果
STM32 蜂鸣器播放孤勇者
结尾:
对这个项目感兴趣的小伙伴,完全可以根据本项目的文章教程,一步步自己搭建属于自己的STM32-FreeRTOS智能家居项目,如果需要完整资料和完整硬件的可私信我(有偿):原理图、STM32源码、Android app源码、操作教程。