#include
#define uchar unsigned
char //定义一下方便使用
#define uint unsigned int
#define ulong unsigned
long
sbit BEEP="P1"^7; //喇叭输出脚
uchar th0_f; //在中断中装载的 T0
的值高 8 位
uchar tl0_f; //在中断中装载的 T0 的值低 8 位
//T0
的值,及输出频率对照表,前面为低字节,后面为高字节
uchar code freq[36*2]={
0xA9,0xEF,//00220HZ
,1 //0
0x93,0xF0,//00233HZ ,1#
0x73,0xF1,//00247HZ ,2
0x49,0xF2,//00262HZ ,2#
0x07,0xF3,//00277HZ ,3
0xC8,0xF3,//00294HZ ,4 //低音部分
0x73,0xF4,//00311HZ ,4#
0x1E,0xF5,//00330HZ ,5
0xB6,0xF5,//00349HZ ,5#
0x4C,0xF6,//00370HZ ,6
0xD7,0xF6,//00392HZ ,6#
0x5A,0xF7,//00415HZ ,7
0xD8,0xF7,//00440HZ 1 //12
0x4D,0xF8,//00466HZ 1# //13
0xBD,0xF8,//00494HZ 2 //14
0x24,0xF9,//00523HZ 2# //15
0x87,0xF9,//00554HZ 3 //16
0xE4,0xF9,//00587HZ 4 //17
0x3D,0xFA,//00622HZ 4# //18
0x90,0xFA,//00659HZ 5 //19
0xDE,0xFA,//00698HZ 5# //20
0x29,0xFB,//00740HZ 6 //21
0x6F,0xFB,//00784HZ 6# //22
0xB1,0xFB,//00831HZ 7 //23
0xEF,0xFB,//00880HZ `1
0x2A,0xFC,//00932HZ `1#
0x62,0xFC,//00988HZ `2
0x95,0xFC,//01046HZ `2#
0xC7,0xFC,//01109HZ `3
0xF6,0xFC,//01175HZ `4 //高音部分
0x22,0xFD,//01244HZ `4#
0x4B,0xFD,//01318HZ `5
0x73,0xFD,//01397HZ `5#
0x98,0xFD,//01480HZ `6
0xBB,0xFD,//01568HZ `6#
0xDC,0xFD,//01661HZ `7 //35
};
//按照约定格式输入乐谱,同时应注意字符串保存的格式为ASCII码,到时需要转换
uchar
xianjian[]={
" "
};
//定时中断 0,用于产生唱歌频率
timer0()
interrupt 1
{
TL0=tl0_f;TH0=th0_f; //调入预定时值
BEEP=~BEEP; //取反
P2=~P2;
}
changedata(uchar *song,uchar
*diao,uchar *jie)
{
uchar i,i1,j;
i=i1=0;
char gaodi;
uchar
banyin;
uchar yinchang;
uchar
jie7[8]={0,12,14,16,17,19,21,23};
*diao=*song;
while(1)
{
gaodi=0;
banyin=0;
yinchang=4;
if(*(song+i)=='|'||(*(song+i)=='
'))
i++;
switch(*(song+i))
{
case',':gaodi=-12;i++;
break;
case'`':gaodi=12;i++;
break;
}
if(*(song+i)==0)//乐谱结束
{
*(diao+i1)=0;
*(jie+i1)=0;
}
j=*(song+i)-0x30;
i++;//是否自加有待考虑
j=jie7[j]+gaodi;
//yinc:
switch(*(song+i))
{
case '#':
i++;j++;
//goto
yinc;
break;
case
'-':
yinchang+=4;
i++;
break;
case
'_':
yinchang/=2;
i++;
//goto
yinc;
break;
case
'.':
yinchang=yingchang+yingchang/2;
i++;
break;
}
*(diao+i1)=j;
*(jie+i1)=yinchang;
i1++;
}
}
//音阶声音自动输出试验
void paly(unsigned char *songdata)
{
uchar
i,c,j=0;
uint n;
uchar diaodata[112];//用于解读乐谱后的频率保存
uchar
jiedata[112];//用于解读乐谱后的音节保存
changedata(songdata,diaodata,jiedata);
TR0=1;
for(i=0;diaodata[i]!=0;i++)
{
tl0_f=freq[diaodata[i]*2];
th0_f=freq[diaodata[i]*2+1];
for(c=0;c
for(n=0;n<32000;n++);
TR0=0;
for(n=0;n<500;n++);
TR0=1;
}
TR0=0;
}
void
main(void) // 主程序
{
TMOD=0x01;
TR0=1;
ET0=1;
EA=1;
while(1)
{
play(xianjian);
}
}