![](https://iknow-base.cdn.bcebos.com/lxb/notice.png)
1个回答
2013-07-20
展开全部
这是我学校电子设计时做的,可以弹琴,也有几首歌在里面。。你应该看得懂#include <reg52.h> #define uchar unsigned char
#define uint unsigned int
#define ulong unsigned long
sbit BEEP=P2^0; //喇叭输出脚uchar th0_f; //在中断中装载的T0的值高8位
uchar tl0_f; //在中断中装载的T0的值低8位
uchar key;/*------------------------------------------------
函数声明
------------------------------------------------*/
uchar keyscan(void);//键盘扫描程序
void delay(uint i); //延时子程序/*-------- T0的值,及输出频率对照表 --------------*/
uchar code freq[36*2]={
0xA9,0xEF, //0x3F,0XEE,//00220HZ ,1 //0
0x93,0xF0, //0X3D,0XEF,//00233HZ ,1#
0x73,0xF1, //0X30,0XF0,//00247HZ ,2
0x49,0xF2, //0X18,0XF1,//00262HZ ,2#
0x07,0xF3, //0XE6,0XF1,//00277HZ ,3
0xC8,0xF3, //0XB7,0XF2,//00294HZ ,4
0x73,0xF4, //0X71,0XF3,//00311HZ ,4#
0x1E,0xF5, //0X2A,0XF4,//00330HZ ,5
0xB6,0xF5, //0XCF,0XF4,//00349HZ ,5#
0x4C,0xF6, //0X72,0XF5,//00370HZ ,6
0xD7,0xF6, //0X09,0XF6,//00392HZ ,6#
0x5A,0xF7, //0X97,0XF6,//00415HZ ,7
0xD8,0xF7, //0X20,0XF7,//00440HZ 1 //12
0x4D,0xF8, //0X9F,0XF7,//00466HZ 1# //13
0xBD,0xF8, //0X18,0XF8,//00494HZ 2 //14
0x24,0xF9, //0X88,0XF8,//00523HZ 2# //15
0x87,0xF9, //0XF3,0XF8,//00554HZ 3 //16
0xE4,0xF9, //0X59,0XF9,//00587HZ 4 //17
0x3D,0xFA, //0X05,0XFA,//00622HZ 4# //18
0x90,0xFA, //0X13,0XFA,//00659HZ 5 //19
0xDE,0xFA, //0X66,0XFA,//00698HZ 5# //20
0x29,0xFB, //0XB9,0XFA,//00740HZ 6 //21
0x6F,0xFB, //0X05,0XFB,//00784HZ 6# //22
0xB1,0xFB, //0X4D,0XFB,//00831HZ 7 //23
0xEF,0xFB, //0X90,0XFB,//00880HZ `1
0x2A,0xFC, //0XD0,0XFB,//00932HZ `1#
0x62,0xFC, //0X0C,0XFC,//00988HZ `2
0x95,0xFC, //0X44,0XFC,//01046HZ `2#
0xC7,0xFC, //0X7B,0XFC,//01109HZ `3
0xF6,0xFC, //0XAD,0XFC,//01175HZ `4
0x22,0xFD, //0XDD,0XFC,//01244HZ `4#
0x4B,0xFD, //0X0A,0XFD,//01318HZ `5
0x73,0xFD, //0X35,0XFD,//01397HZ `5#
0x98,0xFD, //0X5D,0XFD,//01480HZ `6
0xBB,0xFD, //0X83,0XFD,//01568HZ `6#
0xDC,0xFD, //0XA6,0XFD,//01661HZ `7 //35
};//定时中断0,用于产生唱歌频率
timer0() interrupt 1
{
TL0=tl0_f;TH0=th0_f; //调入预定时值
BEEP=~BEEP; //取反音乐输出IO
}//音乐符号串解释函数
//入口:要解释的音乐符号串,输出的音调串,输出的时长串
changedata(uchar *song,uchar *diao,uchar *jie)
{
uchar i,i1,j;
char gaodi; //高低+/-12音阶
uchar banyin;//有没有半个升音阶
uchar yinchang;//音长
uchar code jie7[8]={0,12,14,16,17,19,21,23}; //C调的7个值
*diao=*song;
for(i=0,i1=0;;)
{
gaodi=0; //高低=0
banyin=0;//半音=0
yinchang=4;//音长1拍
if((*(song+i)=='|') || (*(song+i)==' ')) i++;
//拍子间隔和一个空格过滤
switch(*(song+i))
{
case ',': gaodi=-12;i++;//低音
break;
case '`': gaodi=12;i++; //高音
break;
}
if(*(song+i)==0) //遇到0结束
{
*(diao+i1)=0; //加入结束标志0
*(jie+i1)=0;
return;
}
j=*(song+i)-0x30; i++; //取出基准音
j=jie7[j]+gaodi; //加上高低音
yinc: switch(*(song+i))
{
case '#': //有半音j加一个音阶
i++;j++;
goto yinc;
case '-': //有一个音节加长
yinchang+=4;
i++;
goto yinc;
case '_': //有一个音节缩短
yinchang/=2;
i++;
goto yinc;
case '.': //有一个加半拍
yinchang=yinchang+yinchang/2;
i++;
goto yinc;
}
*(diao+i1)=j; //记录音符
*(jie+i1)=yinchang; //记录音长
i1++;
}
}//奏乐函数
//入口:要演奏的音乐符号串
void play(uchar *songdata)
{
uchar i,c,j=0;
uint n;
uchar diaodata[48]; //音调缓冲
uchar jiedata[48]; //音长缓冲
changedata(songdata,diaodata,jiedata); //解释音乐符号串
TR0=1;
for(i=0;diaodata[i]!=0;i++) //逐个符号演奏
{
tl0_f=freq[diaodata[i]*2]; //取出对应的定时值送给T0
th0_f=freq[diaodata[i]*2+1];
for(c=0;c<jiedata[i];c++) //按照音长延时
{ key = keyscan();
for(n=0;n<29500;n++); // 29500
if(key != 0xff)//((!K1)||(!K2)||(!K3)||(!K4))//发现按键,立即退出播放
{
TR0=0;
return;
}
}
TR0=0;
for(n=0;n<460;n++); //460音符间延时
TR0=1;
}
TR0=0;
}//一分钱
uchar code yifenqian[]={
"5`1|6_`1_5|3_5_2_3_|5-|"
"3_5_6_`1_|5_6_5_3_|1.3__|2-|"
"3_2_1_2_|3-|6_5_3_5_|6-|"
"5_`1_6_5_|3_5_2|5_2_3_2_|1-|"
}; //世上只有妈妈好
uchar code mamahao[]={
"6.5_35|`16_5_6-|35_6_53_2_|1_,6_5_3_2-|"
"2.3_55_6_|321-|5.3_2_1_,6_1_|,5--"
};
//找朋友
uchar code zhaopengyou[]={ "5_6_5_6_|5_6_5|5_`1_7_6_|5 3|"
"5_5_3_4_|5_5_3|1_4_3_2_|1_2_1|"
};
//茉莉花
uchar code molihua[]={"33_5_6_`1_`1_6_|55_6_5-|33_5_6_`1_`1_6_|55_6_5-|"
"5553_5_|665-|32_3_53_2_|11_2_1|"
//"3_2_1_3_2.3_|56_`1_5-|23_5_2_3_1_,6_|,5-,61|"
//"2.3_1_2-1_,6_|,5--"
};
//新年好
uchar code xinnianhao[]={
"1_1_1 ,5|3_3_3 1|1_3_5 5|4_3_2-|"
"2_3_4-|3_2_3 1|1_3_2 ,5|,7_2_1-|"
};
//小星星
uchar code xingxing[]={
"1155|665-|4433|221-|"
"5544|332-|5544|332-|"
"1155|665-|4433|221-|"
};
//外婆的澎湖湾
uchar code waipodephw[]={
"3_5_5_5_6_`1_6_5_|`1_`1_`1_6_5-|"
"`3_`3_`3_`3_`4_`3_`2_`1_|`2_`2_`2_`3_`2-|"
"`3_`3_`3_`3_`4_`3_`2_`1_|6_`1_`1_6_5-|"
};
}
#define uint unsigned int
#define ulong unsigned long
sbit BEEP=P2^0; //喇叭输出脚uchar th0_f; //在中断中装载的T0的值高8位
uchar tl0_f; //在中断中装载的T0的值低8位
uchar key;/*------------------------------------------------
函数声明
------------------------------------------------*/
uchar keyscan(void);//键盘扫描程序
void delay(uint i); //延时子程序/*-------- T0的值,及输出频率对照表 --------------*/
uchar code freq[36*2]={
0xA9,0xEF, //0x3F,0XEE,//00220HZ ,1 //0
0x93,0xF0, //0X3D,0XEF,//00233HZ ,1#
0x73,0xF1, //0X30,0XF0,//00247HZ ,2
0x49,0xF2, //0X18,0XF1,//00262HZ ,2#
0x07,0xF3, //0XE6,0XF1,//00277HZ ,3
0xC8,0xF3, //0XB7,0XF2,//00294HZ ,4
0x73,0xF4, //0X71,0XF3,//00311HZ ,4#
0x1E,0xF5, //0X2A,0XF4,//00330HZ ,5
0xB6,0xF5, //0XCF,0XF4,//00349HZ ,5#
0x4C,0xF6, //0X72,0XF5,//00370HZ ,6
0xD7,0xF6, //0X09,0XF6,//00392HZ ,6#
0x5A,0xF7, //0X97,0XF6,//00415HZ ,7
0xD8,0xF7, //0X20,0XF7,//00440HZ 1 //12
0x4D,0xF8, //0X9F,0XF7,//00466HZ 1# //13
0xBD,0xF8, //0X18,0XF8,//00494HZ 2 //14
0x24,0xF9, //0X88,0XF8,//00523HZ 2# //15
0x87,0xF9, //0XF3,0XF8,//00554HZ 3 //16
0xE4,0xF9, //0X59,0XF9,//00587HZ 4 //17
0x3D,0xFA, //0X05,0XFA,//00622HZ 4# //18
0x90,0xFA, //0X13,0XFA,//00659HZ 5 //19
0xDE,0xFA, //0X66,0XFA,//00698HZ 5# //20
0x29,0xFB, //0XB9,0XFA,//00740HZ 6 //21
0x6F,0xFB, //0X05,0XFB,//00784HZ 6# //22
0xB1,0xFB, //0X4D,0XFB,//00831HZ 7 //23
0xEF,0xFB, //0X90,0XFB,//00880HZ `1
0x2A,0xFC, //0XD0,0XFB,//00932HZ `1#
0x62,0xFC, //0X0C,0XFC,//00988HZ `2
0x95,0xFC, //0X44,0XFC,//01046HZ `2#
0xC7,0xFC, //0X7B,0XFC,//01109HZ `3
0xF6,0xFC, //0XAD,0XFC,//01175HZ `4
0x22,0xFD, //0XDD,0XFC,//01244HZ `4#
0x4B,0xFD, //0X0A,0XFD,//01318HZ `5
0x73,0xFD, //0X35,0XFD,//01397HZ `5#
0x98,0xFD, //0X5D,0XFD,//01480HZ `6
0xBB,0xFD, //0X83,0XFD,//01568HZ `6#
0xDC,0xFD, //0XA6,0XFD,//01661HZ `7 //35
};//定时中断0,用于产生唱歌频率
timer0() interrupt 1
{
TL0=tl0_f;TH0=th0_f; //调入预定时值
BEEP=~BEEP; //取反音乐输出IO
}//音乐符号串解释函数
//入口:要解释的音乐符号串,输出的音调串,输出的时长串
changedata(uchar *song,uchar *diao,uchar *jie)
{
uchar i,i1,j;
char gaodi; //高低+/-12音阶
uchar banyin;//有没有半个升音阶
uchar yinchang;//音长
uchar code jie7[8]={0,12,14,16,17,19,21,23}; //C调的7个值
*diao=*song;
for(i=0,i1=0;;)
{
gaodi=0; //高低=0
banyin=0;//半音=0
yinchang=4;//音长1拍
if((*(song+i)=='|') || (*(song+i)==' ')) i++;
//拍子间隔和一个空格过滤
switch(*(song+i))
{
case ',': gaodi=-12;i++;//低音
break;
case '`': gaodi=12;i++; //高音
break;
}
if(*(song+i)==0) //遇到0结束
{
*(diao+i1)=0; //加入结束标志0
*(jie+i1)=0;
return;
}
j=*(song+i)-0x30; i++; //取出基准音
j=jie7[j]+gaodi; //加上高低音
yinc: switch(*(song+i))
{
case '#': //有半音j加一个音阶
i++;j++;
goto yinc;
case '-': //有一个音节加长
yinchang+=4;
i++;
goto yinc;
case '_': //有一个音节缩短
yinchang/=2;
i++;
goto yinc;
case '.': //有一个加半拍
yinchang=yinchang+yinchang/2;
i++;
goto yinc;
}
*(diao+i1)=j; //记录音符
*(jie+i1)=yinchang; //记录音长
i1++;
}
}//奏乐函数
//入口:要演奏的音乐符号串
void play(uchar *songdata)
{
uchar i,c,j=0;
uint n;
uchar diaodata[48]; //音调缓冲
uchar jiedata[48]; //音长缓冲
changedata(songdata,diaodata,jiedata); //解释音乐符号串
TR0=1;
for(i=0;diaodata[i]!=0;i++) //逐个符号演奏
{
tl0_f=freq[diaodata[i]*2]; //取出对应的定时值送给T0
th0_f=freq[diaodata[i]*2+1];
for(c=0;c<jiedata[i];c++) //按照音长延时
{ key = keyscan();
for(n=0;n<29500;n++); // 29500
if(key != 0xff)//((!K1)||(!K2)||(!K3)||(!K4))//发现按键,立即退出播放
{
TR0=0;
return;
}
}
TR0=0;
for(n=0;n<460;n++); //460音符间延时
TR0=1;
}
TR0=0;
}//一分钱
uchar code yifenqian[]={
"5`1|6_`1_5|3_5_2_3_|5-|"
"3_5_6_`1_|5_6_5_3_|1.3__|2-|"
"3_2_1_2_|3-|6_5_3_5_|6-|"
"5_`1_6_5_|3_5_2|5_2_3_2_|1-|"
}; //世上只有妈妈好
uchar code mamahao[]={
"6.5_35|`16_5_6-|35_6_53_2_|1_,6_5_3_2-|"
"2.3_55_6_|321-|5.3_2_1_,6_1_|,5--"
};
//找朋友
uchar code zhaopengyou[]={ "5_6_5_6_|5_6_5|5_`1_7_6_|5 3|"
"5_5_3_4_|5_5_3|1_4_3_2_|1_2_1|"
};
//茉莉花
uchar code molihua[]={"33_5_6_`1_`1_6_|55_6_5-|33_5_6_`1_`1_6_|55_6_5-|"
"5553_5_|665-|32_3_53_2_|11_2_1|"
//"3_2_1_3_2.3_|56_`1_5-|23_5_2_3_1_,6_|,5-,61|"
//"2.3_1_2-1_,6_|,5--"
};
//新年好
uchar code xinnianhao[]={
"1_1_1 ,5|3_3_3 1|1_3_5 5|4_3_2-|"
"2_3_4-|3_2_3 1|1_3_2 ,5|,7_2_1-|"
};
//小星星
uchar code xingxing[]={
"1155|665-|4433|221-|"
"5544|332-|5544|332-|"
"1155|665-|4433|221-|"
};
//外婆的澎湖湾
uchar code waipodephw[]={
"3_5_5_5_6_`1_6_5_|`1_`1_`1_6_5-|"
"`3_`3_`3_`3_`4_`3_`2_`1_|`2_`2_`2_`3_`2-|"
"`3_`3_`3_`3_`4_`3_`2_`1_|6_`1_`1_6_5-|"
};
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |