说明: 我们已经在第十六讲介绍过有源蜂鸣器,因此这里只介绍无源蜂鸣器。 无源蜂鸣器没有震荡源,因此需要通过脉宽调制(PWM)输入特定的方波电压信号来发声。PWM原理可在其他教程中查找,本系列主要介绍传感器。 演奏一首音乐,最重要的每个音的音调(频率)和节拍(时长)。 C调音符对应的音调频率如下图:(每两个音调频率相差 )
我们可以通过tone(pin,frequnce)函数来控制无源蜂鸣器的振动频率,利用delay()来控制节拍,利用noTone(pin)来终止振动。
实验目的:控制无源蜂鸣器演奏《天空之城》。天空之城是C调,一拍为四分之一音符长度,每一小节为四拍。 黑点在上为高音,在下为低音,没有为中音乐。而每两个竖杠之间为一小节,每一小节共有四个四分之一拍。下面如果画了一条横线,则是四分之一拍的二分之一,即八分之一拍;画了两条横线,则是四分之一的四分之一,即十六分之一拍。音符后面的横线则是前一音符的延续。
器材:Stduino UNO/Nano、杜邦线、无源蜂鸣器模块
电路连接:
代码展示: #define C0 (2)
#define C1_LOW (262)
#define C2_LOW (294)
#define C3_LOW (330)
#define C4_LOW (349)
#define C5_LOW (392)
#define C6_LOW (440)
#define C7_LOW (494)
#define C1 (523)
#define C2 (587)
#define C3 (659)
#define C4 (698)
#define C4S (740)//C4#
#define C5 (784)
#define C5S (831)//C5#
#define C6 (880)
#define C7 (988)
#define C1_HIGH (1046)
#define C2_HIGH (1175)
#define C3_HIGH (1318)
#define C4_HIGH (1397)
#define C5_HIGH (1568)
#define C6_HIGH (1760)
#define C7_HIGH (1976)
#define WHOLE (1)
#define HALF (0.5)
#define QUARTER (0.25)
#define EIGHTH (0.125)
#define SIXTEENTH (0.0625)
int tune[]= //根据简谱列出各频率
{
C6,C7,
C1_HIGH,C7,C1_HIGH,C3_HIGH,//
C7,C3,C3,
C6,C5,C6,C1_HIGH,
C5,C0,C3,C3,
C4,C3,C4,C1_HIGH,
C3,C0,C1_HIGH,C1_HIGH,C1_HIGH,
C7,C4S,C4S,C7,
C7,C0,C6,C7,//
C1_HIGH,C7,C1_HIGH,C3_HIGH,
C7,C0,C3,C3,
C6,C5,C6,C1_HIGH,
C5,C0,C3,
C4,C1_HIGH,C7,C7,C1_HIGH,
C2_HIGH,C2_HIGH,C3_HIGH,C1_HIGH,C0,
C1_HIGH,C7,C6,C6,C7,C5S,//
C6,C0,C1_HIGH,C2_HIGH,
C3_HIGH,C2_HIGH,C3_HIGH,C5_HIGH,
C2_HIGH,C0,C5,C5,
C1_HIGH,C7,C1_HIGH,C3_HIGH,C0,C0,
C6,C7,C1_HIGH,C7,C2_HIGH,C2_HIGH,
C1,C5,C5,C0,
C4_HIGH,C3_HIGH,C2_HIGH,C1_HIGH,//
C3_HIGH,
C3_HIGH,C0,C3_HIGH,
C6_HIGH,C5_HIGH,C5_HIGH,
C3_HIGH,C2_HIGH,C1_HIGH,C0,C1_HIGH,
C2_HIGH,C1_HIGH,C2_HIGH,C2_HIGH,C5_HIGH,
C3_HIGH,C0,C3_HIGH,
C6_HIGH,C5_HIGH,
C3_HIGH,C2_HIGH,C1_HIGH,C0,C1_HIGH,
C2_HIGH,C1_HIGH,C2_HIGH,C2_HIGH,C7,
C6,C0,C6,C7,
C6
};
float durt[]= //根据简谱列出各节拍
{
EIGHTH,EIGHTH,
QUARTER + EIGHTH,EIGHTH,QUARTER,QUARTER,
QUARTER + QUARTER + QUARTER,EIGHTH,EIGHTH,
QUARTER + EIGHTH,EIGHTH,QUARTER,QUARTER,
QUARTER + QUARTER,QUARTER,EIGHTH,EIGHTH,
QUARTER + EIGHTH,EIGHTH,EIGHTH,QUARTER+EIGHTH,
QUARTER + QUARTER,QUARTER,EIGHTH,EIGHTH,EIGHTH,
QUARTER + EIGHTH,EIGHTH,QUARTER,QUARTER,
QUARTER+QUARTER,QUARTER,EIGHTH,EIGHTH,
QUARTER+EIGHTH,EIGHTH,QUARTER,QUARTER,
QUARTER+QUARTER,QUARTER,EIGHTH,EIGHTH,
QUARTER+EIGHTH,EIGHTH,QUARTER,QUARTER,
QUARTER+QUARTER+QUARTER,EIGHTH,EIGHTH,
QUARTER,EIGHTH,EIGHTH,EIGHTH,QUARTER,
EIGHTH,EIGHTH,EIGHTH,QUARTER,QUARTER,
QUARTER,EIGHTH,EIGHTH,EIGHTH,QUARTER,QUARTER,
QUARTER+QUARTER,QUARTER,EIGHTH,EIGHTH,
QUARTER+EIGHTH,QUARTER,QUARTER,QUARTER,
QUARTER+QUARTER,QUARTER,EIGHTH,EIGHTH,
EIGHTH,EIGHTH,QUARTER,QUARTER,
QUARTER+QUARTER,QUARTER,QUARTER,
EIGHTH,EIGHTH,QUARTER,QUARTER,EIGHTH,EIGHTH,
QUARTER+EIGHTH,EIGHTH,QUARTER,QUARTER,
QUARTER,QUARTER,QUARTER,QUARTER,
QUARTER+QUARTER+QUARTER,
QUARTER+QUARTER,QUARTER,QUARTER,
QUARTER+QUARTER,QUARTER,QUARTER,
EIGHTH,EIGHTH,QUARTER,EIGHTH,EIGHTH,
QUARTER,EIGHTH,EIGHTH,EIGHTH,QUARTER,
QUARTER+QUARTER,QUARTER,QUARTER,
QUARTER+QUARTER,QUARTER+QUARTER,
EIGHTH,EIGHTH,QUARTER+QUARTER,EIGHTH,EIGHTH,
QUARTER,EIGHTH,EIGHTH,EIGHTH,QUARTER,
QUARTER+QUARTER,QUARTER,EIGHTH,EIGHTH,
QUARTER+QUARTER+QUARTER
};
int Time = 2000;
int BUZZER = A0;
int length;
void setup()
{
// put your setup code here, to run once:
length=sizeof(tune)/sizeof(tune[0]); //计算长度
}
void loop()
{
// put your main code here, to run repeatedly:
for(int i=0;i<length;i++)
{
if(tune[ i]!=0)
{
tone(BUZZER,tune[ i]);
}
else
{
noTone(BUZZER);
}
delay(Time*durt[ i]); //这里用来乐曲速度。
}
delay(2000);
} Stduino IDE是一款面向32位处理器快速入门学习的集成开发平台,目前已经支持Arduino_Core_STM32框架,集聚快捷、高效、免费、开源、易得于一身,软件还集成了GDB调试功能。
IDE当前支持的功能有: - GDB调试
- 封装库下载安装
- 代码自动补全提示
- 中英文模式
- UTF-8\GBK编码格式
- 代码快速格式化
- SWD\串口程序下载&调试(已验证)
- DFU程序下载方式(待验证)
- USB HID程序下载方式(待验证)
- maple DFU程序下载方式(待验证)
- maple DFU2程序下载方式(待验证)
- MassStorage程序下载方式(待验证)
更多功能细节请登录图片上水印显示的网址进行了解 已经支持的芯片&板型列表: - Stduino UNO&Nano
- Nucleo 144 boards
- Nucleo 64 boards
- Nucleo 32 boards
- Discovery boards
- Eval boards
- STM32MP1 series coprocessor boards
- Generic STM32F0 boards
- Generic STM32F1 boards
- Generic STM32F3 boards
- Generic STM32F4 boards
- Generic STM32H7 boards
- Generic STM32L0 boards
- 3D printer boards
- LoRa boards
- Electronic Speed Controller boards
- Generic flight controllers
- Garatronics boards
- Midatronics boards
更多功能集成正在进行中~ |