MIDI模块(电子琴)
来自Labplus盛思维基百科
概述
用于播放乐符,可模拟电子琴等各种乐器演奏效果
技术参数
- 工作电压:3.3V or 5V
- 通讯方式:UART
- 集成功放、双声道喇叭
- 模块尺寸:24x46x7.5mm
引脚定义
| VCC | 电源 |
| RXD | 串口接收 |
| TXD | 串口发送 |
| GND | 地 |
使用教程
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);
}
MicroPython示例
使用Bluebit主板控制MIDI模块中间需要加I2C转串口模块
from microbit import *
def midiInit(volume=0xb4):
i2c.write(0x0c, bytearray([0x01, 0x12, 0x7a])) # set baudrate 31250
i2c.write(0x0c, bytearray([0x02, 0xb0, 0x79, 0x00])) # reset
i2c.write(0x0c, bytearray([0x02, 0xb0, 0x79, 0x7b])) # close all
i2c.write(0x0c, bytearray([0x02, 0xb0, 0x07, volume])) # set volume
def midiChangeProgram(channel, ins): # select instrument
i2c.write(0x0c, bytearray([0x02, (0xC0|(channel & 0x0F)), ins]))
def midiNoteOn(data1, data2=50, cmd=0x90):
i2c.write(0x0c, bytearray([0x02, cmd, data1, data2]))
def midiNoteOff(data1, data2=50, cmd=0x80):
i2c.write(0x0c, bytearray([0x02, cmd, data1, data2]))
# test code
midiInit()
midiChangeProgram(0,41)
while True:
midiNoteOn(69)
sleep(500)
midiNoteOff(69)
sleep(500)
