標(biāo)題: 52單片機(jī)播放音樂 [打印本頁]

作者: 嗚啦啦啦    時間: 2016-11-25 00:52
標(biāo)題: 52單片機(jī)播放音樂

#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
uint code tone[]={
1012,956,852,759,716,638,568,506,478,426,379,};        //C大調(diào)對應(yīng)的半音
uint code song[]={
1,1,5,5,6,6,5,
4,4,3,3,2,2,1,
5,5,4,4,3,3,2,
5,5,4,4,3,3,2,
1,1,5,5,6,6,5,
4,4,3,3,2,2,1,
0x00}; //小星星樂譜
uchar code beat[]={1,1,1,1,1,1,2,
1,1,1,1,1,1,2,1,1,1,1,1,1,2,
1,1,1,1,1,1,2,1,1,1,1,1,1,2,
1,1,1,1,1,1,2};//停頓
sbit P2_4=P2^4;
uchar k;
uint low,high;
void delay(uint x)
{
        uint i,j;
        for(i=x;i>0;i--)
                for(j=110*200;j>0;j--);
}
void init()
{
       
        EA=1;
        ET0=1;
        TMOD=0x00;        //方式0
}
void main()
{               
                init();
                while(1)
        {   
                         if(song[k]!=0x00)
                        {
                        P2_4=0;
                        TR0=1;
                        TH0=(8192-tone[song[k]])/32;
                        TL0=(8192-tone[song[k]])%32;
                        delay(beat[k]);
                        k++;
                        }
                        else
                  {
                        TR0 = 0;
                        P2_4 = 1;
                        delay(100);
                        k=0;
                  }
        }
               
               
       
}
void tone0_timer()interrupt 1        //定時器產(chǎn)生不同的聲音
  {               
                  TH0=(8192-tone[song[k]])/32;
                TL0=(8192-tone[song[k]])%32;
                  P2_4=~P2_4;
  }       





歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1