/************************************************************************
[文件名] C51音樂程序(八月桂花)
[功能] 通過單片機演奏音樂
/**********************************************************************/
#include <REG52.H>
#include <INTRINS.H>
//本例采用89C52, 晶振為11.0592MHZ
//關于如何編制音樂代碼, 其實十分簡單,各位可以看以下代碼.
//頻率常數即音樂術語中的音調,而節拍常數即音樂術語中的多少拍;
//所以拿出譜子, 試探編吧!
//定義各個低音頻率
#define Low_1 262
#define Low_2 294
#define Low_3 330
#define Low_4 349
#define Low_5 392
#define Low_6 440
#define Low_7 494
//
#define Mid_1 523
#define Mid_2 587
#define Mid_3 659
#define Mid_4 698
#define Mid_5 784
#define Mid_6 880
#define Mid_7 988
//
#define High_1 1046
#define High_2 1175
#define High_3 1318
#define High_4 1397
#define High_5 1568
#define High_6 1760
#define High_7 1976
sbit Beep = P1^5 ;
sbit StopSpeaker = P2^7 ;//鄒靜 Add 靜機狀態檢查所用端口
unsigned char n=0; //n為節拍常數變量
unsigned char 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
};
unsigned int code ShengRiCode[]={ Low_5,Low_5,Low_6,Low_5,Mid_1,Low_7,
Low_5,Low_5,Low_6,Low_5,Mid_2,Mid_1,
Low_5,Low_5,Mid_5,Mid_3,Mid_1,Low_7,Low_6,
Mid_4,Mid_4,Mid_3,Mid_1,Mid_2,Mid_1,
0xff
}; //Happy Brisday
unsigned int code ShengRiJP[]={0x96,0x36,0x36,0x02,0x02,0x04,0x04,
0x04,0x08,
0x02,0x02,0x04,0x04,
0x04,0x08,
0x02,0x02,0x04,0x04,
0x04,0x04,0x04,
0x03,0x01,0x04,0x04,
0x04,0x08
};
void int0() interrupt 1 //采用中斷0 控制節拍
{ TH0=0xd8;
TL0=0xef;
n--;
}
void delay (unsigned char m) //控制頻率延時
{
unsigned i=3*m; //頻率延時時間3m
while(--i);
}
void delayms(unsigned char a) //豪秒延時子程序
{
while(--a); //采用while(--a) 不要采用while(a--); 各位可編譯一下看看匯編結果就知道了!
}
void StopStatus()
{
unsigned char m,n;
n=0;
TR0=0; //關定時器1
for(m=0;m<1500;m++) //靜機時間
{
if(StopSpeaker==0) //低電平唱
{
n=1;//正常狀態
TR0=1; //開定時器
break;
}
else //高電平,靜機狀態轉換的延時時間
{
delay(1);
}
}
if(n==0) //高電平不唱n依然為0,即不響,進入靜機狀態
while(1)
{
Beep=0; //蜂鳴器端口為0,不響
}
}
void main()
{ unsigned char p,m; //m為頻率常數變量
unsigned char i=0;
TMOD&=0x0f;
TMOD|=0x01;
TH0=0xd8;TL0=0xef;
IE=0x82;
play:
while(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 if(StopSpeaker==1) { StopStatus();delay(1);} //高電平進入靜機狀態,延時100ms,繼續取下一音符
// else {m=ShengRiCode[i++], n=music_tab[i++];} //取頻率常數 和 節拍常數
else {m=ShengRiCode[i++], n=ShengRiJP[2];} //取頻率常數 和 節拍常數
TR0=1; //開定時器1
while(n!=0) Beep=~Beep,delay(m); //等待節拍完成, 通過P1口輸出音頻(可多聲道哦!)
TR0=0; //關定時器1
}
}