}
#ifndef _SINGING_H_
#define _SINGING_H_
unsigned char T0TH = 0; //T0定时器重载值,用以控制频率
unsigned char T0TL = 0;
/********************************************
频率初值表
*********************************************/
unsigned char code T0ChuZhi[]={
0xFF,0xFF, // low mid high
0xFC,0x44, //1 ## 5 6 7 1 2 3 4 5 6 7 1 2 3
0xFC,0xAC, //2 ## D C B 1 2 3 4 5 6 7 8 9 A
0xFD,0x09, //3
0xFD,0x34, //4
0xFD,0x82, //5
0xFD,0xC8, //6
0xFE,0x06, //7
0xFE,0x22, //8 --high 1 N = 470;
0xFE,0x56, //9 --hign 2 N = 440;
0xFE,0x85, //A --hign 3 N = 410;
0xFC,0x0C, //B---low 7 N = 1012
0xFB,0x90, //C --low 6
0xFB,0x04, //D --low 5
0xFA,0x15 //E --low 3
};
//高4位为音调,低四位为节拍
/********************************************
歌曲(C调)
*********************************************/
unsigned char code SongChar[]={ //龙的传人
0x34, 0x14, 0x32, 0x32, 0x14,
0x32, 0x32, 0x52, 0x62, 0x54, 0x04,
0x62, 0x62, 0x62, 0x52, 0x42, 0x42, 0x44,
0x22, 0x32, 0x22, 0x12, 0x24, 0x04,
0x32, 0x02, 0x12, 0x02, 0x32, 0x02, 0x32, 0x02,
0x32, 0x32, 0x52, 0x62, 0x64, 0x04,
0x14, 0x52, 0x52, 0x64, 0x34,
0x22, 0x12, 0x22, 0x32, 0x54, 0x04,
0x14, 0x52, 0x52, 0x64, 0x34,
0x22, 0x12, 0x22, 0x32, 0x14, 0xff
};
/* 延时1ms,12T,12MHz */
void Delay1ms(unsigned int n) //@12.000MHz
{
unsigned char i, j;
while(n--)
{
i = 2;
j = 239;
do
{
while (--j);
} while (--i);
}
}
/* 延时125MS,即十六分音符 */
void Delay125ms(unsigned int n)
{
unsigned char i, j;
while(n--)
{
i = 244;
j = 45;
do
{
while (--j);
} while (--i);
}
}
/* 音乐演奏 */
void Singing()
{
int jiepai = 0; //节拍
int yinnum = 0; //音符计数,即第几个音符
unsigned char yindiao = 0; //音调
unsigned char yinmatemp = 0; //要处理的音码
TR0 = 0;
while(SysMod == 1) //循环播放
{
yinmatemp = SongChar[yinnum];
if(yinmatemp != 0xFF) //不是歌曲结尾
{
yindiao = yinmatemp >> 4; //取高四位,音调
if(yindiao != 0) //不是休止符
{
T0TH = T0ChuZhi[yindiao*2];
T0TL = T0ChuZhi[yindiao*2+1];
TR0 = 1;
}
else //是休止符
{
TR0 = 0;
}
jiepai = yinmatemp & 0x0F; //取低四位,节拍
Delay125ms(jiepai);
TR0 = 0;
Delay1ms(10);
yinnum++;
}
else //是歌曲结尾
{
TR0 = 0;
yinnum = 0;
Delay1ms(3000);
}
}
}
#endif