您的位置 首页 芯闻

89S51演奏生日快乐的歌曲c程序

程序描述:这个程序利用89S51演奏生日快乐的歌曲*//*******************************************************/#includeREGX51.H

程序描绘:这个程序使用89S51演奏生日快乐的歌曲 */

/* ***************************************************** */

#include

void delay(unsigned char n); //函数delay()存在 文件delay.c

code unsigned song[]={

0x82,0x01,0x81,0x94,0x84,0xB4,0xA4,0x04,0x82,0x01,0x81,

0x94,0x84,0xC4,0xB4,0x04, 0x82,0x01,0x81,0xF4,0xD4,0xB4,

0xA4,0x94,0xE2,0x01,0xE1,0xD4,0xB4,0xC4,0xB4,0x04,

0x82,0x01,0x81,0x94,0x84,0xB4,0xA4,0x04,0x82,0x01,0x81,

0x94,0x84,0xC4,0xB4,0x04, 0x82,0x01,0x81,0xF4,0xD4,0xB4,

0xA4,0x94,0xE2,0x01,0xE1,0xD4,0xB4,0xC4,0xB4,0x04,

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;

static void timer1_isr(void) interrupt TF1_VECTOR using 2

{

TR1=0;

TL1=low_note;

TH1=hi_note;

TR1=1;

P3_7=3_7;

}

static void timer1_initialize(void)

{

EA=0;

TR1=0;

TMOD = 0X10;

ET1=1;

EA=1;

}

void singing()

{

unsigned char 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(beat); // 推迟拍子的时刻

i++;

} while(1);

}

void main (void) {

timer1_initialize();

do {

singing();

} while (1); // 无量循环

}

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/news/xinwen/266134.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部