當前位置:最新電影網 - qq音樂 - 音樂盒設計

音樂盒設計

# include & ltreg52.h & gt

#包含“Music.h”

#包含“SoundPlay.h”

#包含“lcd.h”

# Define system _ OSC 1200000//定義120000000 Hz的晶振頻率。

#define SOUND_SPACE 4/5 //定義普通音符的長度分數,//每個四分音符音程。

sbit bee Pio = p2^0;//定義輸出引腳

無符號字符代碼段[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x 90 };//段代碼0~9

無符號int碼FreTab[12] = { 262,277,294,311,330,349,369,392,415,440,466,494 };//原始頻率表

無符號字符代碼SignTab[7] = { 0,2,4,5,7,9,11 };//1~7在頻率表中的位置

無符號字符代碼長度Tab[7]= { 1,2,4,8,16,32,64 };

無符號char Sound_Temp_TH0,Sound _ Temp _ TL0//臨時存儲音符定時器的初始值

無符號char Sound_Temp_TH1,Sound _ Temp _ TL 1;//暫時存儲持續時間計時器的初始值

//*******************************************************************

#define MaxNo 3 //定義歌曲的數量。

無符號字符代碼*聲音;

無符號char code * code Music[MaxNo]= { Music _ 4,Music_8,Music_7,Music_6,Music_5,Music_9,Music_3,Music_2,Music _ 1 };

有符號字符FlagNo = 0;

靜態無符號字符鍵掃描(void);

靜態空嗶聲(void);

位run stop = 1;//1表示跑步。

sbit keyb =p3^2;//最後壹首歌

sbit keyf =p3^3;//下壹首歌

sbit keyt =p3^5;//暫停

extern void Delay1ms(無符號int);

void InitialSound(void)

{

bee Pio = 0;

sound _ Temp _ th 1 =(65535-(1/1200)* SYSTEM _ OSC)/256;//計算TL1的初始值(初始值為10ms)。

sound _ Temp _ TL 1 =(65535-(1/1200)* SYSTEM _ OSC)% 256;//計算TH1應該加載的初始值。

th 1 = Sound _ Temp _ th 1;

TL 1 = Sound _ Temp _ TL 1;

TMOD | = 0x 11;//兩個定時器都工作在1模式。

ET0 = 1;

et 1 = 0;

TR0 = 0;

tr 1 = 0;

EA = 1;

FlagNo = 0;

聲音=音樂[FlagNo];

P1 =段[FlagNo+1];

}

Void蜂鳴定時器0 (void)中斷1//音符被中斷。

{

BeepIO =!比皮奧;

TH0 = Sound _ Temp _ TH0

TL0 = Sound _ Temp _ TL0

}

//*******************************************************************

無效播放(無符號字符簽名、無符號八和弦、無符號整數速度)

{

unsigned int NewFreTab[12];//新頻率表

無符號字符I,j;

無符號int Point,LDiv,LDiv0,LDiv1,LDiv2,LDiv4,CurrentFre,Temp_T,SoundLength

無符號字符音調、長度、SL、SH、SM、SLen、XG、FD;

#ifdef使用_LCD

LCD _ init();

lcd_write_string(0,“音樂播放器”);

#endif

再次重申:

delay 1 ms(600);

for(I = 0;我& lt12;I++) //根據調號和八度生成新的頻率表。

{

j = i +簽名;

if(j & gt;11)

{

j = j-12;

NewFreTab[I]= FreTab[j]* 2;

}

其他

NewFreTab[I]= FreTab[j];

if(八弦== 1)

NewFreTab[I]& gt;& gt=2;

else if(八弦== 3)

NewFreTab[I]& lt;& lt=2;

}

SoundLength = 0;

while(Sound[SoundLength]!= 0x00) //計算歌曲長度

{

聲音長度+= 2;

}

點= 0;

聲調=聲音[點];

長度=聲音[點+1];//讀取第壹個音符及其時間值。

ldiv 0 = 12000/速度;//計算1個分數音符的長度(幾個10ms)

ldiv 4 = ldiv 0/4;//計算四分音符的長度。

ldiv 4 = ldi v4-ldi v4 * SOUND _ SPACE;//普通音調的最長音程標準

TR0 = 0;

tr 1 = 1;

while(點& lt聲音長度)

{

if(KeyScan())再次goto/*更多鍵掃描速度*/

SL = Tone % 10;//計算音符

SM = Tone/10% 10;//計算音調的高低。

SH = Tone/100;//計算是否增加壹半。

current fre = NewFreTab[sign tab[SL-1]+SH];//找出對應音符的頻率。

if(KeyScan())再次goto/*更多鍵掃描速度*/

如果(SL!=0)

{

if(SM = = 1)current for & gt;& gt= 2;//低音

if(SM = = 3)current for & lt;& lt= 2;//高音

temp _ T = 65536-(50000/current fire)* 10/(12000000/SYSTEM _ OSC);//計算計數器的初始值

sound _ Temp _ TH0 = Temp _ T/256;

Sound _ Temp _ TL0 = Temp _ T % 256

TH0 = Sound _ Temp _ TH0

TL0 = Sound _ Temp _ TL0+12;//加上12就是中斷延遲的補償。

}

if(KeyScan())再次goto/*更多鍵掃描速度*/

SLen = Length tab[Length % 10];//算算有多少音符。

XG =長度/10% 10;//計算音符類型(0普通1連奏2噸)

FD =長度/100;

LDiv = LDiv 0/SLen;//計算連音音符的播放長度(多少個10ms)。

if (FD==1)

LDiv = LDiv+LDiv/2;

如果(XG!=1)

If(XG==0) //計算普通音符的播放長度。

如果(SLen & lt=4)

LDiv 1 = LDiv-LDiv 4;

其他

LDiv 1 = LDiv * SOUND _ SPACE;

其他

LDiv 1 = LDiv/2;//計算暫停的播放長度。

其他

LDiv 1 = LDiv;

if(SL = = 0)ldiv 1 = 0;

LDiv 2 = LDiv-LDiv 1;//計算沈默的長度。

if(KeyScan())再次goto/*更多鍵掃描速度*/

如果(SL!=0)

{

TR0 = 1;

for(I = ldiv 1;我& gt0;I-)//讀出指定的長度。

{

while(TF 1 = = 0);

th 1 = Sound _ Temp _ th 1;

TL 1 = Sound _ Temp _ TL 1;

TF 1 = 0;

if(KeyScan())再次goto/*更多鍵掃描速度*/

}

}

if(KeyScan())再次goto/*更多鍵掃描速度*/

if(LDiv2!=0)

{

TR0 = 0;bee Pio = 0;

for(I = ldiv 2;我& gt0;I-)//音符之間的音程

{

while(TF 1 = = 0);

th 1 = Sound _ Temp _ th 1;

TL 1 = Sound _ Temp _ TL 1;

TF 1 = 0;

if(KeyScan())再次goto/*更多鍵掃描速度*/

}

}

點+= 2;

聲調=聲音[點];

長度=聲音[點+1];

if(KeyScan())再次goto/*更多鍵掃描速度*/

#ifdef使用_LCD

lcd_write_char(12,FlagNo+49);

#endif

}

bee Pio = 0;

flag no++;

if(FlagNo = = MaxNo)FlagNo = 0;

聲音=音樂[FlagNo];

P1 =段[FlagNo+1];

}

/**************************************************************

功能:鍵盤掃描功能

加入我們:viod KeyF的下壹首歌

KeyB最後壹首歌

按鍵暫停

返回:無符號字符

1需要跳轉。0不跳轉。

***************************************************************/

靜態無符號字符鍵掃描(void)

{

無符號字符控制;

If (KeyF == 0){ // Forward

delay 1 ms(5);

if (KeyF == 0){

while(KeyF == 0){

EA = 0;

flag no++;

if(FlagNo = = MaxNo)FlagNo = 0;

P1 =段[FlagNo+1];

beep();

delay 1 ms(600);//可以,長按

EA = 1;

}

if(FlagNo = = MaxNo)FlagNo = 0;

聲音=音樂[FlagNo];

P1=段[FlagNo+1];//筆刷顯示

while(KeyF = = 0);

返回1;

}

}

If (KeyB == 0){ //後退

delay 1 ms(5);

if (KeyB == 0){

while(KeyB == 0){

EA = 0;

FlagNo-;

if(FlagNo = =-1)FlagNo = MaxNo-1;

P1 =段[FlagNo+1];//筆刷顯示

beep();

delay 1 ms(600);//可以,長按

EA = 1;

}

if(FlagNo = =-1)FlagNo = MaxNo-1;

聲音=音樂[FlagNo];

P1 =段[FlagNo+1];

while(KeyB = = 0);

返回1;

}

}

If (KeyT == 0){ //暫停

delay 1 ms(20);

cont = 0;

EA = 0;

TR0 = 0;

tr 1 = 0;

if (KeyT == 0){

RunStop = ~ RunStop

while(KeyT = = 0);//等待第壹次發布

beep();

如果(!RunStop){ //如果是,中斷和定時器將關閉。

EA = 0;

TR0 = 0;

tr 1 = 0;

while(KeyT = = 0);//檢測第二次按鍵並等待釋放(如果有)。

While (KeyT){ //第二次釋放,等待第三次按下。

P1 =段[cont++ % 10];//第三次不按,暫停幾秒0~9。

delay 1 ms(100);

}//在這裏暫停,直到調再次變低。

EA = 1;//重啟定時器並中斷

TR0 = 1;

tr 1 = 1;

聲音=音樂[FlagNo];

P1 =段[FlagNo+1];//恢復原始顯示

}

}

EA = 1;

TR0 = 1;

tr 1 = 1;

返回0;

}

返回0;

}

無效嗶嗶聲(無效)

{

無符號字符I;

for(I = 0;我& lt200 ;i ++){

BeepIO = ~ BeepIO

delay 1 ms(1);

}

}

播放音樂:

# include & ltREG52。H & gt

#包含“Music.h”

#包含“SoundPlay.h”

void delay 1毫秒(無符號整數計數)

{

無符號int i,j;

for(I = 0;我& lt數數;i++)

for(j = 0;j & lt120;j++);

}

主()

{

initial sound();

while(1)

{

玩(0,3,380);

delay 1 ms(500);

}

}

音樂:

# include“Music . h”/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

//揮動翅膀的女孩

無符號字符代碼Music_1[]={

0x17,0x02,0x17,0x03,0x18,0x03,0x19,0x02,0x15,0x03,

0x16,0x03,0x17,0x03,0x17,0x03,0x17,0x03,0x18,0x03,

0x19,0x02,0x16,0x03,0x17,0x03,0x18,0x02,0x18,0x03,

0x17,0x03,0x15,0x02,0x18,0x03,0x17,0x03,0x18,0x02,

0x10,0x03,0x15,0x03,0x16,0x02,0x15,0x03,0x16,0x03,

0x17,0x02,0x17,0x03,0x18,0x03,0x19,0x02,0x1A,0x03,

0x1B,0x03,0x1F,0x03,0x1F,0x03,0x17,0x03,0x18,0x03,

0x19,0x02,0x16,0x03,0x17,0x03,0x18,0x03,0x17,0x03,

0x18,0x03,0x1F,0x03,0x1F,0x02,0x16,0x03,0x17,0x03,

0x18,0x03,0x17,0x03,0x18,0x03,0x20,0x03,0x20,0x02,

0x1F,0x03,0x1B,0x03,0x1F,0x66,0x20,0x03,0x21,0x03,

0x20,0x03,0x1F,0x03,0x1B,0x03,0x1F,0x66,0x1F,0x03,

0x1B,0x03,0x19,0x03,0x19,0x03,0x15,0x03,0x1A,0x66,

0x1A,0x03,0x19,0x03,0x15,0x03,0x15,0x03,0x17,0x03,

0x16,0x66,0x17,0x04,0x18,0x04,0x18,0x03,0x19,0x03,

0x1F,0x03,0x1B,0x03,0x1F,0x66,0x20,0x03,0x21,0x03,

0x20,0x03,0x1F,0x03,0x1B,0x03,0x1F,0x66,0x1F,0x03,

0x1B,0x03,0x19,0x03,0x19,0x03,0x15,0x03,0x1A,0x66,

0x1A,0x03,0x19,0x03,0x19,0x03,0x1F,0x03,0x1B,0x03,

0x1F,0x00,0x1A,0x03,0x1A,0x03,0x1A,0x03,0x1B,0x03,

0x1B,0x03,0x1A,0x03,0x19,0x03,0x19,0x02,0x17,0x03,

0x15,0x17,0x15,0x03,0x16,0x03,0x17,0x03,0x18,0x03,

0x17,0x04,0x18,0x0E,0x18,0x03,0x17,0x04,0x18,0x0E,

0x18,0x66,0x17,0x03,0x18,0x03,0x17,0x03,0x18,0x03,

0x20,0x03,0x20,0x02,0x1F,0x03,0x1B,0x03,0x1F,0x66,

0x20,0x03,0x21,0x03,0x20,0x03,0x1F,0x03,0x1B,0x03,

0x1F,0x66,0x1F,0x04,0x1B,0x0E,0x1B,0x03,0x19,0x03,

0x19,0x03,0x15,0x03,0x1A,0x66,0x1A,0x03,0x19,0x03,

0x15,0x03,0x15,0x03,0x17,0x03,0x16,0x66,0x17,0x04,

0x18,0x04,0x18,0x03,0x19,0x03,0x1F,0x03,0x1B,0x03,

0x1F,0x66,0x20,0x03,0x21,0x03,0x20,0x03,0x1F,0x03,

0x1B,0x03,0x1F,0x66,0x1F,0x03,0x1B,0x03,0x19,0x03,

0x19,0x03,0x15,0x03,0x1A,0x66,0x1A,0x03,0x19,0x03,

0x19,0x03,0x1F,0x03,0x1B,0x03,0x1F,0x00,0x18,0x02,

0x18,0x03,0x1A,0x03,0x19,0x0D,0x15,0x03,0x15,0x02,

0x18,0x66,0x16,0x02,0x17,0x02,0x15,0x00,0x00,0x 00 };

//同壹首歌

無符號字符代碼Music_2[]={

0x0F,0x01,0x15,0x02,0x16,0x02,0x17,0x66,0x18,0x03,

0x17,0x02,0x15,0x02,0x16,0x01,0x15,0x02,0x10,0x02

0x15,0x00,0x0F,0x01,0x15,0x02,0x16,0x02,0x17,0x02,

0x17,0x03,0x18,0x03,0x19,0x02,0x15,0x02,0x18,0x66,

0x17,0x03,0x19,0x02,0x16,0x03,0x17,0x03,0x16,0x00,

0x17,0x01,0x19,0x02,0x1B,0x02,0x1B,0x70,0x1A,0x03,

0x1A,0x01,0x19,0x02,0x19,0x03,0x1A,0x03,0x1B,0x02,

0x1A,0x0D,0x19,0x03,0x17,0x00,0x18,0x66,0x18,0x03,

0x19,0x02,0x1A,0x02,0x19,0x0C,0x18,0x0D,0x17,0x03,

0x16,0x01,0x11,0x02,0x11,0x03,0x10,0x03,0x0F,0x0C,

0x10,0x02,0x15,0x00,0x1F,0x01,0x1A,0x01,0x18,0x66,

0x19,0x03,0x1A,0x01,0x1B,0x02,0x1B,0x03,0x1B,0x03,

0x1B,0x0C,0x1A,0x0D,0x19,0x03,0x17,0x00,0x1F,0x01,

0x1A,0x01,0x18,0x66,0x19,0x03,0x1A,0x01,0x10,0x02,

0x10,0x03,0x10,0x03,0x1A,0x0C,0x18,0x0D,0x17,0x03,

0x16,0x00,0x0F,0x01,0x15,0x02,0x16,0x02,0x17,0x70,

0x18,0x03,0x17,0x02,0x15,0x03,0x15,0x03,0x16,0x66,

0x16,0x03,0x16,0x02,0x16,0x03,0x15,0x03,0x10,0x02,

0x10,0x01,0x11,0x01,0x11,0x66,0x10,0x03,0x0F,0x0C,

0x1A,0x02,0x19,0x02,0x16,0x03,0x16,0x03,0x18,0x66,

0x18,0x03,0x18,0x02,0x17,0x03,0x16,0x03,0x19,0x00,

0x00,0x 00 };

//兩只蝴蝶

無符號字符代碼Music_3[] ={

0x17,0x03,0x16,0x03,0x17,0x 016,0x03,0x17,0x03,

0x16,0x03,0x15,0x01,0x10,0x03,0x15,0x03,0x16,0x02,

0x16,0x0D,0x17,0x03,0x16,0x03,0x15,0x03,0x10,0x03,

0x10,0x0E,0x15,0x04,0x0F,0x01,0x17,0x03,0x16,0x03,

0x17,0x01,0x16,0x03,0x17,0x03,0x16,0x03,0x15,0x01,

0x10,0x03,0x15,0x03,0x16,0x02,0x16,0x0D,0x17,0x03,

0x16,0x03,0x15,0x03,0x10,0x03,0x15,0x03,0x16,0x01,

0x17,0x03,0x16,0x03,0x17,0x 016,0x03,0x17,0x03,

0x16,0x03,0x15,0x01,0x10,0x03,0x15,0x03,0x16,0x02,

0x16,0x0D,0x17,0x03,0x16,0x03,0x15,0x03,0x10,0x03,

0x10,0x0E,0x15,0x04,0x0F,0x01,0x17,0x03,0x19,0x03,

0x19,0x01,0x19,0x03,0x1A,0x03,0x19,0x03,0x17,0x01,

0x16,0x03,0x16,0x03,0x16,0x02,0x16,0x0D,0x17,0x03,

0x16,0x03,0x15,0x03,0x10,0x03,0x10,0x0D,0x15,0x00,

0x19,0x03,0x19,0x03,0x1A,0x03,0x1F,0x03,0x1B,0x03,

0x1B,0x03,0x1A,0x03,0x17,0x0D,0x16,0x03,0x16,0x03,

0x16,0x0D,0x17,0x01,0x17,0x03,0x17,0x03,0x19,0x03,

0x1A,0x02,0x1A,0x02,0x10,0x03,0x17,0x0D,0x16,0x03,

0x16,0x01,0x17,0x03,0x19,0x03,0x19,0x03,0x17,0x03,

0x19,0x02,0x1F,0x02,0x1B,0x03,0x1A,0x03,0x1A,0x0E,

0x1B,0x04,0x17,0x02,0x1A,0x03,0x1A,0x03,0x1A,0x0E,

0x1B,0x04,0x1A,0x03,0x19,0x03,0x17,0x03,0x16,0x03,

0x17,0x0D,0x16,0x03,0x17,0x03,0x19,0x01,0x19,0x03,

0x19,0x03,0x1A,0x03,0x1F,0x03,0x1B,0x03,0x1B,0x03,

0x1A,0x03,0x17,0x0D,0x16,0x03,0x16,0x03,0x16,0x03,

0x17,0x01,0x17,0x03,0x17,0x03,0x19,0x03,0x1A,0x02,

0x1A,0x02,0x10,0x03,0x17,0x0D,0x16,0x03,0x16,0x01,

0x17,0x03,0x19,0x03,0x19,0x03,0x17,0x03,0x19,0x03,

0x1F,0x02,0x1B,0x03,0x1A,0x03,0x1A,0x0E,0x1B,0x04,

0x17,0x02,0x1A,0x03,0x1A,0x03,0x1A,0x0E,0x1B,0x04,

0x17,0x16,0x1A,0x03,0x1A,0x03,0x1A,0x0E,0x1B,0x04,

0x1A,0x03,0x19,0x03,0x17,0x03,0x16,0x03,0x0F,0x02,

0x10,0x03,0x15,0x00,0x00,0x 00 };