📄 ex5-4.c
字号:
/*
标题:范例 5-4
版本:1.0
Target:89S51
程序描述:这个程序利用89S51演奏生日快乐的歌曲 */
/* ***************************************************** */
#include <REGX51.H>
void delay(unsigned char n); //函数delay()存在 文件delay.c
code unsigned song[]={
0xfc,0xd1,0xc1,0xb1,0xc1,0xd4,0xc1,0xd1,0xf1,0xc1,0xd6,0xc1,0xd1, 0xcc,0x01,0xc1,0xb1,0x91,0x81, 0x94,0x81,0x91,0xb1,0x81,0x98,
0x86,0x62,0x84,0xb2,0xc2,0xd6,0xc1,0xd1,0xf6,0xd2, 0xc6,0xc2,0x01,0xc2,0xc2,0xb2,0x92,0xb8,0x41,0x51,0x61,0x51,0x61,0x81,0x41,0x51,
0x62,0xd4,0x01,0xd2,0x01,0xd8, 0xc2,0xc1,0xd1,0xd2,0xc2,0xb4,0x92,0x82, 0x66,0x52,0x42,0xb2,0xb2,0x92, 0x88,0x01,0x81,0x61,0x84,0x01,0x82,
0x92,0xb2,0xb2,0x92,0xb4,0xc1,0xb1,0x91,0x81, 0x62,0x82,0x82,0x81,0x91,0x84,0x42,0x52, 0x62,0x82,0x82,0xb2,0x92,0x82,0x62,0x52, 0x46,0xb2,0x92,0x82,0x61,0x51,0x42,
0x56,0x82,0x82,0x62,0x62,0x52, 0x42,0x42,0x52,0x62,0x86,0x01,0x82, 0x62,0x82,0x82,0xb2,0xb2,0xc2,0xc2,0xc2,0xc1,0xd1, 0xcf,
0xb6,0xc2,0xd4,0xc2,0xb2,0x92,0xb2,0xb2,0x92,0x84,0x64,0x01, 0x66,0x82,0x81,0x91,0x82,0x61,0x51,0x42, 0x5c,0x62,0x82,
0x86,0x92,0x82,0x62,0x84,0x92,0xb2,0xb2,0xc2,0xd8, 0xc6,0x01,0xc2,0xc2,0xc2,0xb2,0x91,0xb1, 0xbf,0x01, 0xbf,0x00};
code int note[]={
0x0000,0xFB03,0xFB8E,0xFC0B,0xFC43,0xFCAB,0xFD08,0xFD32,
0xFD81,0xFDC7,0xFE05,0xFE21,0xFE55,0xFE83,0xFE99,0xFEC0};
unsigned char i=0;
unsigned char hi_note,low_note;
void Delay(unsigned int a)
{
unsigned char i;
while(a--)
for(i=0;i<125;i++);
}
static void timer1_isr(void) interrupt TF1_VECTOR using 2
{
TR1=0;
TL1=low_note;
TH1=hi_note;
TR1=1;
P0_5=~P0_5;
}
static void timer1_initialize(void)
{
EA=0;
TR1=0;
TMOD = 0X10;
ET1=1;
EA=1;
}
void singing()
{
unsigned int beat,temp;
i=0;
do {
temp=song[i]; // 读出乐谱的一个byte
if (temp==0) { // 如果是0就表示音乐结束
TR1=0; // 停止计时计数器1
return; // 返回
}
beat=temp & 0x0f; //取出低阶的4位,这是拍子
temp=(temp >> 4) & 0x0f; //取出高阶4位当成音符的频率
if (temp==0) TR1=0; //如果拍子是0就表示休止符
else {
hi_note=note[temp] >> 8; //根据音符的频率得到Timer1计数值
low_note=note[temp] & 0x00FF;
TR1=1; //启动计时计数器1
}
Delay(140*beat); // 延迟拍子的时间
i++;
} while(1);
}
void main (void) {
timer1_initialize();
do {
singing();
} while (1); // 无穷循环
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -