一个独立按键实现四首歌循坏播放
单片机源程序如下:
- /************************************************************************
- [文件名] C51音乐程序(八月桂花)
- [功能] 通过单片机演奏音乐
-
- /**********************************************************************/
- #include <REG52.H>
- #include <INTRINS.H>
- //本例采用89C52, 晶振为12MHZ
- //关于如何编制音乐代码, 其实十分简单,各位可以看以下代码.
- //频率常数即音乐术语中的音调,而节拍常数即音乐术语中的多少拍;
- //所以拿出谱子, 试探编吧!
- #define uint unsigned int
- #define uchar unsigned char
- sbit Beep = P3^1;
- sbit s1=P1^0; //流水灯顺时针流水
- uchar code discode[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90}; //送段码0-9和"-"
- unsigned char n=0; //n为节拍常数变量
- void ini(void)
- {
- TMOD&=0x0f;
- TMOD|=0x01;
- TH0=0xd8;TL0=0xef;
- IE=0x82;
- }
- void delay (unsigned char m) //控制频率延时
- {
- unsigned i=3*m;
- while(--i);
- }
- void delay1(unsigned char yj)
- {
- uchar yj2=2;
- while(yj!=0)
- {
- while(yj2!=0)
- {
- yj2--;
- }
- yj2=2;
- yj--;
- }
-
- }
- void delayms(unsigned char a) //豪秒延时子程序
- {
- while(--a); //采用while(--a) 不要采用while(a--); 各位可编译一下看看汇编结果就知道了!
- }
- void shuma()
- {
- while(s1==1)
- {
- P0=0X8C;
- P2=0X01;
- }
- }
- uchar code music_tab[] ={
- 0x18, 0x30, 0x1C , 0x10, //格式为: 频率常数, 节拍常数, 频率常数, 节拍常数,
- 0x20, 0x40, 0x1C , 0x10,
- 0x18, 0x10, 0x20 , 0x10,
- 0x1C, 0x10, 0x18 , 0x40,
- 0x1C, 0x20, 0x20 , 0x20,
- 0x1C, 0x20, 0x18 , 0x20,
- 0x20, 0x80, 0xFF , 0x20,
- 0x30, 0x1C, 0x10 , 0x18,
- 0x20, 0x15, 0x20 , 0x1C,
- 0x20, 0x20, 0x20 , 0x26,
- 0x40, 0x20, 0x20 , 0x2B,
- 0x20, 0x26, 0x20 , 0x20,
- 0x20, 0x30, 0x80 , 0xFF,
- 0x20, 0x20, 0x1C , 0x10,
- 0x18, 0x10, 0x20 , 0x20,
- 0x26, 0x20, 0x2B , 0x20,
- 0x30, 0x20, 0x2B , 0x40,
- 0x20, 0x20, 0x1C , 0x10,
- 0x18, 0x10, 0x20 , 0x20,
- 0x26, 0x20, 0x2B , 0x20,
- 0x30, 0x20, 0x2B , 0x40,
- 0x20, 0x30, 0x1C , 0x10,
- 0x18, 0x20, 0x15 , 0x20,
- 0x1C, 0x20, 0x20 , 0x20,
- 0x26, 0x40, 0x20 , 0x20,
- 0x2B, 0x20, 0x26 , 0x20,
- 0x20, 0x20, 0x30 , 0x80,
- 0x20, 0x30, 0x1C , 0x10,
- 0x20, 0x10, 0x1C , 0x10,
- 0x20, 0x20, 0x26 , 0x20,
- 0x2B, 0x20, 0x30 , 0x20,
- 0x2B, 0x40, 0x20 , 0x15,
- 0x1F, 0x05, 0x20 , 0x10,
- 0x1C, 0x10, 0x20 , 0x20,
- 0x26, 0x20, 0x2B , 0x20,
- 0x30, 0x20, 0x2B , 0x40,
- 0x20, 0x30, 0x1C , 0x10,
- 0x18, 0x20, 0x15 , 0x20,
- 0x1C, 0x20, 0x20 , 0x20,
- 0x26, 0x40, 0x20 , 0x20,
- 0x2B, 0x20, 0x26 , 0x20,
- 0x20, 0x20, 0x30 , 0x30,
- 0x20, 0x30, 0x1C , 0x10,
- 0x18, 0x40, 0x1C , 0x20,
- 0x20, 0x20, 0x26 , 0x40,
- 0x13, 0x60, 0x18 , 0x20,
- 0x15, 0x40, 0x13 , 0x40,
- 0x18, 0x80, 0x00 , 0x00
- };
- //挥着翅膀的女孩
- uchar code music_tab1[]={ 0x26,0x20,0x20,0x20,0x20,0x20,0x26,0x10,0x20,0x10,0x20,0x80,0x26,0x20,0x30,0x20,
- 0x30,0x20,0x39,0x10,0x30,0x10,0x30,0x80,0x26,0x20,0x20,0x20,0x20,0x20,0x1c,0x20,
- 0x20,0x80,0x2b,0x20,0x26,0x20,0x20,0x20,0x2b,0x10,0x26,0x10,0x2b,0x80,0x26,0x20,
- 0x30,0x20,0x30,0x20,0x39,0x10,0x26,0x10,0x26,0x60,0x40,0x10,0x39,0x10,0x26,0x20,
- 0x30,0x20,0x30,0x20,0x39,0x10,0x26,0x10,0x26,0x80,0x26,0x20,0x2b,0x10,0x2b,0x10,
- 0x2b,0x20,0x30,0x10,0x39,0x10,0x26,0x10,0x2b,0x10,0x2b,0x20,0x2b,0x40,0x40,0x20,
- 0x20,0x10,0x20,0x10,0x2b,0x10,0x26,0x30,0x30,0x80,0x18,0x20,0x18,0x20,0x26,0x20,
- 0x20,0x20,0x20,0x40,0x26,0x20,0x2b,0x20,0x30,0x20,0x30,0x20,0x1c,0x20,0x20,0x20,
- 0x20,0x80,0x1c,0x20,0x1c,0x20,0x1c,0x20,0x30,0x20,0x30,0x60,0x39,0x10,0x30,0x10,
- 0x20,0x20,0x2b,0x10,0x26,0x10,0x2b,0x10,0x26,0x10,0x26,0x10,0x2b,0x10,0x2b,0x80,
- 0x18,0x20,0x18,0x20,0x26,0x20,0x20,0x20,0x20,0x60,0x26,0x10,0x2b,0x20,0x30,0x20,
- 0x30,0x20,0x1c,0x20,0x20,0x20,0x20,0x80,0x26,0x20,0x30,0x10,0x30,0x10,0x30,0x20,
- 0x39,0x20,0x26,0x10,0x2b,0x10,0x2b,0x20,0x2b,0x40,0x40,0x10,0x40,0x10,0x20,0x10,
- 0x20,0x10,0x2b,0x10,0x26,0x30,0x30,0x80,0x00
- };
- //两只蝴蝶
- uchar code music_tab2[] ={ 0x26,0x10,0x1c,0x10,0x1c,0x10,0x26,0x10,0x20,0x10,0x1c,0x10,0x20,0x10,
- 0x1c,0x10,0x26,0x10,0x20,0x10,0x1c,0x10,0x20,0x10,0x26,0x10,0x0e,0x10,
- 0x2b,0x10,0x2b,0x10,0x0e,0x10,0x30,0x10,0x2b,0x10,0x30,0x10,0x2b,0x10,
- 0x2b,0x10,0x0e,0x10,0x30,0x10,0x2b,0x10,0x30,0x10,0x0e,0x10,0x26,0x10,
- 0x1c,0x10,0x1c,0x10,0x26,0x10,0x20,0x10,0x1c,0x10,0x20,0x10,0x1c,0x10,
- 0x1c,0x10,0x26,0x10,0x20,0x10,0x1c,0x10,0x20,0x10,0x26,0x10,0x0e,0x10,
- 0x2b,0x10,0x2b,0x10,0x0e,0x10,0x30,0x10,0x2b,0x10,0x30,0x10,0x2b,0x10,
- 0x30,0x10,0x2b,0x10,0x26,0x10,0x1c,0x10,0x19,0x10,0x1c,0x10,0x20,0x10,
- 0x1c,0x10,0x1c,0x10,0x19,0x10,0x00
- };
- /******************生日·快乐***************/
- unsigned char code music_tab3[]={
- 0x20,0x20,0x20,0x20,0x1c,0x20,0x20,0x20,0x60,0x20,0x19,0x20,0x20,0x20,
- 0x20,0x20,0x1c,0x20,0x20,0x20,0x55,0x20,0x60,0x20,0x20,0x20,0x20,0x20,
- 0x40,0x20,0x4c,0x20,0x60,0x20,0x19,0x20,0x1c,0x20,0x48,0x20,0x48,0x20,
- 0x4c,0x20,0x60,0x20,0x55,0x20,0x60,0x20,0x20,0x20,0x20,0x20,0x00};
- //电影音乐僵尸中音乐盒的歌在单片机中的编码
- unsigned char code music_tab4[]={
- 0x40,0x20,0x39,0x20,0x40,0x20,0x48,0x20,0x4c,0x20,0x48,0x20,0x40,0x20,
- 0x55,0x20,0x4c,0x20,0x48,0x20,0x4c,0x20,0x48,0x20,0x40,0x20,0x40,0x20,
- 0x39,0x20,0x40,0x20,0x48,0x20,0x4c,0x20,0x48,0x20,0x40,0x20,0x55,0x20,
- 0x40,0x20,0x4c,0x20,0x60,0x20,0x00,
- };
-
- void gequ1()
- {
- uchar p,m; //m为频率常数变量
- uchar i=0;
- s1=P1^0;
- play:
- while(s1==1)
- {
- a: p=music_tab[i];
- if(p==0x00) { i=0; delayms(1000); goto play;} //如果碰到结束符,延时1秒,回到开始再来一遍
- else if(p==0xff) { i=i+1;delayms(100),TR0=0; goto a;} //若碰到休止符,延时100ms,继续取下一音符
- else {m=music_tab[i++], n=music_tab[i++];} //取频率常数 和 节拍常数
- TR0=1; //开定时器1
- while(n!=0) Beep=~Beep,delay(m); //等待节拍完成, 通过P1口输出音频(可多声道哦!)
- TR0=0; //关定时器1
- }
- }
- void gequ2()
- {
- uchar p,yj; //m为频率常数变量
- uchar i=0;
- s1=P1^0;
- play1:
- while(s1==1)
- {
- a1: p=music_tab1[i];
- if(p==0x00) { i=0, delayms(1000); goto play1;} //如果碰到结束符,延时1秒,回到开始再来一遍
- else if(p==0xff) { i=i+1;delayms(100),TR0=0; goto a1;} //若碰到休止符,延时100ms,继续取下一音符
- yj=music_tab1[i++], n=music_tab1[i++]; //取频率常数 和 节拍常数
- TR0=1; //开定时器1
- while(n!=0) Beep=~Beep,delay1(yj); //等待节拍完成, 通过P1口输出音频(可多声道哦!)
- TR0=0; //关定时器1
- }
- }
- void gequ3()
- {
- uchar p,yj; //m为频率常数变量
- uchar i=0;
- s1=P1^0;
- play2:
- while(s1==1)
- {
- a2: p=music_tab2[i];
- if(p==0x00) { i=0, delayms(1000); goto play2;} //如果碰到结束符,延时1秒,回到开始再来一遍
- else if(p==0xff) { i=i+1;delayms(100),TR0=0; goto a2;} //若碰到休止符,延时100ms,继续取下一音符
- else {yj=music_tab2[i++], n=music_tab2[i++];} //取频率常数 和 节拍常数
- TR0=1; //开定时器1
- while(n!=0) Beep=~Beep,delay1(yj); //等待节拍完成, 通过P1口输出音频(可多声道哦!)
- TR0=0; //关定时器1
- }
- }
- void gequ4()
- {
- uchar p,yj; //m为频率常数变量
- uchar i=0;
- s1=P1^0;
- play3:
- while(s1==1)
- {
- a3: p=music_tab3[i];
- if(p==0x00) { i=0, delayms(1000); goto play3;} //如果碰到结束符,延时1秒,回到开始再来一遍
- else if(p==0xff) { i=i+1;delayms(100),TR0=0; goto a3;} //若碰到休止符,延时100ms,继续取下一音符
- else {yj=music_tab3[i++], n=music_tab3[i++];} //取频率常数 和 节拍常数
- TR0=1; //开定时器1
- while(n!=0) Beep=~Beep,delay1(yj); //等待节拍完成, 通过P1口输出音频(可多声道哦!)
- TR0=0; //关定时器1
- }
- }
- void gequ5()
- {
- uchar p,yj; //m为频率常数变量
- uchar i=0;
- s1=P1^0;
- play4:
- while(s1==1)
- {
- a4: p=music_tab4[i];
- if(p==0x00) { i=0, delayms(1000); goto play4;} //如果碰到结束符,延时1秒,回到开始再来一遍
- else if(p==0xff) { i=i+1;delayms(100),TR0=0; goto a4;} //若碰到休止符,延时100ms,继续取下一音符
- else {yj=music_tab4[i++], n=music_tab4[i++];} //取频率常数 和 节拍常数
- TR0=1; //开定时器1
- while(n!=0) Beep=~Beep,delay1(yj); //等待节拍完成, 通过P1口输出音频(可多声道哦!)
- TR0=0; //关定时器1
- }
- }
- void int0() interrupt 1 //采用中断0 控制节拍
- { TH0=0xd8;
- TL0=0xef;
- n--;
- }
-
- void main()
- {
- uchar num,i;
- ini();
- shuma();
- while(1)
- {
- if(s1==0)
- {
- delay(500);
- if(s1==0)
- {
- num++;
- if(num>5)
- {
- num=1;
- }
- while((i<50)&&(s1==0))
- {
- delay(200);
- i++;
- }
- i=0;
- }
- }
- switch(num)
- {
- case 1:
- P0=discode[1];
- P2=0x01;
- gequ1();
- break;
- case 2:
- P0=discode[2];
- P2=0x01;
- gequ2();
- break;
- case 3:
- P0=discode[3];
- P2=0x01;
- gequ3();
- break;
- case 4:
- P0=discode[4];
- P2=0x01;
- gequ4();
- break;
- case 5:
- P0=discode[5];
- P2=0x01;
- gequ5();
- break;
- }
- }
- }
复制代码
所有资料51hei提供下载:
我的音乐盒.zip
(31.44 KB, 下载次数: 29)
|