51单片机《孤勇者》音谱

时间:2023-01-28 06:43:57     来源:哔哩哔哩

食用须知:

1.谱子是我自己打的,但总格式来自江科大自化协的[11-2] 蜂鸣器播放提示音&音乐_哔哩哔哩_bilibili。

2.由于单片机的生产厂家不同,有些IO口也不同,请以自己单片机的原理图为主,如果播放不出来,可以看看是不是蜂鸣器端口写错了。


(资料图)

3.代码用了模块化编程,Delay和Timer部分的代码未放出,可以自己去上述链接找一下。

主函数代码如下:

#include <REGX52.H>

#include "Delay.h"

#include "Timer0.h"

//蜂鸣器端口定义

sbit Buzzer=P2^5;

//播放速度,值为四分音符的时长(ms)

#define SPEED500

//音符与索引对应表,P:休止符,L:低音,M:中音,H:高音,下划线:升半音符号#

#define P0

#define L11

#define L1_2

#define L23

#define L2_4

#define L35

#define L46

#define L4_7

#define L58

#define L5_9

#define L610

#define L6_11

#define L712

#define M113

#define M1_14

#define M215

#define M2_16

#define M317

#define M418

#define M4_19

#define M520

#define M5_21

#define M622

#define M6_23

#define M724

#define H125

#define H1_26

#define H227

#define H2_28

#define H329

#define H430

#define H4_31

#define H532

#define H5_33

#define H634

#define H6_35

#define H736

//索引与频率对照表

unsigned int FreqTable[]={

0,

63628,63731,63835,63928,64021,64103,64185,64260,64331,64400,64463,64528,

64580,64633,64684,64732,64777,64820,64860,64898,64934,64968,65000,65030,

65058,65085,65110,65134,65157,65178,65198,65217,65235,65252,65268,65283,

};

//乐谱

unsigned char code Music[]={

//音符,时值,

//1

M2, 4+4+4+4,

M2, 2,

L7, 2,

M1, 2,

L6, 2,

M2, 2,

L7, 2,

M1, 2,

L6, 2,

M2, 2,

L7, 2,

M1, 2,

L6, 2,

M2, 2,

L7, 2,

M1, 2,

L6, 2,

M2, 2,

L7, 2,

M1, 2,

L6, 2,

M2, 2,

L7, 2,

M1, 2,

L6, 2,

M2, 2,

L7, 2,

M1, 2,

L6, 2,

M2, 2,

L7, 2,

M1, 2,

L6, 2,

M2, 2,

L7, 2,

M1, 2,

L6, 2,

M2, 2,

L7, 2,

M1, 2,

L6, 2,//前奏完

M3, 4+4,

P,  4,

P,  2,

M1, 1,

M2, 1,

M1, 1,

M3, 4+4,

P,  2,

M1, 3,

M2, 1,

M1, 1,

M2, 1,

M3, 1,

L6, 3,

M1, 1,

L6, 3,

M1, 1,

L6, 3,

M1, 1,

M2, 2,

M1, 2,

L7, 4+4,

P,  4,

P,  4,//你犯的错

//2

M3, 4+4,

P,  4,

P,  2,

M1, 1,

M2, 1,

M1, 1,

M3, 4+4,

P,  2,

M1, 3,

M2, 1,

M1, 1,

M2, 1,

M3, 1,

L6, 3,

M1, 1,

L6, 3,

M1, 1,

L6, 3,

M1, 1,

M3, 2,

M2, 2,

L7, 4+4,

P,  4,

P,  4, //你的自我

//3

L6, 1,

M1, 1,

M6, 3,

M6, 1,

M6, 1,

M5, 1,

M6, 2,

M6, 1,

M5, 1,

M6, 1,

M5, 1,

M6, 1,

M5, 1,

M3, 1,

M3, 4+3,

P,  4,

P,  2, //每一头怪兽

L6, 1,

M1, 1,

M6, 3,

M6, 1,

M6, 1,

M5, 1,

M6, 1,

M5, 1,

M7, 3,

M7,1,

M7,1,

M6,1,

M7,3,

M5,1,

M6,1,

M3,4+4+1,//没有人爱小丑

P,  1,

M3,1,

M5,1,

M3,1,

M2,3,

M3,1,

M2,3,

M3,1,

M2,3,

M3,1,

M5,1,

M3,1,

M5,1,

M3,1,

M2,3,

M3,1,

M2,3,

M3,1,

M2,4,

P,  2,

M1,1,

M2,1,

M3,2,

L6,2,

M1,2,

M3,2,

M2,3,

M3,1,

M2,1,

M1,3,

L6,4+4,

P,  4,

P,2,

M6,1,

M7,1,

H1,1,

H2,1,

M7,1,

H1,1,

H1,2,

H1,1,

M7,1,

H1,1,

H2,1,

M7,1,

H1,1,

H1,2,

H1,1,

H2,1,

H3,1,

H2,1,

H3,1,

H2,1,

H3,2,

H3,1,

H2,1,

H3,2,

H5,2,

H3,2,

H6,1,

H7,1,

H1,1,

H2,1,

M7,1,

H1,1,

H1,2,

H1,1,

M7,1,

H1,1,

H2,1,

M7,1,

H1,1,

H1,2,

H1,1,

H2,1,

H3,1,

H2,1,

H3,1,

H2,1,

H3,2,

H3,1,

H2,1,

H3,2,

H5,2,

H3,2,

H5,2,

H3,3,

H5,1,

H3,3,

H5,1,

H3,1,

H5,1,

H6,1,

H3,1,

H5,2,

H5,2,

H3,3,

H5,1,

H3,3,

H5,1,

H3,1,

H5,1,

H6,1,

H3,1,

H5,2,

H5,1,

H5,1,

H3,1,

H2,1,

H2,2,

H2,1,

H1,1,

H3,2,

H2,1,

H2,2,

H2,1,

H1,1,

H1,1,

H1,1,

M6,4+4,

P,4,

P,2,

H5,1,

H5,1,

H3,1,

H2,1,

H2,2,

H2,1,

H1,1,

H1,1,

H3,1,

H3,1,

H2,1,

H2,2,

H2,1,

H1,1,

H1,1,

H1,1,

M6,4+4,

P,4,

P,4,

P,4,

P,4,

P,4,

P,4,

P,4,

P,4,

P,4,

P,4,

P,4,

P,4,

P,4,

P,2,

M6,1,

M5,1,

M6,3,

M5,1,

M6,1,

M5,1,

M6,1,

M5,1,

M6,2,

M6,1,

M5,1,

M6,1,

M5,1,

M6,1,

M5,1,

M3,1,

M3,4+3,

P,4,

P,2,

M6,1,

M5,1,

M6,3,

M5,1,

M6,1,

M5,1,

M6,1,

M5,1,

M7,3,

M6,1,

M7,1,

M6,1,

M7,1,

M6,1,

M3,1,

M3,4+3,

P,4,

P,1,

M3,1,

M5,1,

M3,1,

M2,3,

M3,1,

M2,3,

M3,1,

M2,3,

M3,1,

M5,1,

M3,1,

M5,1,

M3,1,

M2,3,

M3,1,

M2,3,

M3,1,

M2,4,

P,2,

M1,1,

M2,1,

M3,2,

M6,2,

H1,2,

H3,2,

H2,3,

M3,1,

H2,1,

H1,1,

H1,2,

M6,4+4+4,

P,2,

M6,1,

M7,1,

//循环结束

H1,1,

H2,1,

M7,1,

H1,1,

H1,2,

H1,1,

M7,1,

H1,1,

H2,1,

M7,1,

H1,1,

H1,2,

H1,1,

H2,1,

H3,1,

H2,1,

H3,1,

H2,1,

H3,2,

H3,1,

H2,1,

H3,2,

H5,2,

H3,2,

H6,1,

H7,1,

H1,1,

H2,1,

M7,1,

H1,1,

H1,2,

H1,1,

M7,1,

H1,1,

H2,1,

M7,1,

H1,1,

H1,2,

H1,1,

H2,1,

H3,1,

H2,1,

H3,1,

H2,1,

H3,2,

H3,1,

H2,1,

H3,2,

H5,2,

H3,2,

H5,2,

H3,3,

H5,1,

H3,3,

H5,1,

H3,1,

H5,1,

H6,1,

H3,1,

H5,2,

H5,2,

H3,3,

H5,1,

H3,3,

H5,1,

H3,1,

H5,1,

H6,1,

H3,1,

H5,2,

H5,1,

H5,1,

H3,1,

H2,1,

H2,2,

H2,1,

H1,1,

H3,2,

H2,1,

H2,2,

H2,1,

H1,1,

H1,1,

H1,1,

M6,4+4,

P,4,

P,2,

H5,1,

H5,1,

H3,1,

H2,1,

H2,2,

H2,1,

H1,1,

H1,1,

H3,1,

H3,1,

H2,1,

H2,2,

H2,1,

H1,1,

H1,1,

H1,1,

M6,4+4,

P,4,

P,4,

P,4,

P,4,

P,4,

P,4,

P,4,

P,4,

P,4,

P,4,

P,4,

P,4,

P,4,

P,2,

M6,1,

M5,1,

M6,3,

M5,1,

M6,1,

M5,1,

M6,1,

M5,1,

M6,2,

M6,1,

M5,1,

M6,1,

M5,1,

M6,1,

M5,1,

M3,1,

M3,4+3,

P,4,

P,2,

M6,1,

M5,1,

M6,3,

M5,1,

M6,1,

M5,1,

M6,1,

M5,1,

M7,3,

M6,1,

M7,1,

M6,1,

M7,1,

M6,1,

M3,1,

M3,4+3,

P,4,

P,1,

M3,1,

M5,1,

M3,1,

M2,3,

M3,1,

M2,3,

M3,1,

M2,3,

M3,1,

M5,1,

M3,1,

M5,1,

M3,1,

M2,3,

M3,1,

M2,3,

M3,1,

M2,4,

P,2,

M1,1,

M2,1,

M3,2,

M6,2,

H1,2,

H3,2,

H2,3,

M3,1,

H2,1,

H1,1,

H1,2,

M6,4+4+4,

P,2,

M6,1,

M7,1,

//又循环一轮结束

H3,1,

H2,1,

H2,2,

H2,1,

H2,1,

H1,1,

H3,1,

H3,1,

H2,1,

H2,2,

H2,1,

H1,1,

H1,1,

H1,1,

M6,2,

M6,2,

H1,2,

H3,2,

M7,4,

M7,2,

M7,1,

M7,1,

M6,1,

M6,3,

M6,4+4+4,

P,2,

M6,2,

H1,2,

H3,2,

M7,4,

M7,2,

M7,1,

M7,1,

M6,1,

M6,3,

M6,4+4,

P,2,

M6,1,

M7,1,

H3,1,

H2,1,

H2,2,

H2,1,

H1,1,

H1,1,

H3,2,

H2,1,

H2,2,

H2,1,

H1,1,

H1,1,

H1,1,

M6,4+4,

P,4,

P,4,

//片尾结束

0xFF//终止标志

};

unsigned char FreqSelect,MusicSelect;

void main()

{

Timer0Init();

while(1)

{

if(Music[MusicSelect]!=0xFF)//如果不是停止标志位

{

FreqSelect=Music[MusicSelect];//选择音符对应的频率

MusicSelect++;

Delay(SPEED/4*Music[MusicSelect]);//选择音符对应的时值

MusicSelect++;

TR0=0;

Delay(5);//音符间短暂停顿

TR0=1;

}

else//如果是停止标志位

{

TR0=0;

while(1);

}

}

}

void Timer0_Routine() interrupt 1

{

if(FreqTable[FreqSelect])//如果不是休止符

{

/*取对应频率值的重装载值到定时器*/

TL0 = FreqTable[FreqSelect]%256;//设置定时初值

TH0 = FreqTable[FreqSelect]/256;//设置定时初值

Buzzer=!Buzzer;//翻转蜂鸣器IO口

}

}

参考乐谱如下:

标签: TIMER MUSIC bilibili WHILE 自己打的 哔哩哔哩

最新文章推荐