您的位置 首页 芯闻

#单片机#歌曲——世上只要妈妈

includereg52h>defineucharunsignedchardefineuintunsignedintsbitk3=P3^5;sbitsound=P0^4;bitFY=0;放乐曲时

#include

#define uchar unsigned char
#define uint unsigned int
sbit k3=P3^5;
sbit sound=P0^4;
bit FY=0; //放乐曲时FY=1, 电子琴演奏时FY=0
uchar Song_Index=0,Tone_Index=0; //放音乐的参数
uchar k, key,flag;
uchar code DSY_CODE[]= {0xc0,0xf9,0xa4,0xb0,
0x99,0x92,0x82,0xf8,
0x80,0x90,0x88,0x83,
0xc6,0xa1,0x86,0x8e};
uchar code Song[]= //世上只要妈妈好的旋律
{13,12,10,12,15,13,12,13,10,12,13,12,10,8,1,12,10,9,9,10,12,12,13,10,9,8,12,10,9,8,6,8,5,-1};
char code Len[]= //旋律每个音符对应
{3,1,2,2,2,1,1,4,2,1,1,2,2,1,1,1,1,4,3,1,2,1,1,2,2,4,3,1,1,1,1,1,6,-1};
uint code tab[]= {0,63628,63835,64021,64103,64260,64400,64524, //0,低1,低2,低3,低4,低5,低6,
64580,64684,64777,64820,64898,64968,65030, //中1,中2,中3,中4,中5,中6,中7
65058,65110,65157,65178,65217,65252,65283 }; //高1,高2,高3,高4,高5,高6,高7
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x–)
for(y=114;y>0;y–);
}
void main()
{
TMOD=0x01;//设置定时器0工作方式1
TH0=(tab[0])/256;
TL0=(tab[0])%6;//赋初值
EA=1;//开总中止
ET0=1;//开定时器中止0
P2=0X00;
while(1)
{
Tone_Index=0;
while(Song[Tone_Index]!=-1)
{
P0=DSY_CODE;
TH0=(tab[Song[Tone_Index]])/256;
TL0=(tab[Song[Tone_Index]])%6;
TR0 = 1;
delay(250*Len[Tone_Index]);
Tone_Index ;
TR0=0;
}
}
}
void timer0() interrupt 1
{
TH0=(tab[Song[Tone_Index]])/256;
TL0=(tab[Song[Tone_Index]])%6;
sound=!sound;
}

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

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

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

微信扫一扫关注我们

返回顶部