51單片機(jī)蜂鳴器音樂(lè)簡(jiǎn)譜轉(zhuǎn)換工具
以下此程序的主界面:
本文引用地址:http://www.ex-cimer.com/article/201611/315982.htm程序范例可以點(diǎn)主界面上的“關(guān)于”按鈕后就能找到,但是代碼也不全,需要加入對(duì)音樂(lè)程序的調(diào)用代碼,其它范例里也寫(xiě)的很清楚了,只需要調(diào)用Play函數(shù)就行了,其調(diào)用格式如下:
Play(樂(lè)曲名,調(diào)號(hào),升降八度,演奏速度);
經(jīng)過(guò)實(shí)踐,此函數(shù)的的后三個(gè)參數(shù)分別設(shè)為0,2和350時(shí)演奏出的音樂(lè)比較符合“原味”,下面給出本站的入門型51實(shí)驗(yàn)板結(jié)合此工具作的音樂(lè)程序:
//51單片機(jī)播放音樂(lè)庫(kù)文件
/**************************************************************************
SOUND PLAY FOR 51MCU
COPYRIGHT (c) 2005 BY JJJ.
-- ALL RIGHTS RESERVED --
File Name: SoundPlay.h
Author: Jiang Jian Jun
Created: 2005/5/16
Modified: NO
Revision: 1.0
*******************************************************************************/
/*說(shuō)明**************************************************************************
曲譜存貯格式 unsigned char code MusicName{音高,音長(zhǎng),音高,音長(zhǎng)...., 0,0}; 末尾:0,0 表示結(jié)束(Important)
音高由三位數(shù)字組成:
個(gè)位是表示 1~7 這七個(gè)音符
十位是表示音符所在的音區(qū):1-低音,2-中音,3-高音;
百位表示這個(gè)音符是否要升半音: 0-不升,1-升半音。
音長(zhǎng)最多由三位數(shù)字組成:
個(gè)位表示音符的時(shí)值,其對(duì)應(yīng)關(guān)系是:
|數(shù)值(n): |0 |1 |2 |3 | 4 | 5 | 6
|幾分音符: |1 |2 |4 |8 |16 |32 |64 音符=2^n
十位表示音符的演奏效果(0-2): 0-普通,1-連音,2-頓音
百位是符點(diǎn)位: 0-無(wú)符點(diǎn),1-有符點(diǎn)
調(diào)用演奏子程序的格式
Play(樂(lè)曲名,調(diào)號(hào),升降八度,演奏速度);
|樂(lè)曲名 : 要播放的樂(lè)曲指針,結(jié)尾以(0,0)結(jié)束;
|調(diào)號(hào)(0-11) : 是指樂(lè)曲升多少個(gè)半音演奏;
|升降八度(1-3) : 1:降八度, 2:不升不降, 3:升八度;
|演奏速度(1-12000): 值越大速度越快;
***************************************************************************/
#ifndef __SOUNDPLAY_H_REVISION_FIRST__
#define __SOUNDPLAY_H_REVISION_FIRST__
#include
//**************************************************************************
#define SYSTEM_OSC 11059200//12000000 //定義晶振頻率12000000HZ
#define SOUND_SPACE 4/5 //定義普通音符演奏的長(zhǎng)度分率,//每4分音符間隔
sbit BeepIO = P3^5; //定義輸出管腳
unsigned int code FreTab[12] = { 262,277,294,311,330,349,369,392,415,440,466,494 }; //原始頻率表
unsigned char code SignTab[7] = { 0,2,4,5,7,9,11 }; //1~7在頻率表中的位置
unsigned char code LengthTab[7]= { 1,2,4,8,16,32,64 };
unsigned char Sound_Temp_TH0,Sound_Temp_TL0; //音符定時(shí)器初值暫存
unsigned char Sound_Temp_TH1,Sound_Temp_TL1; //音長(zhǎng)定時(shí)器初值暫存
//**************************************************************************
void InitialSound(void)
{
BeepIO = 1;
Sound_Temp_TH1 = (65535-(1/1200)*SYSTEM_OSC)/256; // 計(jì)算TL1應(yīng)裝入的初值 (10ms的初裝值)
Sound_Temp_TL1 = (65535-(1/1200)*SYSTEM_OSC)%256; // 計(jì)算TH1應(yīng)裝入的初值
TH1 = Sound_Temp_TH1;
TL1 = Sound_Temp_TL1;
TMOD |= 0x11;
ET0 = 1;
ET1 = 0;
TR0 = 0;
TR1 = 0;
EA = 1;
}
void BeepTimer0(void) interrupt 1 //音符發(fā)生中斷
{
BeepIO = !BeepIO;
TH0 = Sound_Temp_TH0;
TL0 = Sound_Temp_TL0;
}
//**************************************************************************
void Play(unsigned char *Sound,unsigned char Signature,unsigned Octachord,unsigned int Speed)
{
unsigned int NewFreTab[12]; //新的頻率表
unsigned char i,j;
unsigned int Point,LDiv,LDiv0,LDiv1,LDiv2,LDiv4,CurrentFre,Temp_T,SoundLength;
unsigned char Tone,Length,SL,SH,SM,SLen,XG,FD;
for(i=0;i<12;i++) // 根據(jù)調(diào)號(hào)及升降八度來(lái)生成新的頻率表
{
j = i + Signature;
if(j > 11)
{
j = j-12;
NewFreTab[i] = FreTab[j]*2;
}
else
NewFreTab[i] = FreTab[j];
if(Octachord == 1)
NewFreTab[i]>>=2;
else if(Octachord == 3)
NewFreTab[i]<<=2;
}
SoundLength = 0;
while(Sound[SoundLength] != 0x00) //計(jì)算歌曲長(zhǎng)度
{
SoundLength+=2;
}
Point = 0;
Tone = Sound[Point];
Length = Sound[Point+1]; // 讀出第一個(gè)音符和它時(shí)時(shí)值
LDiv0 = 12000/Speed; // 算出1分音符的長(zhǎng)度(幾個(gè)10ms)
LDiv4 = LDiv0/4; // 算出4分音符的長(zhǎng)度
LDiv4 = LDiv4-LDiv4*SOUND_SPACE; // 普通音最長(zhǎng)間隔標(biāo)準(zhǔn)
TR0 = 0;
TR1 = 1;
while(Point < SoundLength)
{
SL=Tone%10; //計(jì)算出音符
SM=Tone/10%10; //計(jì)算出高低音
SH=Tone/100; //計(jì)算出是否升半
CurrentFre = NewFreTab[SignTab[SL-1]+SH]; //查出對(duì)應(yīng)音符的頻率
if(SL!=0)
{
if (SM==1) CurrentFre >>= 2; //低音
if (SM==3) CurrentFre <<= 2; //高音
Temp_T = 65536-(50000/CurrentFre)*10/(12000000/SYSTEM_OSC);//計(jì)算計(jì)數(shù)器初值
Sound_Temp_TH0 = Temp_T/256;
Sound_Temp_TL0 = Temp_T%256;
TH0 = Sound_Temp_TH0;
TL0 = Sound_Temp_TL0 + 12; //加12是對(duì)中斷延時(shí)的補(bǔ)償
}
SLen=LengthTab[Length%10]; //算出是幾分音符
XG=Length/10%10; //算出音符類型(0普通1連音2頓音)
FD=Length/100;
LDiv=LDiv0/SLen; //算出連音音符演奏的長(zhǎng)度(多少個(gè)10ms)
if (FD==1)
LDiv=LDiv+LDiv/2;
if(XG!=1)
if(XG==0) //算出普通音符的演奏長(zhǎng)度
if (SLen<=4)
LDiv1=LDiv-LDiv4;
else
LDiv1=LDiv*SOUND_SPACE;
else
LDiv1=LDiv/2; //算出頓音的演奏長(zhǎng)度
else
LDiv1=LDiv;
if(SL==0) LDiv1=0;
LDiv2=LDiv-LDiv1; //算出不發(fā)音的長(zhǎng)度
if (SL!=0)
{
TR0=1;
for(i=LDiv1;i>0;i--) //發(fā)規(guī)定長(zhǎng)度的音
{
while(TF1==0);
TH1 = Sound_Temp_TH1;
TL1 = Sound_Temp_TL1;
TF1=0;
}
}
if(LDiv2!=0)
{
TR0=0; BeepIO=1;
for(i=LDiv2;i>0;i--) //音符間的間隔
{
while(TF1==0);
TH1 = Sound_Temp_TH1;
TL1 = Sound_Temp_TL1;
TF1=0;
}
}
Point+=2;
Tone=Sound[Point];
Length=Sound[Point+1];
}
BeepIO = 1;
}
//**************************************************************************
#endif
//揮著翅膀的女孩
unsigned char code Music_Girl[]={ 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,0x00};
//同一首歌
unsigned char code Music_Same[]={ 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,0x00 };
//兩只蝴蝶
unsigned char code Music_Two[] ={ 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,
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,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,
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,0x00 };
void main()
{
InitialSound();
while(1)
{
Play(Music_Girl,0,2,350);
Play(Music_Same,0,2,350);
Play(Music_Two,0,2,350);
}
}
點(diǎn)擊下載Music Encode
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
再例如:關(guān)于“世上只有媽媽好”的單片機(jī)音樂(lè)演奏程序
單片機(jī)演奏一個(gè)音符,是通過(guò)引腳,周期性的輸出一個(gè)特定頻率的方波。
這就需要單片機(jī),在半個(gè)周期內(nèi)輸出低電平、另外半個(gè)周期輸出高電平,周而復(fù)始。
半個(gè)周期的時(shí)間是多長(zhǎng)呢?眾所周知,周期為頻率的倒數(shù),可以通過(guò)音符的頻率計(jì)算出半周期。
演奏時(shí),要根據(jù)音符頻率的不同,把對(duì)應(yīng)的、半個(gè)周期的定時(shí)時(shí)間初始值,送入定時(shí)器,再由定時(shí)器按時(shí)輸出高低電平。
下面是個(gè)網(wǎng)上廣泛流傳的單片機(jī)音樂(lè)演奏程序,它可以循環(huán)的播放“世上只有媽媽好”這首樂(lè)曲。很多人都關(guān)心如何修改這個(gè)樂(lè)曲的內(nèi)容,但是不知如何入手。
做而論道對(duì)這個(gè)程序,給出說(shuō)明,希望對(duì)大家有所幫助,以后大家自己就能夠編寫(xiě)進(jìn)去新的樂(lè)曲。
在這個(gè)程序中,有兩個(gè)數(shù)據(jù)表,其中存放了事先算好的、各種音符頻率所對(duì)應(yīng)的、半周期的定時(shí)時(shí)間初始值。
有了這些數(shù)據(jù),單片機(jī)就可以演奏從低音、中音、高音和超高音,四個(gè)八度共28個(gè)音符。
演奏樂(lè)曲時(shí),就根據(jù)音符的不同數(shù)值,從半周期數(shù)據(jù)表中找到定時(shí)時(shí)間初始值,送入定時(shí)器即可控制發(fā)音的音調(diào)。
比如把表中的0xF2和0x42送到定時(shí)器,定時(shí)器按照這個(gè)初始值來(lái)產(chǎn)生中斷,輸出的方波,人們聽(tīng)起來(lái),這就是低音1。
樂(lè)曲的數(shù)據(jù),也要寫(xiě)個(gè)數(shù)據(jù)表,程序中以 code unsigned char sszymmh[] 命名。
這個(gè)表中每三個(gè)數(shù)字,說(shuō)明了一個(gè)音符,它們分別代表:
第一個(gè)數(shù)字是音符的數(shù)值1234567之一,代表多來(lái)咪發(fā)...;
第二個(gè)數(shù)字是0123之一,代表低音、中音、高音、超高音;
第三個(gè)數(shù)字是時(shí)間長(zhǎng)度,以半拍為單位。
樂(lè)曲數(shù)據(jù)表的結(jié)尾是三個(gè)0。
程序如下:
#include
sbit speaker = P1^7;
unsigned char timer0h, timer0l, time;
//--------------------------------------
//單片機(jī)晶振采用11.0592MHz
// 頻率-半周期數(shù)據(jù)表 高八位 本軟件共保存了四個(gè)八度的28個(gè)頻率數(shù)據(jù)
code unsigned char FREQH[] = {
0xF2, 0xF3, 0xF5, 0xF5, 0xF6, 0xF7, 0xF8, //低音1234567
0xF9, 0xF9, 0xFA, 0xFA, 0xFB, 0xFB, 0xFC, 0xFC,//1,2,3,4,5,6,7,i
0xFC, 0xFD, 0xFD, 0xFD, 0xFD, 0xFE, //高音 234567
0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFF}; //超高音 1234567
// 頻率-半周期數(shù)據(jù)表 低八位
code unsigned char FREQL[] = {
0x42, 0xC1, 0x17, 0xB6, 0xD0, 0xD1, 0xB6, //低音1234567
0x21, 0xE1, 0x8C, 0xD8, 0x68, 0xE9, 0x5B, 0x8F, //1,2,3,4,5,6,7,i
0xEE, 0x44, 0x6B, 0xB4, 0xF4, 0x2D, //高音 234567
0x47, 0x77, 0xA2, 0xB6, 0xDA, 0xFA, 0x16}; //超高音 1234567
//--------------------------------------
//世上只有媽媽好數(shù)據(jù)表 要想演奏不同的樂(lè)曲, 只需要修改這個(gè)數(shù)據(jù)表
code unsigned char sszymmh[] = {
6, 2, 3, 5, 2, 1, 3, 2, 2, 5, 2, 2, 1, 3, 2, 6, 2, 1, 5, 2, 1,
//一個(gè)音符有三個(gè)數(shù)字。前為第幾個(gè)音、中為第幾個(gè)八度、后為時(shí)長(zhǎng)(以半拍為單位)。
//6, 2, 3 分別代表:6, 中音, 3個(gè)半拍;
//5, 2, 1 分別代表:5, 中音, 1個(gè)半拍;
//3, 2, 2 分別代表:3, 中音, 2個(gè)半拍;
//5, 2, 2 分別代表:5, 中音, 2個(gè)半拍;
//1, 3, 2 分別代表:1, 高音, 2個(gè)半拍;
//
6, 2, 4, 3, 2, 2, 5, 2, 1, 6, 2, 1, 5, 2, 2, 3, 2, 2, 1, 2, 1,
6, 1, 1, 5, 2, 1, 3, 2, 1, 2, 2, 4, 2, 2, 3, 3, 2, 1, 5, 2, 2,
5, 2, 1, 6, 2, 1, 3, 2, 2, 2, 2, 2, 1, 2, 4, 5, 2, 3, 3, 2, 1,
2, 2, 1, 1, 2, 1, 6, 1, 1, 1, 2, 1, 5, 1, 6, 0, 0, 0};
//--------------------------------------
void t0int() interrupt 1 //T0中斷程序,控制發(fā)音的音調(diào)
{
TR0 = 0; //先關(guān)閉T0
speaker = !speaker; //輸出方波, 發(fā)音
TH0 = timer0h; //下次的中斷時(shí)間, 這個(gè)時(shí)間, 控制音調(diào)高低
TL0 = timer0l;
TR0 = 1; //啟動(dòng)T0
}
//--------------------------------------
void delay(unsigned char t) //延時(shí)程序,控制發(fā)音的時(shí)間長(zhǎng)度
{
unsigned char t1;
unsigned long t2;
for(t1 = 0; t1 < t; t1++) //雙重循環(huán), 共延時(shí)t個(gè)半拍
for(t2 = 0; t2 < 8000; t2++); //延時(shí)期間, 可進(jìn)入T0中斷去發(fā)音
TR0 = 0; //關(guān)閉T0, 停止發(fā)音
}
//--------------------------------------
void song() //演奏一個(gè)音符
{
TH0 = timer0h; //控制音調(diào)
TL0 = timer0l;
TR0 = 1; //啟動(dòng)T0, 由T0輸出方波去發(fā)音
delay(time); //控制時(shí)間長(zhǎng)度
}
//--------------------------------------
void main(void)
{
unsigned char k, i;
TMOD = 1; //置T0定時(shí)工作方式1
ET0 = 1; //開(kāi)T0中斷
EA = 1; //開(kāi)CPU中斷
while(1) {
i = 0;
time = 1;
while(time) {
k = sszymmh[i] + 7 * sszymmh[i + 1] - 1;
//第i個(gè)是音符, 第i+1個(gè)是第幾個(gè)八度
timer0h = FREQH[k]; //從數(shù)據(jù)表中讀出頻率數(shù)值
timer0l = FREQL[k]; //實(shí)際上, 是定時(shí)的時(shí)間長(zhǎng)度
time = sszymmh[i + 2]; //讀出時(shí)間長(zhǎng)度數(shù)值
i += 3;
song(); //發(fā)出一個(gè)音符
} } }
//======================================
應(yīng)網(wǎng)友要求,下面再詳細(xì)寫(xiě)一下樂(lè)譜和數(shù)據(jù)的轉(zhuǎn)換關(guān)系。
以李叔同大師的《送別》的前二小節(jié)來(lái)說(shuō)明轉(zhuǎn)換的方法。
這部分的歌詞是:“長(zhǎng) 亭 外, 古 道 邊,”;
這部分的樂(lè)譜是:| 5 35 1 - | 6 16 5 - |。
(注意:樂(lè)譜中的1是高音,上邊是帶點(diǎn)的;還有些音符,應(yīng)該有下劃線,在這里都無(wú)法標(biāo)出。感興趣的網(wǎng)友應(yīng)該去查看正規(guī)的樂(lè)譜。)
那么,據(jù)此就可以寫(xiě)出《送別》前二小節(jié)的數(shù)據(jù)表:
//--------------------------------------
code unsigned char sszymmh[] = {
5, 2, 2, 3, 2, 1, 5, 2, 1, 1, 3, 4,
//嗦,中音,2個(gè)半拍; 咪,中音,1個(gè)半拍; 嗦,中音,1個(gè)半拍; 哆,高音,4個(gè)半拍
6, 2, 2, 1, 3, 1, 6, 2, 1, 5, 2, 4,
//啦,中音,2個(gè)半拍; 哆,高音,1個(gè)半拍; 啦,中音,1個(gè)半拍; 嗦,中音,4個(gè)半拍
0, 0, 0};
//結(jié)束標(biāo)記
//--------------------------------------
記?。喝齻€(gè)數(shù)字一組,代表一個(gè)音符。
第一個(gè)數(shù)字是1234567之一,代表音符哆來(lái)咪發(fā)...;
第二個(gè)數(shù)字是0123之一,代表低音、中音、高音、超高音;
第三個(gè)數(shù)字是半拍的個(gè)數(shù),代表時(shí)間長(zhǎng)度。
當(dāng)三個(gè)數(shù)字都是0,就代表樂(lè)曲數(shù)據(jù)表的結(jié)尾。
用這個(gè)數(shù)據(jù)表,替換掉程序中《世上只有媽媽好》的數(shù)據(jù)表,本程序就可以播放《送別》的前兩小節(jié)。
評(píng)論