相關知識:
1.要產生音頻脈沖,只要算出某壹音頻的周期(1/頻率),然後將此周期除以2,即為半周期的時間。利用定時器計時這個半周期時間,每當計時到後就將輸出脈沖的I/O反相,然後重復計時此半周期時間再對I/O反相,就可在I/O腳上得到此頻率的脈沖。
2.利用8051的內部定時器使其工作在計數器模式MODE1下,改變計數值TH0及TL0以產生不同頻率的方法。
3.例如頻率為523Hz,其周期T=1/523=1912us,因此只要令計數器計時956us/1us=956,在每計數956次時將I/O反相,就可得到中音DO(523Hz)。
計數脈沖值與頻率的關系公式如下:
N=Fi÷2÷Fr N:計數值;
Fi:內部計時壹次為1us,故其頻率為12MHz;
Fr:要產生的頻率;
4.其計數值的求法如下:
T=65536-N=65536-Fi÷2÷Fr
例如:設K=65536,F=1000000=Fi=1MHz,求低音DO(261MHz)、中音DO(523MHz)、高音DO(1046MHz)的計數值。
T=65536-N=65536-Fi÷2÷Fr=65536-1000000÷2÷Fr=65536-500000/Fr
低音DO的T=65536-500000/262=63627
中音DO的T=65536-500000/523=64580
高音DO的T=65536-500000/1047=65059
5.C調各音符頻率與計數值T的對照表如下:
表1 C調各音符頻率與計數值T的對照表
音符
低1DO 頻率(Hz) 簡譜碼(T值) 音符 頻率(Hz) 簡譜碼(T值)
262 63628 #4FA# 740 64860
#1DO# 277 63731 中5SO 784 64898
低2RE
#2RE# 294 63835 #5SO# 831 64934
311 63928 中6LA 880 64968
低3M 330 64021 #6 932 64994
低4FA 349 64103 中7SI 988 65030
#4FA# 370 64185 高1DO 1046 65058
低5SO 392 64260 #1DO# 1109 65085
#5SO# 415 64331 高2RE 1175 65110
低6LA 440 64400 #2RE# 1245 65134
#6 466 64463 高3M 1318 65157
低7SI 494 64524 高4FA 1397 65178
中1DO 523 64580 #4FA# 1480 65198
#1DO# 554 64633 高5SO 1568 65217
中2RE 587 64684 #5SO# 1661 65235
#2RE# 622 64732 高6LA 1760 65252
中3M 659 64777 #6 1865 65268
中4FA 698 64820 高 1967 65283
表2 節拍與節拍碼對照
節 拍 碼 節 拍 數 節 拍 碼 節 拍 數
1 1/4拍 1 1/8拍
2 2/4拍 2 1/4拍
3 3/4拍 3 3/8拍
4 1拍 4 1/2拍
5 1又1/4拍 5 5/8拍
6 1又1/2拍 6 3/4拍
8 2拍 8 1
A 2又1/2拍 A 1又1/4拍
C 3拍 C 1又1/2拍
F 3又3/4拍
表3各調1/4節拍的時間設定 各調1/4節拍的時間設定
曲 調 值 DELAY 曲 調 值 DELAY
調4/4 125毫秒 調4/4 62毫秒
調3/4 187毫秒 調3/4 94毫秒
調2/4 250毫秒 調2/4 125毫秒
1/4拍的延遲時間=187毫秒
DELAY: MOV R7,#02
D2: MOV R4,#187 別的延遲值,只需修改這兒的值為相應值,即可。
D3: MOV R3,#248
DJNZ R3,$
DJNZ R4,D3
DJNZ R7,D2
DJNZ R5,DELAY 節拍值放在R5,決定節拍
RET
建立音樂的步驟:
1. 先把樂譜的音符找出,然後建立T值表的順序。
2. 把T值表建立在TABLE1,構成發音符是計數值放在“TABLE”。
3. 簡譜碼(音符)為高位,節拍為(節拍數)為低4位,音符節拍碼放在程序的“TABLE”處。
相關知識:
每壹音符使用1個字節,字節的高4位代表音符的高低,低4位代表音符的節拍,表2為節拍與節拍碼的對照。如果1拍為0.4秒,1/4拍是0.1秒,只要設定延遲時間就可求得節拍的時間 。假使1/4拍為1DELAY,則1拍應為4DELAY,以此類推。所以只要求得1/4拍的DEALY時間,其余的節拍就是它的倍數,如表3為1/4和1/8節拍的時間設定。
簡 譜 發 音 簡 譜 碼 T值
5 低音SO 1 64260
6 低音LA 2 64400
7 低音TI 3 64524
1 中音DO 4 64580
2 中音RE 5 64684
3 中音MI 3 64777
4 中音FA 7 64820
5 中音SO 8 64898
6 中音LA 9 64968
7 中音TI A 65030
1 高音DO B 65058
2 高音RE C 65110
3 高音MI D 65157
4 高音FA E 65178
5 高音SO F 65217
不發音 0
節 拍 碼 節 拍 數
1 1/4拍
2 2/4拍
3 3/4拍
4 1拍
5 1又1/4拍
6 1又1/2拍
8 2拍
A 2又1/2拍
C 3拍
F 3又3/4拍
表4 簡譜對應的簡譜碼、T值、節拍數
#include "reg51.h"
#define uint unsigned int
#define uchar unsigned char
uchar * TABLE ;
code uint TABLE1 [15]={64260,64400,64524,64580,
64684,64777,64820,64898,
64968,65030,65058,65110,
65157,65178,65217};
code uchar SONG [ ]={
//1. 遲來的愛
//0
0x08,0x08,
//1
0x02,0x12,0x42,0x62,0x52,0x42,0x21,11,
0x18,0x18,
0x02,0x22,0x42,0x82,0x92,0x82,0x61,0x51,0x42,
0x58,0x58,
//2
0x66,0x51,0x61,0x84,0x62,0x82,
0x52,0x62,0x42,0x52,0x28,
0x52,0x42,0x52,0x62,0x94,0x81,0x91,0x81,0x61,
0x1C,0x12,0x82,
//3
0x68,0x02,0x92,0x82,0x42,
0x5C,0x12,0x62,
0x58,0x02,0x42,0x51,0x41,0x22,
0x4C,0x62,0x52,
//4
0x66,0x82,0x92,0x82,0x61,0x51,0x41,0x51,
0x6C,0x52,0x62,
0x53,0x63,0x52,0x42,0x42,0x22,
0x1C,0x12,0x22,
//5
0x42,0x44,0x51,0x61,0x82,0x84,0x61,0x81,
0x96,0x82,0x66,0x51,0x61,
0x56,0x42,0x22,0x42,0x82,0x62,
0x5C,0x62,0x52,
//6
0x66,0x82,0x92,0x82,0x61,0x51,0x41,0x51,
0x6C,0x52,0x62,
0x56,0x62,0x52,0x42,0x42,0x22,
0x1C,0x12,0x22,
//7
0x42,0x44,0x51,0x61,0x82,0x84,0x61,0x81,
0x96,0x82,0x66,0x51,0x61,
0x56,0x62,0x92,0x82,0x62,0x52,
0x4C,0x62,0x61,0x81,
//8
0x9C,0x81,0x91,0x81,0x61,
0x6C,0x82,0x62,
0x56,0x42,0x24,0x42,0x52,
0x6C,0x62,0x61,0x81,
//9
0x9C,0xB2,0x92,
0x8C,0x82,0x92,
0xB2,0xB2,0xB2,0x92,0xD4,0xC1,0xD1,0xC1,0xB1,
0xCC,0xB2,0xB1,0xC1,
//A
0xDC,0xD2,0xC1,0xB1,
0x9C,0x92,0x91,0xC1,
0x92,0x84,0x92,0x82,0x62,0x51,0x61,0x51,0x41,
0x48,0x48,
//B
0x42,0x41,0x51,0x66,0x82,0x91,0xB1,
0x92,0x86,0x88,
0x42,0x41,0x51,0x66,0x42,0x41,0x51,
0x22,0x16,0x18,
0x86,0x62,0x58,
//C
0x83,0x91,0x62,0x82,0x58,
0x52,0x62,0x52,0x42,0x22,0x12,0x62,0x52,
0x4C,0x62,0x52,
//6
0x66,0x82,0x92,0x82,0x61,0x51,0x41,0x51,
0x6C,0x52,0x62,
0x56,0x62,0x52,0x42,0x42,0x22,
0x1C,0x12,0x22,
//7
0x42,0x44,0x51,0x61,0x82,0x84,0x61,0x81,
0x96,0x82,0x66,0x51,0x61,
0x56,0x62,0x92,0x82,0x62,0x52,
0x4C,0x62,0x61,0x81,
//8
0x9C,0x81,0x91,0x81,0x61,
0x6C,0x82,0x62,
0x56,0x42,0x24,0x42,0x52,
0x6C,0x62,0x61,0x81,
//9
0x9C,0xB2,0x92,
0x8C,0x82,0x92,
0xB2,0xB2,0xB2,0x92,0xD4,0xC1,0xD1,0xC1,0xB1,
0xCC,0xB2,0xB1,0xC1,
//A
0xDC,0xD2,0xC1,0xB1,
0x9C,0x92,0x91,0xC1,
0x92,0x84,0x92,0x82,0x62,0x51,0x61,0x51,0x41,
0x4C,0x62,0x61,0x81,
//8
0x9C,0x81,0x91,0x81,0x61,
0x6C,0x82,0x62,
0x56,0x42,0x24,0x42,0x52,
0x6C,0x62,0x61,0x81,
//9
0x9C,0xB2,0x92,
0x8C,0x82,0x92,
0xB2,0xB2,0xB2,0x92,0xD4,0xC1,0xD1,0xC1,0xB1,
0xCC,0xB2,0xB1,0xC1,
//A
0xDC,0xD2,0xC1,0xB1,
0x9C,0x92,0x91,0xC1,
0x92,0x84,0x92,0x82,0x62,0x51,0x61,0x51,0x41,
0x4C,0x62,0x61,0x81,
//D
0x66,0x51,0x61,0x86,0x61,0x81,
0x52,0x62,0x52,0x42,0x28,
0x82,0x91,0x01,0x62,0x11,0x52,0x11,0x01,0x62,0x54,
0x48,0x48,0x00};
uchar ljsz;
uchar jpm;
uchar yfm;
uchar t0 =0;
uint i=0;
sbit Speaker =P1^0;
uint k;
void Delay1()
{
for ( k=0;k<20000;k++);
}
void Lignt( )
{ uchar t;
for(t=0;t<10;t++)
{
P2=0xff;
Delay1();
P2=0x00;
Delay1();
}
}
void Lignt1( )
{
P2=0xaa;
Delay1();
P2=0x55;
Delay1();
}
void Delay(uchar t0)//延時
{
while(t0--) //決定節拍
Lignt1();
}
void Delay2(uchar w0)//延時
{
while(w0--) //決定節拍
Lignt();
}
void T0Int() interrupt 1//TIMER0
{
TL0 = ljsz;
TH0 = hjsz;
Speaker=!Speaker;
}
void ExtInt0() interrupt 0//INT0
{
if(t0>10)
t0=0;
i=0;
switch(t0++)
{
case 0 : TABLE=SONG;break; //選擇第1首
case 1 : TABLE=SONG1;break;//選擇第2首
case 2 : TABLE=SONG2;break;//選擇第3首
case 3 : TABLE=SONG3;break;//選擇第4首
case 4 : TABLE=SONG4;break;//選擇第5首
case 5 : TABLE=SONG5;break;//選擇第6首
case 6 : TABLE=SONG6;break;//選擇第7首
case 7 : TABLE=SONG7;break;//選擇第8首
case 8 : TABLE=SONG8;break;//選擇第9首
case 9 : TABLE=SONG9;break;//選擇第10首
}
}
void songsing()//發音程序
{
i=0;
while(TABLE[i])
{
jpm=TABLE[i]&0x0f; //節拍值
yfm=(TABLE[i]>>0x04)&0x0f;//簡譜值
if(yfm) //簡譜為1,取計數值
{
yfm=yfm-1;
hjsz=(uchar)(TABLE1[yfm]>>0x08);//取計數值高字節
TH0 = hjsz;
ljsz=(uchar)(TABLE1[yfm]); //取計數值低字節
TL0 = ljsz;
TR0 = 1; //啟動TIMER0
}
else TR0 = 0; //簡譜為0,不發音
Delay(jpm); //節拍延時
i++;
}
}
void main(void)
{
TMOD =0X01;//設TIMER0在MODE1
TCON =0X01;//外部中斷INT0,下降沿觸發
IE =0x83;//開中斷(INT0,TIMER0)
IP =0x00;//設置中斷優先級
TABLE=SONG0;
while(1)
{
Lignt( );
songsing();//調用發音程序
TR0 = 0;//停止TIMER0
Delay2(1);
TABLE=SONG0;
songsing();
}
}