本帖最后由 iiv 于 2023-9-3 23:19 编辑
<span style="color: #666666;font-size: 23px">介绍:无源蜂鸣器是一种常见的电子元件,它能发出简单的蜂鸣声。与有源蜂鸣器不同,无源蜂鸣器本身不需要外部电源供电,它利用振动器将电能转换为声能。无源蜂鸣器由一个压电陶瓷振荡器和一个共振腔组成。当通过直流电信号或方波信号输入到无源蜂鸣器时,振荡器内部的压电陶瓷片开始振动,产生声音。这个声音的频率由振荡器的物理特性决定,通常在几千赫兹到几万赫兹之间。无源蜂鸣器结构简单、价格便宜,并且使用方便。它被广泛应用在各种电子设备中,如家电、电子玩具、电子钟表等。常见的应用包括警报提示、提醒功能、按键声音等。需要注意的是,无源蜂鸣器只能产生固定频率的蜂鸣声,无法实现复杂的声音效果。如果需要产生不同频率或连续变化的声音,可能需要借助其他电路或芯片来实现。<br><br><br></span>
<span style="font-size: 20px"> 大家好,楼楼本次分享一下如果使用小安派驱动无源蜂鸣器播放MIDI音乐,它的原理是将乐谱对应的音调转换为蜂鸣器所需的频率和持续时间,然后通过单片机的 GPIO 引脚利用脉冲信号控制蜂鸣器的振动,产生音频频率和持续时间对应的声音。当脉冲信号的频率快时,蜂鸣器发出高音;频率慢时,发出低音。通过改变频率和持续时间,可以模拟不同的音符、音调和节奏。这种简单的音乐播放原理广泛应用于一些电子设备中,如闹钟、电子游戏和电子玩具等。虽然蜂鸣器音质较差,无法还原复杂的音乐,但在一些简单场景下,它是一种经济、简便的音频输出解决方案。</span>
<span style="font-size: 20px;color:#666666"> <br>我们用到的是低电平触发的无源蜂鸣器,就是长下面这个样子:<br><br></span>
<div style="text-align: center;">
<img src="http://bbs.ai-thinker.com/data/attachment/forum/202309/03/212304h248bz72pmtbsbmw.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/300" alt="p1.jpg" title="p1.jpg">
</div>
<span style="font-size: 20px"> <br><br><br><br>视频演示:</span>
<div style="text-align: center;">
<br><br>
<罗刹海市>
<video controls style="width: 650px; height: 400px;">
<source src="https://whxy.shop/tools/?data=lchs.mp4" type="video/mp4">
你的浏览器不支持播放改视频
</video>
<br><br><br>
<向云端>
<video controls style="width: 650px; height: 400px;">
<source src="https://whxy.shop/tools/?data=xyd.mp4" type="video/mp4">
你的浏览器不支持播放改视频
</video>
<br><br><br>
<赛马>
<video controls style="width: 650px; height: 400px;">
<source src="https://whxy.shop/tools/?data=saima.mp4" type="video/mp4">
你的浏览器不支持播放改视频
</video>
<br><br>
</div>
<span style="font-size: 20px"> 代码: </span>
<span style="font-size: 20px"> fmq.h文件,新建一个文件复制进去就可以啦! </span>
#ifndef __FMQ_H__
#define __FMQ_H__
#include "bflb_mtimer.h"
#include "bflb_pwm_v2.h"
#include "bflb_clock.h"
#include "bflb_gpio.h"
uint8_t CH,pwm_Ch[] = {0, 1, 2, 3}; // PWM通道
struct bflb_device_s *pwm;
struct bflb_device_s *gpio;
void fmq_play(int val,int num);
/**
* @brief 定义了音调:有3个八度,还有延时
*
*/
enum midi{ C0 = 262,D0 = 294, E0 = 330, F0 = 349, G0 = 392,
A0 = 440, B0 = 494,C = 523,D = 587,E = 659,F = 698,G = 784,
A = 880,B = 988,C1 = 1047,D1 = 1175,E1 = 1319,F1 = 1397,
G1 = 1568,A1 = 1760,B1 = 1976,N = 0,L1 = 4000,L10=6000,
L2 = 2000,L20 = 3000,L4 = 1000,L40 =1500,L8 = 500,
L80 = 750,L16 = 250,L160 = 375,L32 = 125,L320 = 188};
/*======================================================================================================================================*/
//沧海一声笑
int demo_0_midi[] = {A,L80,G,L16,E,L8,D,L8,C,L40,N,L8,E,L80,D,L16,C,L8,A0,L8,G0,L40,N,L8,G0,L80,A0,L16,G0,L80,A0,L16,C,L80,D,L16,E,L8,G,L8,A,L80,G,L16,E,L8,D,L16,C,L16,D,L40,N,L8,
A,L8,A,L16,G,L16,E,L8,D,L8,C,L40,N,L8,E,L80,D,L16,C,L8,A0,L8,G0,L40,N,L8,G0,L80,A0,L16,G0,L80,A0,L16,C,L80,D,L16,E,L8,G,L8,A,L80,G,L16,E,L16,D,L16,C,L8,D,L40,N,L8,
A,L8,A,L16,G,L16,E,L8,D,L8,C,L40,N,L8,E,L80,D,L16,C,L8,A0,L8,G0,L40,N,L8,G0,L80,A0,L16,G0,L80,A0,L16,C,L80,D,L16,E,L8,G,L8,A,L80,G,L16,E,L16,D,L16,C,L8,D,L40,N,L8,
A,L8,A,L16,G,L16,E,L8,D,L8,C,L40,N,L8,E,L80,D,L16,C,L8,A0,L8,G0,L40,N,L8,G0,L80,A0,L16,G0,L80,A0,L16,C,L80,D,L16,E,L8,G,L8,A,L80,G,L16,E,L8,D,L8,C,L40,N,L8};
//别知己
int demo_1_midi[] = {E,L80,A0,L16,A0,L8,C,L16,E,L16,D,L4,B0,L16,A0,L16,G0,L16,A0,L16,A0,L40,N,L8,A0,L16,A0,L16,A,L16,A,L8,A,L16,A,L8,A,L16,G,L16,G,L80,G,L16,E,L16,D,L16,D,L16,E,L16,E,L40,N,L8,E,L16,G,L16,
A,L80,A,L16,A,L8,A,L16,G,L16,G,L80,G,L16,E,L16,D,L16,D,L8,E,L80,G,L16,E,L8,D,L8,C,L4,N,L8,E,L16,G,L16,D,L16,D,L16,D,L16,D,L16,D,L8,E,L16,G,L16,D,L80,E,L16,B0,L16,A0,L16,G0,L16,A0,L16,A0,L40,N,L8,
E,L80,A0,L16,A0,L8,C,L16,E,L16,D,L80,D,L16,B0,L16,A0,L16,G0,L16,A0,L16,A0,L40,N,L8,A0,L16,A0,L16,A,L80,A,L16,A,L8,A,L16,G,L16,G,L80,G,L16,E,L8,D,L16,E,L16,E,L40,N,L8,E,L16,G,L16,
A,L80,A,L16,A,L8,A,L16,G,L16,G,L16,G,L8,G,L16,E,L8,D,L16,D,L16,E,L80,G,L16,E,L8,D,L8,C,L4,N,L8,E,L16,G,L16,D,L16,D,L16,D,L16,D,L16,D,L8,E,L16,G,L16,D,L80,E,L16,B0,L16,A0,L16,G0,L16,A0,L16,
A0,L40,N,L8,C,L16,D,L16,E,L16,G,L16,A,L40,A,L8,G,L80,G,L16,E,L8,D,L16,E,L16,E,L40,N,L8,C,L16,D,L16,E,L16,G,L16,A,L40,A,L8,B,L80,B,L16,C1,L8,B,L16,A,L16,A,L40,N,L8,E,L16,G,L16,
A,L80,A,L16,A,L8,A,L16,G,L16,G,L16,G,L8,G,L16,E,L8,D,L16,D,L16,E,L80,G,L16,E,L8,D,L8,C,L4,N,L8,E,L16,G,L16,D,L16,D,L16,D,L16,D,L16,D,L8,E,L16,G,L16,D,L80,E,L16,B0,L16,A0,L16,G0,L16,A0,L16,A0,L40};
//孤勇者
int demo_2_midi[] = {E,L40,N,L8,C,L16,D,L16,C,L16,E,L40,N,L8,C,L16,D,L16,C,L16,D,L16,E,L16,A0,L80,C,L16,A0,L80,C,L16,A0,L80,C,L16,D,L8,C,L8,
B0,L40,N,L8,E,L40,N,L8,C,L16,D,L16,C,L16,E,L40,N,L8,C,L16,D,L16,C,L16,D,L16,E,L16,A0,L80,C,L16,A0,L80,C,L16,A0,L80,C,L16,E,L8,D,L8,B0,L40,N,L8,
A0,L16,C,L16,A,L80,A,L16,A,L16,G,L16,A,L8,A,L16,G,L16,A,L16,G,L16,A,L16,G,L16,E,L16,E,L40,N,L8,A0,L16,C,L16,A,L80,A,L16,A,L16,G,L16,A,L16,G,L16,B,L80,B,L16,B,L16,A,L16,B,L80,
A,L8,E,L4,N,L8,E,L16,G,L16,E,L16,D,L80,E,L16,D,L80,E,L16,D,L80,E,L16,G,L16,E,L16,G,L16,E,L16,D,L80,E,L16,D,L80,E,L16,D,L4,N,L8,C,L16,D,L16,
E,L8,A0,L8,C,L8,E,L8,D,L8,E,L16,D,L16,C,L16,C,L8,A0,L40,N,L8,A,L16,B,L16,C1,L16,D1,L16,B,L16,C1,L16,C1,L8,C1,L16,B,L16,C1,L16,D1,L16,B,L16,C1,L16,C1,L8,C1,L16,D1,L16,
E1,L16,D1,L16,E1,L16,D1,L16,E1,L8,E1,L16,D1,L16,E1,L8,G1,L8,E1,L8,A,L16,B,L16,C1,L16,D1,L16,B,L16,C1,L16,C1,L8,C1,L16,B,L16,C1,L16,D1,L16,B,L16,C1,L16,C1,L8,C1,L16,D1,L16,
E1,L16,D1,L16,E1,L16,D1,L16,E1,L8,E1,L16,D1,L16,E1,L8,G1,L8,E1,L8,G1,L8,E1,L80,G1,L16,E1,L80,G1,L16,E1,L16,G1,L16,A1,L16,E1,L16,G1,L8,G1,L8,E1,L8,G1,L16,E1,L8,G1,L16,E1,L16,G1,L16,A1,L16,E1,L16,G1,L8,G1,L16,G1,L16,
E1,L16,D1,L16,D1,L8,D1,L8,C1,L16,E1,L8,D1,L16,D1,L8,D1,L8,C1,L16,C1,L16,A,L40,N,L8,G1,L16,G1,L16,E1,L16,D1,L16,D1,L8,D1,L8,C1,L16,E1,L8,D1,L16,D1,L8,D1,L8,C1,L16,C1,L16,A,L4};
//赛马
int demo_3_midi[] = {A,L40,E,L16,G,L16,A,L40,E,L16,G,L16,A,L40,E,L16,G,L16,A,L40,E,L16,G,L16,A,L16,G,L16,E,L16,G,L16,A,L16,G,L16,E,L16,G,L16,A,L16,G,L16,E,L16,G,L16,A,L16,G,L16,E,L16,G,L16,
A,L8,G,L16,A,L8,G,L8,A,L16,A,L8,G,L8,A,L16,A,L8,G,L8,A,L16,A,L8,A0,L8,E,L8,C,L8,A0,L8,E,L8,A,L8,G,L8,E,L8,D,L16,E,L16,D,L16,C,L16,D,L16,E,L16,D,L16,C,L16,D,L16,E,L16,D,L16,C,L16,D,L16,E,L16,D,L16,C,L16,
A0,L8,E,L8,C,L8,A0,L8,E,L8,A,L8,G,L8,E,L8,D,L16,E,L16,D,L16,C,L16,D,L16,E,L16,D,L16,C,L16,D,L16,E,L16,D,L16,C,L16,D,L16,E,L16,D,L16,C,L16,D,L40,A0,L16,C,L16,D,L40,A0,L16,C,L16,
D,L40,A0,L16,C,L16,D,L40,A0,L16,C,L16,D,L16,E,L16,D,L16,C,L16,D,L16,E,L16,D,L16,C,L16,D,L16,E,L16,D,L16,C,L16,D,L16,E,L16,D,L16,C,L16,D,L8,C,L16,D,L8,D,L8,C,L16,D,L8,D,L8,C,L16,D,L8,D,L8,C,L16,D,L8,
A0,L4,A,L4,G,L4,E,L4,D,L4,G,L4,E,L4,C,L4,A0,L4,A,L8,B,L8,G,L4,E,L4,D,L4,G,L4,E,L4,D,L4,A0,L4,C,L16,D,L16,A0,L40,C,L16,D,L16,
A0,L40,C,L16,D,L16,A0,L40,C,L16,D,L16,A0,L16,D,L16,C,L16,D,L16,A0,L16,D,L16,C,L16,D,L16,A0,L16,D,L16,C,L16,D,L16,A0,L16,D,L16,C,L16,D,L16,A0,L4,A0,L8,A0,L8,A0,L4,N,L8,
E,L4,A,L8,C1,L16,G,L40,E,L8,G,L8,A,L8,C1,L8,D1,L16,C1,L16,A,L40,N,L8,E,L4,A,L80,C1,L16,G,L4,G,L8,E,L8,D,L8,E,L8,A,L8,G,L8,E,L40,N,L8,
G,L4,A,L80,C1,L16,C,L40,A0,L8,D,L8,E,L8,A,L8,G,L8,E,L4,E,L8,D,L8,C,L80,D,L16,E,L8,G,L8,A,L4,A0,L4,D,L8,E,L8,C,L4,A0,L40,N,L8,
E,L8,E,L16,E,L16,A,L8,C1,L16,C1,L16,G,L8,G,L16,G,L16,G,L8,E,L16,E,L16,G,L8,G,L16,A,L16,C1,L8,D1,L16,C1,L16,A,L8,A,L16,A,L16,A,L8,A,L16,A,L16,E,L8,E,L16,E,L16,A,L8,C1,L16,C1,L16,
A,L8,A,L16,A,L16,A,L8,E,L16,E,L16,D,L16,D,L16,E,L8,A,L16,G,L16,G,L8,E,L16,E,L16,G,L8,E,L8,A0,L8,G,L8,G,L16,G,L16,A,L8,C1,L16,C1,L16,C,L8,C,L16,C,L16,C,L8,A0,L8,D,L8,D,L16,E,L16,A,L8,G,L16,G,L16,
E,L8,E,L16,G,L16,E,L8,D,L8,C,L16,A0,L16,C,L16,D,L16,E,L16,D,L16,E,L16,G,L16,A,L16,G,L16,A,L16,C1,L16,G,L16,A,L16,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L8,E1,L4,N,L8,A1,L4,A1,L4,A1,L4,A1,L4,
2218,L4,A1,L4,G1,L8,E1,L8,D1,L8,E1,L8,E1,L8,G1,L8,E1,L8,D1,L8,D1,L8,B,L8,A,L4,A,L4,E,L4,G,L4,A,L4,A,L4,D1,L8,B,L8,A,L8,G,L8,N,L8,E,L4,E,L4,G,L4,C,L4,A0,L4,
N,L8,E,L4,G,L4,E,L4,A0,L4,A0,L4,C,L4,A0,L4,N,L8,A,L4,A,L4,G,L4,E,L4,G,L8,E,L4,A,L4,A,L4,D1,L4,B,L4,A,L4,E,L4,A,L4,D,L4,G,L4,E,L4,N,L8,
D1,L4,D1,L4,D1,L4,B,L4,A,L4,G,L4,N,L8,E,L4,D1,L4,B,L4,A,L4,A,L4,G,L4,E,L4,N,L8,E,L8,G,L8,D,L4,D,L4,B0,L4,A0,L4,A0,L4,
};
/*======================================================================================================================================*/
/*======================================================================================================================================*/
// 沧海一声笑
void player_0(int t){
float T = (float)60/t*1000;
for (int j = (0); j < (sizeof(demo_0_midi)/sizeof(demo_0_midi[0])); j = j + 1) {
fmq_play(demo_0_midi[(int)(j)],(((float)demo_0_midi[(int)(j + 1)] / 1000) * T));
j++;
}
}
// 别知己
void player_1(int t){
float T = (float)60/t*1000;
for (int j = (0); j < (sizeof(demo_1_midi)/sizeof(demo_1_midi[0])); j = j + 1) {
fmq_play(demo_1_midi[(int)(j)],(((float)demo_1_midi[(int)(j + 1)] / 1000) * T));
j++;
}
}
// 孤勇者
void player_2(int t){
float T = (float)60/t*1000;
for (int j = (0); j < (sizeof(demo_2_midi)/sizeof(demo_2_midi[0])); j = j + 1) {
fmq_play(demo_2_midi[(int)(j)],(((float)demo_2_midi[(int)(j + 1)] / 1000) * T));
j++;
}
}
// 赛马
void player_3(int t){
float T = (float)60/t*1000;
for (int j = (0); j < (sizeof(demo_3_midi)/sizeof(demo_3_midi[0])); j = j + 1) {
fmq_play(demo_3_midi[(int)(j)],(((float)demo_3_midi[(int)(j + 1)] / 1000) * T));
j++;
}
}
// 早安隆回
void player_4(int t){
float T = (float)60/t*1000;
for (int j = (0); j < (sizeof(demo_4_midi)/sizeof(demo_4_midi[0])); j = j + 1) {
fmq_play(demo_4_midi[(int)(j)],(((float)demo_4_midi[(int)(j + 1)] / 1000) * T));
j++;
}
}
// 向云端 T:70
void player_5(int t){
float T = (float)60/t*1000;
for (int j = (0); j < (sizeof(demo_5_midi)/sizeof(demo_5_midi[0])); j = j + 1) {
fmq_play(demo_5_midi[(int)(j)],(((float)demo_5_midi[(int)(j + 1)] / 1000) * T));
j++;
}
}
// 罗刹海市 T:130
void player_6(int t){
float T = (float)60/t*1000;
for (int j = (0); j < (sizeof(demo_6_midi)/sizeof(demo_6_midi[0])); j = j + 1) {
fmq_play(demo_6_midi[(int)(j)],(((float)demo_6_midi[(int)(j + 1)] / 1000) * T));
j++;
}
}
/**
* @brief 播放一个midi
*
* @param val 播放曲子的编号,0:沧海一声笑,1:别知己,2:孤勇者,3:赛马T110,4:早安隆回,5:向云端T70,6:罗刹海市T130
* @param T 速度
*/
void fmq_play_demo(int val,int T){
switch (val)
{
case 0:
//沧海一声笑
player_0(T);
break;
case 1:
//别知己
player_1(T);
break;
case 2:
//孤勇者
player_2(T);
break;
case 3:
//赛马
player_3(T);
break;
case 4:
//早安隆回
player_4(T);
break;
case 5:
//向云端
player_5(T);
break;
case 6:
//罗刹海市
player_6(T);
break;
}
}
/**
* @brief 初始化蜂鸣器
*
* @param pin 引脚号
* @param vol 音量,1-100
*/
void fmq_init(uint8_t pin, uint16_t vol){
if(pin > 34)return; // 如果引脚超过34就退出
// 取出引脚对应pwm通道
for (int i = 0; i < 4; i++) {
if (pin % 4 == pwm_Ch[i]) {
CH = i;
}
}
vol = 100 - vol;
pwm = bflb_device_get_by_name("pwm_v2_0");
gpio = bflb_device_get_by_name("gpio");
/*初始化IO ,pwm0*/
bflb_gpio_init(gpio, pin, GPIO_FUNC_PWM0 | GPIO_ALTERNATE | GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_1);
/*初始化PWM输出*/
struct bflb_pwm_v2_config_s cfg = {
.clk_source = BFLB_SYSTEM_XCLK, // PWM时钟源,
.clk_div = 40, // PWM时钟分频器
.period = 1000, // PWM计数周期
};
bflb_pwm_v2_init(pwm, &cfg);
bflb_pwm_v2_channel_set_threshold(pwm, CH, vol, 100); //调整占空比,可以调整蜂鸣器音量,
bflb_pwm_v2_channel_positive_start(pwm, CH);
}
/**
* @brief 演奏一个音符
*
* @param val 音符
* @param num 延时
*/
void fmq_play(int val,int num){
int period = 1000000 / val;
// bflb_pwm_v2_stop(pwm);
bflb_pwm_v2_set_period(pwm,period);
bflb_pwm_v2_start(pwm);
bflb_mtimer_delay_ms(num);
bflb_pwm_v2_stop(pwm);
bflb_mtimer_delay_ms(3);
}
#endif
<span style="font-size: 20px"> main.c </span>
#include "board.h"
#include "fmq.h"
int main(void)
{
board_init();
//初始化,引脚号,音量
fmq_init(31,15);
//蜂鸣器播放一首曲子
fmq_play_demo(5, 70);
while (1) {
bflb_mtimer_delay_ms(1000);
}
}
<span style="font-size: 20px">代码调用很简单,楼楼里面也写的很清楚啦,大家如果感兴趣的话可以试着玩一下,如果代码有不对的地方,大家可以自行修改! </span>
<span style="font-size: 20px"> 好啦,以上就是楼楼本次的分享,如果有喜欢的歌也可以留言给楼楼,楼楼有空的话响给你听! </span>
<span style="font-size: 20px;color:#8bded5"> <br>写在最后:如果你想添加自己喜欢的音乐,下面是一个midi代码编写指南: </span>
<span style="font-size: 18px;color:#8bded5"> 参考demo,一个音符跟一个时值,开头必须是音符,末尾必须是一个时值,都是用大写字母表示,一共有3个八度,C0[低音],C[中音],C1[高音],C代表音符[1234567]对应的[ABCDEFG],后面的数字表示演奏哪个八度,0是低八度,1是高八度,什么没有就是中音,时值用大写L表示。如:L4,代表4分音符,时值分别有:[L1,L2,L4,L8,L16,L32],分别对应着[全音符,2分,4分,8分,16分,32分音符],在时值LX的后面加一个0可以表示一位附点音符,如:L40,L80...,字母N表示休止符,在需要停顿的地方使用 </span>
[/i]