一個獨立按鍵實現四首歌循壞播放
單片機源程序如下:
- /************************************************************************
- [文件名] 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)
2019-10-3 10:49 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|