我也贴一个我写的MIDI解码的算法代码,在ATmega8上用的:
#include <avr/io.h>
#include <avr/pgmspace.h>
#include "midifreq.h"
/*------宏定义------*/
#define uint unsigned int
#define uchar unsigned char
#define BIT(x) (1<<(x))
uchar k,config,midicode[3];
uint i,t0count,ontime,bps;
void enablepwm()
{
TCCR1A=0x23;
TCCR1B=0x1A;
}
void disablepwm()
{
TCCR1B=0x00;
TCCR1A=0x00;
}
void silent()
{
disablepwm();
TCNT1H=0x00;
TCNT1L=0x00;
}
void play()
{
silent();
OCR1A=pgm_read_byte(&midith[midicode[1]-21])*256+pgm_read_byte(&miditl[midicode[1]-21]);
OCR1B=((unsigned long)ontime*midicode[2])/127-1;
enablepwm();
}
//端口初始化
void port_init(void)
{
PORTB = 0x00;
DDRB = 0x04;
PORTC = 0x24;
DDRC = 0x3C;
PORTD = 0x6E;
DDRD = 0x00;
}
//定时T1初始化
void timer1_init(void)
{
disablepwm(); //停止定时器
TCNT1H = 0x00;
TCNT1L = 0x00; //初始值
OCR1AH = 0x00;
OCR1AL = 0x00; //匹配A值
OCR1BH = 0x00;
OCR1BL = 0x00; //匹配B值
ICR1H = 0xFF;
ICR1L = 0xFF; //输入捕捉匹配值
}
//串口通信初始化
void usart_init(void)
{
UCSRB = 0x00;//禁止中断
UCSRA = 0x00;
UCSRC = BIT(URSEL) | 0x06;
UBRRL = 0x0F;
UBRRH = 0x00;
}
//串行接收结束中断服务程序
SIGNAL(SIG_UART_RECV)
{
if(k==0)
{
k=UDR&0xF0; //判断即将接收的是否为发声指令
if(!((k==0x80)||(k==0x90)))
{
k=0;
return;
}
else
k=0;
}
midicode[k]=UDR;
k++;
if(k==3) //判断指令是否接收完毕
{
k=0x80|(0x0F&config);
if(midicode[0]==k)
silent();
else
{
k=0x90|(0x0F&config);
if(midicode[0]==k)
play();
}
k=0;
}
}
void init_devices(void)
{
cli(); //禁止所有中断
port_init();
timer0_init();
timer1_init();
usart_init();
int_init();
sei(); //开全局中断
}
因为这原本是给TC灭弧器用的所以省略了一部大分代码,只把与解码相关的部分贴上来,需要用的请自行补全~(midifreq.h保存了各个音符对应频率所应该给定时器装的初值)
MIDI接口电路示例: