自学内容网 自学内容网

【STM32】无源蜂鸣器播放音乐《千与千寻》,HAL库

目录

一、工程链接

二、简单介绍

主要特点:

应用:

驱动电路:

三、原理图

四、cubeMX配置

时钟配置

五、keil配置

六、驱动编写

演奏函数

主函数编写

七、效果展示

八、驱动附录

music.h

music.c


一、工程链接

STM32无源蜂鸣器播放音乐《千与千寻》资源-CSDN文库

二、简单介绍

        无源蜂鸣器(Passive Buzzer)是一种没有内建振荡器的蜂鸣器。它通常需要外部的信号来驱动发声。在工作时,无源蜂鸣器会根据输入的脉冲信号频率和波形来产生音频信号。简单来说,它通过震动产生声音,其声音的频率取决于输入信号的频率。

主要特点:

  1. 驱动方式:无源蜂鸣器需要一个方波信号或音频信号来驱动,通常由微控制器(如STM32)或其他信号源提供。
  2. 音调调节:音调是通过改变输入信号的频率来控制的,频率越高,声音越尖锐;频率越低,声音越低沉。
  3. 节能:无源蜂鸣器比有源蜂鸣器更节能,因为它没有内部的震荡电路,工作时消耗的功率较低。
  4. 灵活性:可以通过改变驱动信号的频率和占空比来调整音调和响度,适应不同的应用需求。

应用:

  • 在各种警告和提醒系统中,如电池电量低、设备故障提示等。
  • 用于电子设备中发出提示音,例如遥控器、计算器等。
  • 通过合成音调来播放简单的音乐或警报声。

驱动电路:

  • PWM(脉宽调制)驱动:微控制器生成一定频率的PWM信号,通过滤波器将其转化为模拟信号来驱动无源蜂鸣器。
  • 时钟信号驱动:无源蜂鸣器也可以直接用一个方波信号驱动,方波的频率决定声音的频率。

与有源蜂鸣器不同,使用无源蜂鸣器时,需要编程控制输出信号的频率和波形,因此提供了更大的灵活性来生成不同的音调和音效。

三、原理图

这里笔者采用的是自己绘制的STM32F407VET6开发板,无源蜂鸣器电路部分如下图所示:

BEEP引脚连接至MCU的PB4引脚

四、cubeMX配置

时钟配置

开启定时器9作为1ms定时器

定时器3的通道1作为PWM输出,分频系数设置为84-1,因为定时器3是挂载在APB1总线上的

开启晶振,调试口等常规项的配置就不赘述了

五、keil配置

这里笔者使用了自己写的keil脚本,添加music.c和music.h文件即可

六、驱动编写

笔者作为一个音乐小白,最基本的理解是:想用无源蜂鸣器来演奏音乐,需要控制声音的频率,持续时间。方便起见,这里参考了midi音乐的协议规范。

typedef struct
{
uint8_t onoff; /* on or off*/
uint8_t freq;  /* frequence */
uint16_t tick; /* time */
}midiType;

定义三个参数:开启和关闭音符,频率,时间

配置PWM波的频率来改变无源蜂鸣器的声调高低,改变PWM波的持续时间来改变每一个音符的持续时间。

演奏函数

void play(midiType* mid)
{

   cnt++;

   if (cnt >= mid[noteIdx].tick)
   {
      switch (mid[noteIdx].onoff)
      {
         case 0x80:
         {
            HAL_TIM_PWM_Stop(&htim3, TIM_CHANNEL_1);
            break;
         }
         case 0x90:
         {
            __HAL_TIM_SET_AUTORELOAD(&htim3, 1000000u / midiFreq_table[mid[noteIdx].freq]);
            __HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_1, 500000u / midiFreq_table[mid[noteIdx].freq]);
            HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_1);
            break;
         }
         default:
            break;
      }
      noteIdx++;
      cnt = 0;
   }
}

主函数编写

在1ms的中断里反复地调用这个play函数,就可以完成音乐的播放了

/**
  * @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_TIM3_Init();
  MX_SPI2_Init();
  MX_TIM9_Init();
  /* USER CODE BEGIN 2 */
HAL_TIM_Base_Start_IT(&htim3);
HAL_TIM_Base_Start_IT(&htim9);

  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* 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};

  /** Configure the main internal regulator output voltage
  */
  __HAL_RCC_PWR_CLK_ENABLE();
  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);

  /** 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.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  RCC_OscInitStruct.PLL.PLLM = 4;
  RCC_OscInitStruct.PLL.PLLN = 168;
  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  RCC_OscInitStruct.PLL.PLLQ = 4;
  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_DIV4;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
  {
    Error_Handler();
  }
}

/* USER CODE BEGIN 4 */

void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
  static unsigned int cnt_1ms = 0;
  static unsigned char cnt = 0;

  if (htim == &htim3)
  {
    
  }

  if (htim == &htim9)
  {
    cnt_1ms++;

    if (cnt_1ms >= 100)
    {
      cnt_1ms = 0;
      cnt = cnt % 8;
      
      cnt++;
    }
    play(Spirited_Away);
  }
}

/* USER CODE END 4 */

七、效果展示

Spirited_Away

八、驱动附录

music.h

#ifndef MUSIC_H
#define MUSIC_H


#include "main.h"
#include "tim.h"
#include "stdint.h"

typedef struct
{
uint8_t onoff; /* on or off*/
uint8_t freq;  /* frequence */
uint16_t tick; /* time */
}midiType;

extern const midiType Spirited_Away[];

void play(midiType* mid);

#endif

music.c

#include "music.h"

uint16_t midiFreq_table[] = 
{
   8,
   9,
   9,
   10,
   10,
   11,
   12,
   12,
   13,
   14,
   15,
   15,
   16,
   17,
   18,
   19,
   21,
   22,
   23,
   24,
   26,
   27,
   29,
   31,
   33,
   35,
   37,
   39,
   41,
   44,
   46,
   49,
   52,
   55,
   58,
   62,
   65,
   69,
   74,
   78,
   82,
   87,
   92,
   98,
   104,
   110,
   116,
   123,
   130,
   138,
   146,
   155,
   164,
   174,
   184,
   195,
   207,
   220,
   233,
   247,
   261,
   277,
   293,
   311,
   329,
   349,
   370,
   392,
   415,
   440,
   466,
   493,
   523,
   554,
   587,
   622,
   659,
   698,
   740,
   784,
   830,
   880,
   932,
   987,
   1047,
   1109,
   1175,
   1245,
   1319,
   1397,
   1480,
   1568,
   1661,
   1760,
   1865,
   1975,
   2093,
   2217,
   2349,
   2489,
   2637,
   2794,
   2960,
   3136,
   3322,
   3520,
   3729,
   3951,
   4186,
   4435,
   4698,
   4978,
   5274,
   5588,
   5920,
   6272,
   6645,
   7040,
   7458,
   7902,
   8372,
   8870,
   9397,
   9956,
   10548,
   11176,
   11840,
   12544
};


const midiType Spirited_Away[] = 
{
0x90,0x59,0x14A0,
0x80,0x59,0x00D8,
0x90,0x5B,0x0018,
0x80,0x5B,0x00D8,
0x90,0x5D,0x0018,
0x80,0x5D,0x00D8,
0x90,0x59,0x0018,
0x80,0x59,0x00D8,
0x90,0x60,0x0018,
0x80,0x60,0x02B8,
0x90,0x5D,0x0018,
0x80,0x5D,0x00D8,
0x90,0x5B,0x0018,
0x80,0x5B,0x01B0,
0x90,0x60,0x0030,
0x80,0x60,0x01B0,
0x90,0x5B,0x0030,
0x80,0x5B,0x01B0,
0x90,0x59,0x0030,
0x80,0x59,0x00D8,
0x90,0x56,0x0018,
0x80,0x56,0x00D8,
0x90,0x5D,0x0018,
0x80,0x5D,0x02B8,
0x90,0x59,0x0018,
0x80,0x59,0x00D8,
0x90,0x58,0x0018,
0x80,0x58,0x0360,
0x90,0x58,0x0060,
0x80,0x58,0x01B0,
0x90,0x56,0x0030,
0x80,0x56,0x01B0,
0x90,0x58,0x0030,
0x80,0x58,0x01B0,
0x90,0x59,0x0030,
0x80,0x59,0x00D8,
0x90,0x5B,0x0018,
0x80,0x5B,0x00D8,
0x90,0x54,0x0018,
0x80,0x54,0x01B0,
0x90,0x59,0x0030,
0x80,0x59,0x01B0,
0x90,0x5B,0x0030,
0x80,0x5B,0x00D8,
0x90,0x5D,0x0018,
0x80,0x5D,0x00D8,
0x90,0x5E,0x0018,
0x80,0x5E,0x01B0,
0x90,0x5E,0x0030,
0x80,0x5E,0x00D8,
0x90,0x5D,0x0018,
0x80,0x5D,0x00D8,
0x90,0x5B,0x0018,
0x80,0x5B,0x00D8,
0x90,0x59,0x0018,
0x80,0x59,0x00D8,
0x90,0x5B,0x0018,
0x80,0x5B,0x0360,
0x90,0x59,0x0060,
0x80,0x59,0x00D8,
0x90,0x5B,0x0018,
0x80,0x5B,0x00D8,
0x90,0x5D,0x0018,
0x80,0x5D,0x00D8,
0x90,0x59,0x0018,
0x80,0x59,0x00D8,
0x90,0x60,0x0018,
0x80,0x60,0x02B8,
0x90,0x5D,0x0018,
0x80,0x5D,0x00D8,
0x90,0x5B,0x0018,
0x80,0x5B,0x01B0,
0x90,0x60,0x0030,
0x80,0x60,0x01B0,
0x90,0x5B,0x0030,
0x80,0x5B,0x01B0,
0x90,0x59,0x0030,
0x80,0x59,0x00D8,
0x90,0x56,0x0018,
0x80,0x56,0x00D8,
0x90,0x56,0x0018,
0x80,0x56,0x01B0,
0x90,0x58,0x0030,
0x80,0x58,0x00D8,
0x90,0x59,0x0018,
0x80,0x59,0x00D8,
0x90,0x54,0x0018,
0x80,0x54,0x0360,
0x90,0x54,0x0060,
0x80,0x54,0x01B0,
0x90,0x56,0x0030,
0x80,0x56,0x01B0,
0x90,0x58,0x0030,
0x80,0x58,0x01B0,
0x90,0x59,0x0030,
0x80,0x59,0x00D8,
0x90,0x5B,0x0018,
0x80,0x5B,0x00D8,
0x90,0x54,0x0018,
0x80,0x54,0x01B0,
0x90,0x59,0x0030,
0x80,0x59,0x01B0,
0x90,0x5B,0x0030,
0x80,0x5B,0x00D8,
0x90,0x5D,0x0018,
0x80,0x5D,0x00D8,
0x90,0x5E,0x0018,
0x80,0x5E,0x01B0,
0x90,0x5E,0x0030,
0x80,0x5E,0x00D8,
0x90,0x5D,0x0018,
0x80,0x5D,0x00D8,
0x90,0x5B,0x0018,
0x80,0x5B,0x00D8,
0x90,0x59,0x0018,
0x80,0x59,0x00D8,
0x90,0x59,0x0018,
0x80,0x59,0x0750,
0x90,0x5D,0x0210,
0x80,0x5D,0x00D8,
0x90,0x5E,0x0018,
0x80,0x5E,0x00D8,
0x90,0x60,0x0018,
0x80,0x60,0x01B0,
0x90,0x60,0x0030,
0x80,0x60,0x01B0,
0x90,0x60,0x0030,
0x80,0x60,0x01B0,
0x90,0x60,0x0030,
0x80,0x60,0x01B0,
0x90,0x60,0x0030,
0x80,0x60,0x00D8,
0x90,0x62,0x0018,
0x80,0x62,0x00D8,
0x90,0x60,0x0018,
0x80,0x60,0x00D8,
0x90,0x5E,0x0018,
0x80,0x5E,0x00D8,
0x90,0x5D,0x0018,
0x80,0x5D,0x01B0,
0x90,0x5D,0x0030,
0x80,0x5D,0x01B0,
0x90,0x5D,0x0030,
0x80,0x5D,0x01B0,
0x90,0x5D,0x0030,
0x80,0x5D,0x01B0,
0x90,0x5D,0x0030,
0x80,0x5D,0x00D8,
0x90,0x5E,0x0018,
0x80,0x5E,0x00D8,
0x90,0x5D,0x0018,
0x80,0x5D,0x00D8,
0x90,0x5B,0x0018,
0x80,0x5B,0x00D8,
0x90,0x59,0x0018,
0x80,0x59,0x01B0,
0x90,0x59,0x0030,
0x80,0x59,0x01B0,
0x90,0x59,0x0030,
0x80,0x59,0x00D8,
0x90,0x58,0x0018,
0x80,0x58,0x00D8,
0x90,0x56,0x0018,
0x80,0x56,0x01B0,
0x90,0x58,0x0030,
0x80,0x58,0x01B0,
0x90,0x58,0x0030,
0x80,0x58,0x00D8,
0x90,0x59,0x0018,
0x80,0x59,0x00D8,
0x90,0x5B,0x0018,
0x80,0x5B,0x01B0,
0x90,0x5B,0x0030,
0x80,0x5B,0x00D8,
0x90,0x5D,0x0018,
0x80,0x5D,0x00D8,
0x90,0x5B,0x0018,
0x80,0x5B,0x00D8,
0x90,0x5D,0x0018,
0x80,0x5D,0x00D8,
0x90,0x5B,0x0018,
0x80,0x5B,0x0360,
0x90,0x5D,0x0060,
0x80,0x5D,0x00D8,
0x90,0x5E,0x0018,
0x80,0x5E,0x00D8,
0x90,0x60,0x0018,
0x80,0x60,0x01B0,
0x90,0x60,0x0030,
0x80,0x60,0x01B0,
0x90,0x60,0x0030,
0x80,0x60,0x01B0,
0x90,0x60,0x0030,
0x80,0x60,0x01B0,
0x90,0x60,0x0030,
0x80,0x60,0x00D8,
0x90,0x62,0x0018,
0x80,0x62,0x00D8,
0x90,0x60,0x0018,
0x80,0x60,0x00D8,
0x90,0x5E,0x0018,
0x80,0x5E,0x00D8,
0x90,0x5D,0x0018,
0x80,0x5D,0x01B0,
0x90,0x5D,0x0030,
0x80,0x5D,0x01B0,
0x90,0x5D,0x0030,
0x80,0x5D,0x01B0,
0x90,0x5D,0x0030,
0x80,0x5D,0x00D8,
0x90,0x5E,0x0018,
0x80,0x5E,0x00D8,
0x90,0x5D,0x0018,
0x80,0x5D,0x00D8,
0x90,0x5B,0x0018,
0x80,0x5B,0x00D8,
0x90,0x59,0x0018,
0x80,0x59,0x00D8,
0x90,0x58,0x0018,
0x80,0x58,0x00D8,
0x90,0x56,0x0018,
0x80,0x56,0x01B0,
0x90,0x56,0x0030,
0x80,0x56,0x00D8,
0x90,0x58,0x0018,
0x80,0x58,0x00D8,
0x90,0x59,0x0018,
0x80,0x59,0x00D8,
0x90,0x5B,0x0018,
0x80,0x5B,0x00D8,
0x90,0x54,0x0018,
0x80,0x54,0x01B0,
0x90,0x59,0x0030,
0x80,0x59,0x01B0,
0x90,0x5B,0x0030,
0x80,0x5B,0x00D8,
0x90,0x5D,0x0018,
0x80,0x5D,0x00D8,
0x90,0x5B,0x0018,
0x80,0x5B,0x0288,
0x90,0x5B,0x0048,
0x80,0x5B,0x00D8,
0x90,0x5B,0x0018,
0x80,0x5B,0x00D8,
0x90,0x59,0x0018,
0x80,0x59,0x00D8,
0x90,0x59,0x0018,
0x80,0x59,0x0360,
0x90,0x59,0x0BA0,
0x80,0x59,0x00D8,
0x90,0x5B,0x0018,
0x80,0x5B,0x00D8,
0x90,0x5D,0x0018,
0x80,0x5D,0x00D8,
0x90,0x59,0x0018,
0x80,0x59,0x00D8,
0x90,0x60,0x0018,
0x80,0x60,0x02B8,
0x90,0x5D,0x0018,
0x80,0x5D,0x00D8,
0x90,0x5B,0x0018,
0x80,0x5B,0x01B0,
0x90,0x60,0x0030,
0x80,0x60,0x01B0,
0x90,0x5B,0x0030,
0x80,0x5B,0x01B0,
0x90,0x59,0x0030,
0x80,0x59,0x00D8,
0x90,0x56,0x0018,
0x80,0x56,0x00D8,
0x90,0x5D,0x0018,
0x80,0x5D,0x02B8,
0x90,0x59,0x0018,
0x80,0x59,0x00D8,
0x90,0x58,0x0018,
0x80,0x58,0x0360,
0x90,0x59,0x0060,
0x80,0x59,0x00D8,
0x90,0x58,0x0018,
0x80,0x58,0x00D8,
0x90,0x56,0x0018,
0x80,0x56,0x01B0,
0x90,0x58,0x0030,
0x80,0x58,0x01B0,
0x90,0x59,0x0030,
0x80,0x59,0x00D8,
0x90,0x5B,0x0018,
0x80,0x5B,0x00D8,
0x90,0x54,0x0018,
0x80,0x54,0x01B0,
0x90,0x59,0x0030,
0x80,0x59,0x01B0,
0x90,0x5B,0x0030,
0x80,0x5B,0x00D8,
0x90,0x5D,0x0018,
0x80,0x5D,0x00D8,
0x90,0x5E,0x0018,
0x80,0x5E,0x01B0,
0x90,0x5E,0x0030,
0x80,0x5E,0x00D8,
0x90,0x5D,0x0018,
0x80,0x5D,0x00D8,
0x90,0x5B,0x0018,
0x80,0x5B,0x00D8,
0x90,0x59,0x0018,
0x80,0x59,0x00D8,
0x90,0x5B,0x0018,
0x80,0x5B,0x0360,
0x90,0x59,0x0060,
0x80,0x59,0x00D8,
0x90,0x5B,0x0018,
0x80,0x5B,0x00D8,
0x90,0x5D,0x0018,
0x80,0x5D,0x00D8,
0x90,0x59,0x0018,
0x80,0x59,0x00D8,
0x90,0x60,0x0018,
0x80,0x60,0x02B8,
0x90,0x5D,0x0018,
0x80,0x5D,0x00D8,
0x90,0x5B,0x0018,
0x80,0x5B,0x01B0,
0x90,0x60,0x0030,
0x80,0x60,0x01B0,
0x90,0x5B,0x0030,
0x80,0x5B,0x00D8,
0x90,0x5B,0x0018,
0x80,0x5B,0x00D8,
0x90,0x59,0x0018,
0x80,0x59,0x00D8,
0x90,0x56,0x0018,
0x80,0x56,0x00D8,
0x90,0x56,0x0018,
0x80,0x56,0x01B0,
0x90,0x58,0x0030,
0x80,0x58,0x00D8,
0x90,0x59,0x0018,
0x80,0x59,0x00D8,
0x90,0x54,0x0018,
0x80,0x54,0x0360,
0x90,0x54,0x0060,
0x80,0x54,0x01B0,
0x90,0x56,0x0030,
0x80,0x56,0x01B0,
0x90,0x58,0x0030,
0x80,0x58,0x01B0,
0x90,0x59,0x0030,
0x80,0x59,0x00D8,
0x90,0x5B,0x0018,
0x80,0x5B,0x00D8,
0x90,0x54,0x0018,
0x80,0x54,0x01B0,
0x90,0x59,0x0030,
0x80,0x59,0x01B0,
0x90,0x5B,0x0030,
0x80,0x5B,0x00D8,
0x90,0x5D,0x0018,
0x80,0x5D,0x00D8,
0x90,0x5E,0x0018,
0x80,0x5E,0x01B0,
0x90,0x5E,0x0030,
0x80,0x5E,0x00D8,
0x90,0x5D,0x0018,
0x80,0x5D,0x00D8,
0x90,0x5B,0x0018,
0x80,0x5B,0x00D8,
0x90,0x59,0x0018,
0x80,0x59,0x00D8,
0x90,0x59,0x0018,
0x80,0x59,0x0360,
0x90,0x69,0x0BA0,
0x80,0x69,0x00D8,
0x90,0x6A,0x0018,
0x80,0x6A,0x00D8,
0x90,0x6C,0x0018,
0x80,0x6C,0x01B0,
0x90,0x6C,0x0030,
0x80,0x6C,0x01B0,
0x90,0x6C,0x0030,
0x80,0x6C,0x01B0,
0x90,0x6C,0x0030,
0x80,0x6C,0x01B0,
0x90,0x6C,0x0030,
0x80,0x6C,0x00D8,
0x90,0x6E,0x0018,
0x80,0x6E,0x00D8,
0x90,0x6C,0x0018,
0x80,0x6C,0x00D8,
0x90,0x6A,0x0018,
0x80,0x6A,0x00D8,
0x90,0x69,0x0018,
0x80,0x69,0x01B0,
0x90,0x69,0x0030,
0x80,0x69,0x01B0,
0x90,0x69,0x0030,
0x80,0x69,0x01B0,
0x90,0x69,0x0030,
0x80,0x69,0x01B0,
0x90,0x69,0x0030,
0x80,0x69,0x00D8,
0x90,0x6A,0x0018,
0x80,0x6A,0x00D8,
0x90,0x69,0x0018,
0x80,0x69,0x00D8,
0x90,0x67,0x0018,
0x80,0x67,0x00D8,
0x90,0x65,0x0018,
0x80,0x65,0x01B0,
0x90,0x65,0x0030,
0x80,0x65,0x01B0,
0x90,0x65,0x0030,
0x80,0x65,0x00D8,
0x90,0x64,0x0018,
0x80,0x64,0x00D8,
0x90,0x62,0x0018,
0x80,0x62,0x01B0,
0x90,0x64,0x0030,
0x80,0x64,0x01B0,
0x90,0x64,0x0030,
0x80,0x64,0x00D8,
0x90,0x65,0x0018,
0x80,0x65,0x00D8,
0x90,0x67,0x0018,
0x80,0x67,0x01B0,
0x90,0x67,0x0030,
0x80,0x67,0x00D8,
0x90,0x69,0x0018,
0x80,0x69,0x00D8,
0x90,0x67,0x0018,
0x80,0x67,0x00D8,
0x90,0x69,0x0018,
0x80,0x69,0x00D8,
0x90,0x67,0x0018,
0x80,0x67,0x0360,
0x90,0x69,0x0060,
0x80,0x69,0x00D8,
0x90,0x6A,0x0018,
0x80,0x6A,0x00D8,
0x90,0x6C,0x0018,
0x80,0x6C,0x01B0,
0x90,0x6C,0x0030,
0x80,0x6C,0x01B0,
0x90,0x6C,0x0030,
0x80,0x6C,0x01B0,
0x90,0x6C,0x0030,
0x80,0x6C,0x01B0,
0x90,0x6C,0x0030,
0x80,0x6C,0x00D8,
0x90,0x6E,0x0018,
0x80,0x6E,0x00D8,
0x90,0x6C,0x0018,
0x80,0x6C,0x00D8,
0x90,0x6A,0x0018,
0x80,0x6A,0x00D8,
0x90,0x69,0x0018,
0x80,0x69,0x01B0,
0x90,0x69,0x0030,
0x80,0x69,0x01B0,
0x90,0x69,0x0030,
0x80,0x69,0x01B0,
0x90,0x69,0x0030,
0x80,0x69,0x00D8,
0x90,0x6A,0x0018,
0x80,0x6A,0x00D8,
0x90,0x69,0x0018,
0x80,0x69,0x00D8,
0x90,0x67,0x0018,
0x80,0x67,0x00D8,
0x90,0x65,0x0018,
0x80,0x65,0x00D8,
0x90,0x64,0x0018,
0x80,0x64,0x00D8,
0x90,0x62,0x01F8,
0x80,0x62,0x00D8,
0x90,0x64,0x0018,
0x80,0x64,0x00D8,
0x90,0x65,0x0018,
0x80,0x65,0x00D8,
0x90,0x67,0x0018,
0x80,0x67,0x00D8,
0x90,0x60,0x0018,
0x80,0x60,0x01B0,
0x90,0x65,0x0030,
0x80,0x65,0x01B0,
0x90,0x67,0x0030,
0x80,0x67,0x00D8,
0x90,0x69,0x0018,
0x80,0x69,0x00D8,
0x90,0x67,0x0018,
0x80,0x67,0x0360,
0x90,0x67,0x0060,
0x80,0x67,0x00D8,
0x90,0x65,0x0018,
0x80,0x65,0x00D8,
0x90,0x65,0x0018,
0x80,0x59,0x0510,
0x90,0x59,0x0090,
0x80,0x65,0x0360,

};


static int16_t cnt = 0;
static uint16_t noteIdx = 1;

void play(midiType* mid)
{

   cnt++;

   if (cnt >= mid[noteIdx].tick)
   {
      switch (mid[noteIdx].onoff)
      {
         case 0x80:
         {
            HAL_TIM_PWM_Stop(&htim3, TIM_CHANNEL_1);
            break;
         }
         case 0x90:
         {
            __HAL_TIM_SET_AUTORELOAD(&htim3, 1000000u / midiFreq_table[mid[noteIdx].freq]);
            __HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_1, 500000u / midiFreq_table[mid[noteIdx].freq]);
            HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_1);
            break;
         }
         default:
            break;
      }
      noteIdx++;
      cnt = 0;
   }
}

原文地址:https://blog.csdn.net/weixin_56719449/article/details/145043586

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!