概述
用于播放乐符,可模拟电子琴等各种乐器演奏效果
技术参数
- 工作电压:VCC 3.3-5V
- 通讯方式:UART
- 集成功放、双声道喇叭
- 模块尺寸:24x46x7.5mm
引脚定义
VCC |
电源
|
RXD |
串口接收
|
TXD |
串口发送
|
GND |
地
|
使用教程
1.RXD连接至microbit串口的TX,在定义串口引脚时需要注意下!
2.串口波特率:31250bps
3.程序烧下载完成后须断电重启
4.MIDI音符音色代码资料:
|
Arduino示例
#include <SoftwareSerial.h>
#include "vs1103b.h"
#define NOTE_C3 60
#define NOTE_D3 62
#define NOTE_E3 64
#define NOTE_F3 65
#define NOTE_G3 67
#define NOTE_A3 69
#define NOTE_B3 71
#define NOTE_C4 72
#define NOTE_D4 74
#define NOTE_E4 76
#define NOTE_F4 77
#define NOTE_G4 79
#define NOTE_A4 81
#define NOTE_B4 83
#define VELOCITY1 0x7F
#define VELOCITY2 0x00
uint8_t noteArr[14] = { 60, 62, 64, 65, 67, 69, 71, 72, 74, 76, 77, 79, 81, 83 };
Vs1103bClass vs1103b(A5, A4); //RX TX
;
void midiVolume(uint8_t vol);
void setup()
{
vs1103b.begin();
vs1103b.usartSendChannelMsg(CHANNEL0, MSG_CONTROL_CHANGE, DAT1_RESET, 0); //复位
vs1103b.usartSendChannelMsg(CHANNEL0, MSG_CONTROL_CHANGE, DAT1_NOTE_OFF, 0); //关闭所有音
midiVolume(180); //音量设置
}
void loop()
{
/*音调测试*/
for (int i = 0; i < 14; i++)
{
vs1103b.usartSendChannelMsg(CHANNEL0, MSG_NOTE_OPEN, noteArr[i], 0x7F);
delay(500);
vs1103b.usartSendChannelMsg(CHANNEL0, MSG_NOTE_OPEN, noteArr[i], 0x00);
delay(500);
}
}
void midiVolume(uint8_t vol)
{
vs1103b.usartSendChannelMsg(CHANNEL0, MSG_CONTROL_CHANGE, 0x07, vol);
delay(2);
}
python示例
|
连接示意图
|
from microbit import *
def midi_init(pin):
uart.init(baudrate=31250,tx=pin)
sleep(30)
uart.write(bytearray([0xb0,0x78,0x00]))
sleep(5)
uart.write(bytearray([0xb0,0x79,0x7f]))
sleep(15)
def midi_set_volume(vol):
uart.write(bytearray([0xb0,0x07,vol]))
sleep(10)
def midi_set_instrument(ins):
uart.write(bytearray([0xc0,ins]))
sleep(10)
def midi_note(note, on_off):
if on_off == 1:
uart.write(bytearray([0x90,note,0x7f]))
elif on_off == 0:
uart.write(bytearray([0x80,note,0x00]))
sleep(5)
#test code
music=bytearray([60,62,64,60,60,62,64,60,64,65,67,64,65,67]) #两只老虎
duration=bytearray([1,1,1,1,1,1,1,1,1,1,2,1,1,2]) #节拍
music_len=len(music)
midi_init(pin1) #设置串口tx引脚
midi_set_instrument(58) #设置音色
midi_set_volume(100) #设置音量,范围 0-127
while True:
for i in range(music_len):
midi_note(music[i],1)
sleep(200*duration[i])
midi_note(music[i],0)
sleep(200)
图形化示例
程序功能:播放钢琴的部分音符声音
|
|
版本历史记录
Version |
Date |
Note [+]新增[-]删除[^]修复
|
V2.0 |
|
|