音乐转换成16进制写进单片机的方法

这个需要自己把乐谱转换为十六进制数据:

相关知识:

1.要产生音频脉冲,只要算出某一音频的周期(1/频率),然后将此周期除以2,即为半周期的时间。利用定时器计时这个半周期时间,每当计时到后就将输出脉冲的I/O反相,然后重复计时此半周期时间再对I/O反相,就可在I/O脚上得到此频率的脉冲。

2.利用8051的内部定时器使其工作在计数器模式MODE1下,改变计数值TH0及TL0以产生不同频率的方法。

3.例如频率为523Hz,其周期T=1/523=1912us,因此只要令计数器计时956us/1us=956,在每计数956次时将I/O反相,就可得到中音DO(523Hz)。

计数脉冲值与频率的关系公式如下:

N=Fi÷2÷Fr N:计数值;

Fi:内部计时一次为1us,故其频率为12MHz;

Fr:要产生的频率;

4.其计数值的求法如下:

T=65536-N=65536-Fi÷2÷Fr

例如:设K=65536,F=1000000=Fi=1MHz,求低音DO(261MHz)、中音DO(523MHz)、高音DO(1046MHz)的计数值。

T=65536-N=65536-Fi÷2÷Fr=65536-1000000÷2÷Fr=65536-500000/Fr

低音DO的T=65536-500000/262=63627

中音DO的T=65536-500000/523=64580

高音DO的T=65536-500000/1047=65059

5.C调各音符频率与计数值T的对照表如下:

表1 C调各音符频率与计数值T的对照表

音符

低1DO 频率(Hz) 简谱码(T值) 音符 频率(Hz) 简谱码(T值)

262 63628 #4FA# 740 64860

#1DO# 277 63731 中5SO 784 64898

低2RE

#2RE# 294 63835 #5SO# 831 64934

311 63928 中6LA 880 64968

低3M 330 64021 #6 932 64994

低4FA 349 64103 中7SI 988 65030

#4FA# 370 64185 高1DO 1046 65058

低5SO 392 64260 #1DO# 1109 65085

#5SO# 415 64331 高2RE 1175 65110

低6LA 440 64400 #2RE# 1245 65134

#6 466 64463 高3M 1318 65157

低7SI 494 64524 高4FA 1397 65178

中1DO 523 64580 #4FA# 1480 65198

#1DO# 554 64633 高5SO 1568 65217

中2RE 587 64684 #5SO# 1661 65235

#2RE# 622 64732 高6LA 1760 65252

中3M 659 64777 #6 1865 65268

中4FA 698 64820 高 1967 65283

表2 节拍与节拍码对照

节 拍 码 节 拍 数 节 拍 码 节 拍 数

1 1/4拍 1 1/8拍

2 2/4拍 2 1/4拍

3 3/4拍 3 3/8拍

4 1拍 4 1/2拍

5 1又1/4拍 5 5/8拍

6 1又1/2拍 6 3/4拍

8 2拍 8 1

A 2又1/2拍 A 1又1/4拍

C 3拍 C 1又1/2拍

F 3又3/4拍

表3各调1/4节拍的时间设定 各调1/4节拍的时间设定

曲 调 值 DELAY 曲 调 值 DELAY

调4/4 125毫秒 调4/4 62毫秒

调3/4 187毫秒 调3/4 94毫秒

调2/4 250毫秒 调2/4 125毫秒

1/4拍的延迟时间=187毫秒

DELAY: MOV R7,#02

D2: MOV R4,#187 别的延迟值,只需修改这儿的值为相应值,即可。

D3: MOV R3,#248

DJNZ R3,$

DJNZ R4,D3

DJNZ R7,D2

DJNZ R5,DELAY 节拍值放在R5,决定节拍

RET

建立音乐的步骤:

1. 先把乐谱的音符找出,然后建立T值表的顺序。

2. 把T值表建立在TABLE1,构成发音符是计数值放在“TABLE”。

3. 简谱码(音符)为高位,节拍为(节拍数)为低4位,音符节拍码放在程序的“TABLE”处。

相关知识:

每一音符使用1个字节,字节的高4位代表音符的高低,低4位代表音符的节拍,表2为节拍与节拍码的对照。如果1拍为0.4秒,1/4拍是0.1秒,只要设定延迟时间就可求得节拍的时间 。假使1/4拍为1DELAY,则1拍应为4DELAY,以此类推。所以只要求得1/4拍的DEALY时间,其余的节拍就是它的倍数,如表3为1/4和1/8节拍的时间设定。

简 谱 发 音 简 谱 码 T值

5 低音SO 1 64260

6 低音LA 2 64400

7 低音TI 3 64524

1 中音DO 4 64580

2 中音RE 5 64684

3 中音MI 3 64777

4 中音FA 7 64820

5 中音SO 8 64898

6 中音LA 9 64968

7 中音TI A 65030

1 高音DO B 65058

2 高音RE C 65110

3 高音MI D 65157

4 高音FA E 65178

5 高音SO F 65217

不发音 0

节 拍 码 节 拍 数

1 1/4拍

2 2/4拍

3 3/4拍

4 1拍

5 1又1/4拍

6 1又1/2拍

8 2拍

A 2又1/2拍

C 3拍

F 3又3/4拍

表4 简谱对应的简谱码、T值、节拍数

#include "reg51.h"

#define uint unsigned int

#define uchar unsigned char

uchar * TABLE ;

code uint TABLE1 [15]={64260,64400,64524,64580,

64684,64777,64820,64898,

64968,65030,65058,65110,

65157,65178,65217};

code uchar SONG [ ]={

//1. 迟来的爱

//0

0x08,0x08,

//1

0x02,0x12,0x42,0x62,0x52,0x42,0x21,11,

0x18,0x18,

0x02,0x22,0x42,0x82,0x92,0x82,0x61,0x51,0x42,

0x58,0x58,

//2

0x66,0x51,0x61,0x84,0x62,0x82,

0x52,0x62,0x42,0x52,0x28,

0x52,0x42,0x52,0x62,0x94,0x81,0x91,0x81,0x61,

0x1C,0x12,0x82,

//3

0x68,0x02,0x92,0x82,0x42,

0x5C,0x12,0x62,

0x58,0x02,0x42,0x51,0x41,0x22,

0x4C,0x62,0x52,

//4

0x66,0x82,0x92,0x82,0x61,0x51,0x41,0x51,

0x6C,0x52,0x62,

0x53,0x63,0x52,0x42,0x42,0x22,

0x1C,0x12,0x22,

//5

0x42,0x44,0x51,0x61,0x82,0x84,0x61,0x81,

0x96,0x82,0x66,0x51,0x61,

0x56,0x42,0x22,0x42,0x82,0x62,

0x5C,0x62,0x52,

//6

0x66,0x82,0x92,0x82,0x61,0x51,0x41,0x51,

0x6C,0x52,0x62,

0x56,0x62,0x52,0x42,0x42,0x22,

0x1C,0x12,0x22,

//7

0x42,0x44,0x51,0x61,0x82,0x84,0x61,0x81,

0x96,0x82,0x66,0x51,0x61,

0x56,0x62,0x92,0x82,0x62,0x52,

0x4C,0x62,0x61,0x81,

//8

0x9C,0x81,0x91,0x81,0x61,

0x6C,0x82,0x62,

0x56,0x42,0x24,0x42,0x52,

0x6C,0x62,0x61,0x81,

//9

0x9C,0xB2,0x92,

0x8C,0x82,0x92,

0xB2,0xB2,0xB2,0x92,0xD4,0xC1,0xD1,0xC1,0xB1,

0xCC,0xB2,0xB1,0xC1,

//A

0xDC,0xD2,0xC1,0xB1,

0x9C,0x92,0x91,0xC1,

0x92,0x84,0x92,0x82,0x62,0x51,0x61,0x51,0x41,

0x48,0x48,

//B

0x42,0x41,0x51,0x66,0x82,0x91,0xB1,

0x92,0x86,0x88,

0x42,0x41,0x51,0x66,0x42,0x41,0x51,

0x22,0x16,0x18,

0x86,0x62,0x58,

//C

0x83,0x91,0x62,0x82,0x58,

0x52,0x62,0x52,0x42,0x22,0x12,0x62,0x52,

0x4C,0x62,0x52,

//6

0x66,0x82,0x92,0x82,0x61,0x51,0x41,0x51,

0x6C,0x52,0x62,

0x56,0x62,0x52,0x42,0x42,0x22,

0x1C,0x12,0x22,

//7

0x42,0x44,0x51,0x61,0x82,0x84,0x61,0x81,

0x96,0x82,0x66,0x51,0x61,

0x56,0x62,0x92,0x82,0x62,0x52,

0x4C,0x62,0x61,0x81,

//8

0x9C,0x81,0x91,0x81,0x61,

0x6C,0x82,0x62,

0x56,0x42,0x24,0x42,0x52,

0x6C,0x62,0x61,0x81,

//9

0x9C,0xB2,0x92,

0x8C,0x82,0x92,

0xB2,0xB2,0xB2,0x92,0xD4,0xC1,0xD1,0xC1,0xB1,

0xCC,0xB2,0xB1,0xC1,

//A

0xDC,0xD2,0xC1,0xB1,

0x9C,0x92,0x91,0xC1,

0x92,0x84,0x92,0x82,0x62,0x51,0x61,0x51,0x41,

0x4C,0x62,0x61,0x81,

//8

0x9C,0x81,0x91,0x81,0x61,

0x6C,0x82,0x62,

0x56,0x42,0x24,0x42,0x52,

0x6C,0x62,0x61,0x81,

//9

0x9C,0xB2,0x92,

0x8C,0x82,0x92,

0xB2,0xB2,0xB2,0x92,0xD4,0xC1,0xD1,0xC1,0xB1,

0xCC,0xB2,0xB1,0xC1,

//A

0xDC,0xD2,0xC1,0xB1,

0x9C,0x92,0x91,0xC1,

0x92,0x84,0x92,0x82,0x62,0x51,0x61,0x51,0x41,

0x4C,0x62,0x61,0x81,

//D

0x66,0x51,0x61,0x86,0x61,0x81,

0x52,0x62,0x52,0x42,0x28,

0x82,0x91,0x01,0x62,0x11,0x52,0x11,0x01,0x62,0x54,

0x48,0x48,0x00};

uchar ljsz;

uchar jpm;

uchar yfm;

uchar t0 =0;

uint i=0;

sbit Speaker =P1^0;

uint k;

void Delay1()

{

for ( k=0;k<20000;k++);

}

void Lignt( )

{ uchar t;

for(t=0;t<10;t++)

{

P2=0xff;

Delay1();

P2=0x00;

Delay1();

}

}

void Lignt1( )

{

P2=0xaa;

Delay1();

P2=0x55;

Delay1();

}

void Delay(uchar t0)//延时

{

while(t0--) //决定节拍

Lignt1();

}

void Delay2(uchar w0)//延时

{

while(w0--) //决定节拍

Lignt();

}

void T0Int() interrupt 1//TIMER0

{

TL0 = ljsz;

TH0 = hjsz;

Speaker=!Speaker;

}

void ExtInt0() interrupt 0//INT0

{

if(t0>10)

t0=0;

i=0;

switch(t0++)

{

case 0 : TABLE=SONG;break; //选择第1首

case 1 : TABLE=SONG1;break;//选择第2首

case 2 : TABLE=SONG2;break;//选择第3首

case 3 : TABLE=SONG3;break;//选择第4首

case 4 : TABLE=SONG4;break;//选择第5首

case 5 : TABLE=SONG5;break;//选择第6首

case 6 : TABLE=SONG6;break;//选择第7首

case 7 : TABLE=SONG7;break;//选择第8首

case 8 : TABLE=SONG8;break;//选择第9首

case 9 : TABLE=SONG9;break;//选择第10首

}

}

void songsing()//发音程序

{

i=0;

while(TABLE[i])

{

jpm=TABLE[i]&0x0f; //节拍值

yfm=(TABLE[i]>>0x04)&0x0f;//简谱值

if(yfm) //简谱为1,取计数值

{

yfm=yfm-1;

hjsz=(uchar)(TABLE1[yfm]>>0x08);//取计数值高字节

TH0 = hjsz;

ljsz=(uchar)(TABLE1[yfm]); //取计数值低字节

TL0 = ljsz;

TR0 = 1; //启动TIMER0

}

else TR0 = 0; //简谱为0,不发音

Delay(jpm); //节拍延时

i++;

}

}

void main(void)

{

TMOD =0X01;//设TIMER0在MODE1

TCON =0X01;//外部中断INT0,下降沿触发

IE =0x83;//开中断(INT0,TIMER0)

IP =0x00;//设置中断优先级

TABLE=SONG0;

while(1)

{

Lignt( );

songsing();//调用发音程序

TR0 = 0;//停止TIMER0

Delay2(1);

TABLE=SONG0;

songsing();

}

}