新聞中心

EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計應(yīng)用 > 51單片機蜂鳴器音樂簡譜轉(zhuǎn)換工具

51單片機蜂鳴器音樂簡譜轉(zhuǎn)換工具

作者: 時間:2016-11-18 來源:網(wǎng)絡(luò) 收藏
相信您在作蜂鳴器發(fā)聲實驗時肯定為復(fù)雜的頻率與音調(diào)之間的轉(zhuǎn)換而傷腦筋,這里向大家介紹一款工具可以大大簡化轉(zhuǎn)換操作,只需要輸入簡譜就能自動生成音樂播放程序代碼。

以下此程序的主界面:

本文引用地址:http://butianyuan.cn/article/201611/315982.htm

程序范例可以點主界面上的“關(guān)于”按鈕后就能找到,但是代碼也不全,需要加入對音樂程序的調(diào)用代碼,其它范例里也寫的很清楚了,只需要調(diào)用Play函數(shù)就行了,其調(diào)用格式如下:


Play(樂曲名,調(diào)號,升降八度,演奏速度);

經(jīng)過實踐,此函數(shù)的的后三個參數(shù)分別設(shè)為0,2和350時演奏出的音樂比較符合“原味”,下面給出本站的入門型51實驗板結(jié)合此工具作的音樂程序:


//51單片機播放音樂庫文件
/**************************************************************************
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
*******************************************************************************/
/*說明**************************************************************************
曲譜存貯格式 unsigned char code MusicName{音高,音長,音高,音長...., 0,0}; 末尾:0,0 表示結(jié)束(Important)
音高由三位數(shù)字組成:
個位是表示 1~7 這七個音符
十位是表示音符所在的音區(qū):1-低音,2-中音,3-高音;
百位表示這個音符是否要升半音: 0-不升,1-升半音。
音長最多由三位數(shù)字組成:
個位表示音符的時值,其對應(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-頓音
百位是符點位: 0-無符點,1-有符點
調(diào)用演奏子程序的格式
Play(樂曲名,調(diào)號,升降八度,演奏速度);
|樂曲名 : 要播放的樂曲指針,結(jié)尾以(0,0)結(jié)束;
|調(diào)號(0-11) : 是指樂曲升多少個半音演奏;
|升降八度(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 //定義普通音符演奏的長度分率,//每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; //音符定時器初值暫存
unsigned char Sound_Temp_TH1,Sound_Temp_TL1; //音長定時器初值暫存
//**************************************************************************
void InitialSound(void)
{
BeepIO = 1;
Sound_Temp_TH1 = (65535-(1/1200)*SYSTEM_OSC)/256; // 計算TL1應(yīng)裝入的初值 (10ms的初裝值)
Sound_Temp_TL1 = (65535-(1/1200)*SYSTEM_OSC)%256; // 計算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)號及升降八度來生成新的頻率表
{
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) //計算歌曲長度
{
SoundLength+=2;
}
Point = 0;
Tone = Sound[Point];
Length = Sound[Point+1]; // 讀出第一個音符和它時時值
LDiv0 = 12000/Speed; // 算出1分音符的長度(幾個10ms)
LDiv4 = LDiv0/4; // 算出4分音符的長度
LDiv4 = LDiv4-LDiv4*SOUND_SPACE; // 普通音最長間隔標(biāo)準(zhǔn)
TR0 = 0;
TR1 = 1;
while(Point < SoundLength)
{
SL=Tone%10; //計算出音符
SM=Tone/10%10; //計算出高低音
SH=Tone/100; //計算出是否升半
CurrentFre = NewFreTab[SignTab[SL-1]+SH]; //查出對應(yīng)音符的頻率
if(SL!=0)
{
if (SM==1) CurrentFre >>= 2; //低音
if (SM==3) CurrentFre <<= 2; //高音
Temp_T = 65536-(50000/CurrentFre)*10/(12000000/SYSTEM_OSC);//計算計數(shù)器初值
Sound_Temp_TH0 = Temp_T/256;
Sound_Temp_TL0 = Temp_T%256;
TH0 = Sound_Temp_TH0;
TL0 = Sound_Temp_TL0 + 12; //加12是對中斷延時的補償
}
SLen=LengthTab[Length%10]; //算出是幾分音符
XG=Length/10%10; //算出音符類型(0普通1連音2頓音)
FD=Length/100;
LDiv=LDiv0/SLen; //算出連音音符演奏的長度(多少個10ms)
if (FD==1)
LDiv=LDiv+LDiv/2;
if(XG!=1)
if(XG==0) //算出普通音符的演奏長度
if (SLen<=4)
LDiv1=LDiv-LDiv4;
else
LDiv1=LDiv*SOUND_SPACE;
else
LDiv1=LDiv/2; //算出頓音的演奏長度
else
LDiv1=LDiv;
if(SL==0) LDiv1=0;
LDiv2=LDiv-LDiv1; //算出不發(fā)音的長度
if (SL!=0)
{
TR0=1;
for(i=LDiv1;i>0;i--) //發(fā)規(guī)定長度的音
{
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);
}
}

點擊下載Music Encode

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

再例如:關(guān)于“世上只有媽媽好”的單片機音樂演奏程序

單片機演奏一個音符,是通過引腳,周期性的輸出一個特定頻率的方波。
這就需要單片機,在半個周期內(nèi)輸出低電平、另外半個周期輸出高電平,周而復(fù)始。
半個周期的時間是多長呢?眾所周知,周期為頻率的倒數(shù),可以通過音符的頻率計算出半周期。
演奏時,要根據(jù)音符頻率的不同,把對應(yīng)的、半個周期的定時時間初始值,送入定時器,再由定時器按時輸出高低電平。

下面是個網(wǎng)上廣泛流傳的單片機音樂演奏程序,它可以循環(huán)的播放“世上只有媽媽好”這首樂曲。很多人都關(guān)心如何修改這個樂曲的內(nèi)容,但是不知如何入手。
做而論道對這個程序,給出說明,希望對大家有所幫助,以后大家自己就能夠編寫進去新的樂曲。

在這個程序中,有兩個數(shù)據(jù)表,其中存放了事先算好的、各種音符頻率所對應(yīng)的、半周期的定時時間初始值。
有了這些數(shù)據(jù),單片機就可以演奏從低音、中音、高音和超高音,四個八度共28個音符。

演奏樂曲時,就根據(jù)音符的不同數(shù)值,從半周期數(shù)據(jù)表中找到定時時間初始值,送入定時器即可控制發(fā)音的音調(diào)。
比如把表中的0xF2和0x42送到定時器,定時器按照這個初始值來產(chǎn)生中斷,輸出的方波,人們聽起來,這就是低音1。

樂曲的數(shù)據(jù),也要寫個數(shù)據(jù)表,程序中以 code unsigned char sszymmh[] 命名。
這個表中每三個數(shù)字,說明了一個音符,它們分別代表:
第一個數(shù)字是音符的數(shù)值1234567之一,代表多來咪發(fā)...;
第二個數(shù)字是0123之一,代表低音、中音、高音、超高音;
第三個數(shù)字是時間長度,以半拍為單位。
樂曲數(shù)據(jù)表的結(jié)尾是三個0。

程序如下:

#include
sbit speaker = P1^7;
unsigned char timer0h, timer0l, time;
//--------------------------------------
//單片機晶振采用11.0592MHz
// 頻率-半周期數(shù)據(jù)表 高八位 本軟件共保存了四個八度的28個頻率數(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ù)表 要想演奏不同的樂曲, 只需要修改這個數(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,
//一個音符有三個數(shù)字。前為第幾個音、中為第幾個八度、后為時長(以半拍為單位)。
//6, 2, 3 分別代表:6, 中音, 3個半拍;
//5, 2, 1 分別代表:5, 中音, 1個半拍;
//3, 2, 2 分別代表:3, 中音, 2個半拍;
//5, 2, 2 分別代表:5, 中音, 2個半拍;
//1, 3, 2 分別代表:1, 高音, 2個半拍;
//
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; //下次的中斷時間, 這個時間, 控制音調(diào)高低
TL0 = timer0l;
TR0 = 1; //啟動T0
}
//--------------------------------------
void delay(unsigned char t) //延時程序,控制發(fā)音的時間長度
{
unsigned char t1;
unsigned long t2;
for(t1 = 0; t1 < t; t1++) //雙重循環(huán), 共延時t個半拍
for(t2 = 0; t2 < 8000; t2++); //延時期間, 可進入T0中斷去發(fā)音
TR0 = 0; //關(guān)閉T0, 停止發(fā)音
}
//--------------------------------------
void song() //演奏一個音符
{
TH0 = timer0h; //控制音調(diào)
TL0 = timer0l;
TR0 = 1; //啟動T0, 由T0輸出方波去發(fā)音
delay(time); //控制時間長度
}
//--------------------------------------
void main(void)
{
unsigned char k, i;
TMOD = 1; //置T0定時工作方式1
ET0 = 1; //開T0中斷
EA = 1; //開CPU中斷
while(1) {
i = 0;
time = 1;
while(time) {
k = sszymmh[i] + 7 * sszymmh[i + 1] - 1;
//第i個是音符, 第i+1個是第幾個八度
timer0h = FREQH[k]; //從數(shù)據(jù)表中讀出頻率數(shù)值
timer0l = FREQL[k]; //實際上, 是定時的時間長度
time = sszymmh[i + 2]; //讀出時間長度數(shù)值
i += 3;
song(); //發(fā)出一個音符
} } }
//======================================
應(yīng)網(wǎng)友要求,下面再詳細寫一下樂譜和數(shù)據(jù)的轉(zhuǎn)換關(guān)系。
以李叔同大師的《送別》的前二小節(jié)來說明轉(zhuǎn)換的方法。

這部分的歌詞是:“長 亭 外, 古 道 邊,”;
這部分的樂譜是:| 5 35 1 - | 6 16 5 - |。
(注意:樂譜中的1是高音,上邊是帶點的;還有些音符,應(yīng)該有下劃線,在這里都無法標(biāo)出。感興趣的網(wǎng)友應(yīng)該去查看正規(guī)的樂譜。)

那么,據(jù)此就可以寫出《送別》前二小節(jié)的數(shù)據(jù)表:
//--------------------------------------
code unsigned char sszymmh[] = {
5, 2, 2, 3, 2, 1, 5, 2, 1, 1, 3, 4,
//嗦,中音,2個半拍; 咪,中音,1個半拍; 嗦,中音,1個半拍; 哆,高音,4個半拍
6, 2, 2, 1, 3, 1, 6, 2, 1, 5, 2, 4,
//啦,中音,2個半拍; 哆,高音,1個半拍; 啦,中音,1個半拍; 嗦,中音,4個半拍
0, 0, 0};
//結(jié)束標(biāo)記
//--------------------------------------

記住:三個數(shù)字一組,代表一個音符。
第一個數(shù)字是1234567之一,代表音符哆來咪發(fā)...;
第二個數(shù)字是0123之一,代表低音、中音、高音、超高音;
第三個數(shù)字是半拍的個數(shù),代表時間長度。
當(dāng)三個數(shù)字都是0,就代表樂曲數(shù)據(jù)表的結(jié)尾。

用這個數(shù)據(jù)表,替換掉程序中《世上只有媽媽好》的數(shù)據(jù)表,本程序就可以播放《送別》的前兩小節(jié)。



評論


技術(shù)專區(qū)

關(guān)閉