作为一个多年的周杰伦粉丝,在学习单片机的过程中,那必须要将jay的歌曲让单片机演奏出来,这里,我将要让单片机演奏的是《蒲公英的约定》。
作者用的是普中51单片机,蜂鸣器接口是P2^5,如果不一样的话可以在代码中sbit Buzzer=P2^5中将后面的P2^5改为对应的接口。本代码灵感来源于https://blog.csdn.net/m0_63738169/article/details/140933085
剩下的不多说,直接放代码:
#include <REGX52.H>
#define SPEED 500
//音符与索引对应表,P:休止符,L:低音,M:中音,H:高音,下划线:升半音符号#
#define P0 0
#define L1 1
#define L1_ 2
#define L2 3
#define L2_ 4
#define L3 5
#define L4 6
#define L4_ 7
#define L5 8
#define L5_ 9
#define L6 10
#define L6_ 11
#define L7 12
#define M1 13
#define M1_ 14
#define M2 15
#define M2_ 16
#define M3 17
#define M4 18
#define M4_ 19
#define M5 20
#define M5_ 21
#define M6 22
#define M6_ 23
#define M7 24
#define H1 25
#define H1_ 26
#define H2 27
#define H2_ 28
#define H3 29
#define H4 30
#define H4_ 31
#define H5 32
#define H5_ 33
#define H6 34
#define H6_ 35
#define H7 36
sbit Buzzer=P2^5;
void Delay(unsigned int xms) //@12.000MHz
{
unsigned char i, j;
while(xms--)
{
i = 2;
j = 239;
do
{
while (--j);
} while (--i);
}
}
void Timer0_Init(void) //1毫秒@12.000MHz
{
TMOD &= 0xF0; //设置定时器模式
TMOD |= 0x01; //设置定时器模式
TL0 = 0x18; //设置定时初值
TH0 = 0xFC; //设置定时初值
TF0 = 0; //清除TF0标志
TR0 = 1; //定时器0开始计时
ET0=1;
EA=1;
}
unsigned int FreqTable[]={
0,
63777,63872,63969,64054,64140,64216,64291,64360,64426,64489,64547,64607,
64655,64704,64751,64795,64837,64876,64913,64948,64981,65012,65042,65070,
65095,65120,65144,65166,65186,65206,65225,65242,65259,65274,65289,65303,
};
unsigned int code Music[]={
//1
M3,2,
M5,2,
H1,2,
H3,2,
//2
H3,6,
H4,2,
H2,8,
//3
H2,4,
H5,2,
M7,2,
H1,8,
//4
H3,2,
H4,2,
H5,2,
H1,2,
H1,4,
H2,4,
//5
H3,8,
M3,2,
M5,2,
H1,2,
H3,2,
//6
H3,6,
H4,2,
H2,8,
//7
H2,4,
H5,2,
M7,2,
H1,8,
//8
H3,2,
H4,2,
H5,2,
H1,2,
H1,4,
H2,4,
//9
H1,4,
M2,2,
M4,2,
M3,2,
M3,2, //小学篱
M4,2,
M5,2,
//10
M5,2,
M5,2,
M5,2,
M5,2,
M5,2,
M6,2,
M7,2,
H1,2, //笆旁的蒲公英
//11
H1,8,
H1,2,
M7,2,
H2,2,
M6,2, //是记忆里
//12
M6,2,
M5,2,
M5,2,
H2,2,
H2,2,
H1,2,
H3,4, //有味道的风
//13
H3,8,
H1,2,
H2,2,
H3,2,
H3,2, //景 午睡操场
//14
H3,2,
H3,2,
H4,2,
H3,2,
H3,2,
H2,2,
H3,2,
H1,2, //传来蝉的声音
//14
H1,8,
M6,2,
M7,2,
H1,2,
H2,2, //多少年后
//15
H2,2,
H1,2,
H1,2,
M6,2,
M6,2,
H3,2,
H3,2,
H2,2, //也还是很好听
//16
H2,8,
P0,2,
M3,2,
M4,2,
M5,2, //将愿望
//17
M5,2,
M5,2,
M5,2,
M5,2,
M5,2,
M6,2,
M7,2,
M5,1,
H2,1, //折纸飞机寄成
//18
H1,8,
H1,2,
M7,2,
H2,2,
M5,1,
M6,1, //信 因为我们
//19
M6,2,
M5,2,
M5,2,
H2,2,
H2,2,
H1,2,
H3,3,
H3,1, //等不到那流星
//20
H3,8,
H1,2,
H2,2,
H3,2,
H3,2, //认真投决
//21
H3,2,
H3,2,
H4,2,
H3,2,
H3,2,
H2,2,
H3,2,
H2,2, //定命运的硬币
//22
H1,8,
M6,2,
M7,2,
H1,2,
H2,2, //却不知道
//23
H2,2,
H1,2,
H1,2,
M6,2,
M6,2,
H3,2,
H3,2,
H2,2, //到底能去哪里
//24
H2,8,
M3,2,
M5,2,
H1,2,
H3,2, //一起长大
//25
H3,6,
H4,2,
H2,8, //的约定
//26
H2,3,
H5,2,
M7,2,
H1,8, //那样清晰
//27
H3,2,
H4,2,
H5,2,
H1,2,
H1,4,
H2,2,
H3,1,
H3,1, //打过勾的我相信
//28
H3,8,
M3,2,
M5,2,
H1,2,
H3,1, //说好要一
//29
H3,6,
H4,2,
H2,8, //起旅行
//30
H2,4,
H5,2,
M7,2,
H1,8, //是你如今
//31
H3,2,
H4,2,
H5,2,
H1,2,
H1,4,
H2,2,
H1,2, //唯一坚持的任
//32
H1,8, //性
0xFF
//终止标志
};
unsigned int FreqSelect,MusicSelect;
void main()
{
Timer0_Init();
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;
}
}