5-11聲音產生控制實習 (Sound generation) Q: How to generate a sound from the speaker? A: The 8051 microprocessor generate square signal of a certain frequency on P3.7. This signal controls the ON/OFF of the transistor Q1 in series connection with the speaker. The ON/OFF of Q1 controls the current flowing through the speaker.
Program function (範例) Program 5_11_1: 使揚聲器持續發出 1 kHz 的聲音。 Program 5_11_3: 利用 44 矩陣鍵盤之 0~E 鍵,產生低音的 Si 到高音的 Si 共15個音階,以模擬電子琴的鍵盤。按 ”F” 鍵則不發出聲音。 Program 5_11_4: 計數 55 5振盪電路的脈波數,來控制揚聲器演奏中音的Do到高音的Do等八個音階,使達到利用脈波信號的週期控制音階的拍數。操作時,將JP4之bit 2與 bit 3短路。 Program 5_11_5: 連續播放 小蜜蜂 樂曲,其簡譜如下所示。 |5 3 3 |4 2 2 |1 2 3 4 |5 5 5 | |1 3 5 5 |3 | |2 2 2 2 |2 3 4 |3 3 3 3 |3 4 5 | |1 |
Program function (自我練習) Program 5_11_1_1:使揚聲器持續發出 400 Hz 的聲音。 Program 5_11_2_1: 忙音是以 400 Hz 的聲音叫0.5秒、停0.5秒而形成的,請設計此聲音程式。 Program 5_11_2_2: 鈴聲是以320 Hz和 480 Hz 的聲音交替響25 ms 而形成的,請設計一程式,模擬電話鈴聲,每響1秒鐘,靜音2秒鐘。 (Design a program that generates phone ring. The phone ring contains alternate 320 Hz and 480 Hz sounds with 2 sec of silence after every 1 sec of ringing. ) Program 5_11_3_1: 修改程式,使按下 “F” 鍵時,能將中音的七個音階播放一遍。 Program 5_11_3_2: 修改程式,除了依按鍵的值發出音階聲音以外,另將按鍵的二進制值由D5 LED 上顯示出來。 Program 5_11_4_1: 程式5_11_4 的TABLE表中所設定的資料為中音的Do到高音的Do之資料,所以只會連續播放Do、Re、Mi、Fa、So、La、Si、Do的音,若改成其他的音階資料,則將演奏不同的樂曲。請更改Table 表中的資料,自行創作一首樂曲出來。 Program 5_11_4_2: 修改程式5_11_4,使其在每播完一個音階之後,停頓 50 ms,再播放下一個音階。(提示:每計數一個脈波,就呼叫一個50 ms的延時副程式來達成。) Program 5_11_5_1: 請自行找出16首音樂之簡譜,利用鍵盤的 0 ~ F 按鍵來分別控制這16首音樂的播放,以做出一台點歌機。
5-11-1 基本發聲練習 (一) Program function: 5-11-1 基本發聲練習 (一) Program function: To make the speaker generate a 1 kHz sound. One period = (0.5 + 0.5) ms = 1 ms. Fundamental frequency = 1 kHz.
Program 5_11_1 ORG 000H START: SETB P3.7 CALL DELAY CLR P3.7 JMP START ;============================== ; DELAY 0.5mS DELAY: MOV R7,#249 DJNZ R7,$ RET END One period = (0.5 + 0.5) ms = 1 ms. Fundamental frequency = 1 kHz.
5-11-2 基本發聲練習 (二) Program function: To make the speaker repeatedly 5-11-2 基本發聲練習 (二) Program function: To make the speaker repeatedly (1) generate a 1 kHz sound for 0.5 second and (2) keep silent for 0.5 second.
5-11-2 ORG 000H START: MOV R0,#5 NEXT2: MOV R1,#100 NEXT1: SETB P3.7 ;============================== ; DELAY 0.5mS = half period of square wave DELAY: MOV R7,#249 DJNZ R7,$ RET ; DELAY 0.5S = silence time DL05S: MOV R5,#5 DL2: MOV R6,#200 DL1: DJNZ R6,DL1 DJNZ R5,DL2 END ORG 000H START: MOV R0,#5 NEXT2: MOV R1,#100 NEXT1: SETB P3.7 CALL DELAY CLR P3.7 DJNZ R1,NEXT1 DJNZ R0,NEXT2 CALL DL05S JMP START Duration of a sound = 1 ms x 5 x 100 = 0.5 s
The DELAY procedure Machine cycle Repetition times MOV R7,#249 1 DJNZ R7,$ 2 249 RET Delay time = (1 x 1 + 2 x 249 + 2 x 1) machine cycles = 501 machine cycles = 501 x 1 s = 0.501 ms
5-11-3 電子琴 Program function: Key 1 2 3 4 5 6 7 8 9 A B C D E F Sound 5-11-3 電子琴 Frequency Period ½ Period Low pitch Middle Pitch High Frequency Period ½ Period Frequency Period ½ Period Program function: Key 1 2 3 4 5 6 7 8 9 A B C D E F Sound Si Do Re Mi Fa So La silent
5-11-3 ORG 000H JMP MAIN ORG 003H JMP INT0 ORG 100H MAIN: MOV IE,#10000001B SETB IT0 MOV DPTR,#TABLE JMP $ ;============================== INT0: CLR EA MOV A,P1 ANL A,#0FH CJNE A,#0FH,PLAY JMP RETURN PLAY: MOVC A,@A+DPTR CONTI: MOV R0,A SETB P3.7 CALL DELAY CLR P3.7 JNB P3.2,CONTI RETURN: SETB EA RETI ½ period of SI = 1012 us ;============================== ; This table stores R0 values ; R0 value = (half period in us)/10 TABLE: DB 101 ;SI: 1012/10 DB 96 ;DO: 956/10 DB 85 ;RE: 851/10 DB 76 ;MI: 759/10 DB 72 ;FA:716/10 DB 64 ;SO:638/10 DB 57 ;LA: 568/10 DB 51 ;SI: 506/10 DB 48 ;DO: 478/10 DB 43 ;RE: 426/10 DB 38 ;MI: 380/10 DB 36 ;FA: 358/10 DB 32 ;SO: 319/10 DB 28 ;LA: 284/10 DB 25 ;SI: 253/10 ; DELAY R0 X 10 uS (1/2 period) DELAY: MOV R7,#4 DJNZ R7,$ DJNZ R0,DELAY RET END 1 byte
Instruction Machine cycle Times DELAY: MOV R7,#4 1 1 x R0 DJNZ R7,$ 2 4 * R0 DJNZ R0,DELAY 1 * R0 RET Delay time = (1 x 1 + 2 x 4 + 2 x 1) x R0 + 2 x 1 = 11 x R0 +2 (in machine cycle)
5-11-4 脈波控制節拍器 TL0 Sound 1 2 3 4 5 6 7 .
TMOD (SFR 89H) Timer 1 Timer 0 Gate C/T M1 M0 1 M1 M0 Mode 1 M1 M0 Mode 13-bit timer 1 16-bit timer 2 8-bit autoload 3 2 independent 8-bit timer
5-11-4 ORG 000H MOV DPTR,#TABLE MOV TMOD,#00000110B MOV TH0,#0 ; counter’s initial value MOV TL0,#0 ; counter’s initial value SETB TR0 ; to start counting NEXT: MOV A,TL0 ANL A,#00000111B MOVC A,@A+DPTR MOV R0,A ; R0 = the tone SETB P3.7 CALL DELAY MOV R0,A CLR P3.7 JMP NEXT ;============================== TABLE: DB 96 ;DO DB 85 ;RE DB 76 ;MI DB 72 ;FA DB 64 ;SO DB 57 ;LA DB 51 ;SI DB 48 ;DO ; DELAY R0X10uS DELAY: MOV R7,#4 DJNZ R7,$ DJNZ R0,DELAY RET END
5-11-5 音樂盒 Program function: To play 小蜜蜂 小蜜蜂 簡譜 4/4 5-11-5 音樂盒 Program function: To play 小蜜蜂 小蜜蜂 簡譜 4/4 | 5 3 3 | 4 2 2 | 1 2 3 4 | 5 5 5 | | 5 3 3 | 4 2 2 | 1 3 5 5 | 3 | | 2 2 2 2 | 2 3 4 | 3 3 3 3 | 3 4 5 | | 5 3 3 | 4 2 2 | 1 3 5 5 | 1 | 二拍= 1 second 一拍= 0.5 second
5-11-5 ; ------------------------------ ; Input: A = tone PLAY: MOV R0,A SETB P3.7 CALL DELAY MOV R0,A CLR P3.7 JNB F0,PLAY JMP NEXT ORG 000H JMP MAIN ORG 00BH JMP TIMER0 ORG 100H MAIN: MOV IE,#10000010B MOV TMOD,#00000001B MOV TH0,#>(65536-50000) MOV TL0,#<(65536-50000) SETB TR0 CONTI: MOV R1,#0 ;DATA POINT NEXT: CLR F0 MOV DPTR,#TABLE ;GET BEAT (拍數) MOV A,R1 MOVC A,@A+DPTR MOV R2,A ; R2 = 10 x the no. of beats INC DPTR ; GET TONE (音調) MOVC A,@A+DPTR ; A = tone CJNE A,#0,PLAY ; A = 0 at the end of the song JMP CONTI ;============================== ; Input: R2 = 10 50 ms x 10 = 0.5 s = 1 beat ; R2 = 20 50 ms x 20 = 1 s = 2 beat TIMER0: CLR TR0 DJNZ R2,EXIT CALL DLY50MS SETB F0 INC R1 ; to point to next tone INC R1 ; EXIT: MOV TH0,#>(65536-50000) MOV TL0,#<(65536-50000) SETB TR0 RETI
5-11-5 (cont.) ;============================== ½ period / 10 1 beat 0.5 sec 2 beat 1 sec ;============================== TABLE: DB 10,64,10,76,20,76 ; 5 3 3 ; 10 = 1 beat, 20 = 2 beats DB 10,72,10,85,20,85 ; 4 2 2 DB 10,96,10,85,10,76,10,72 ; 1 2 3 4 DB 10,64,10,64,20,64 ; 5 5 5 DB 10,64,10,76,20,76 ; 5 3 3 DB 10,72,10,85,20,85 ; 4 2 2 DB 10,96,10,76,20,64 ; 1 3 5 5 DB 40,76 ; 3 DB 10,85,10,85,10,85,10,85 DB 10,85,10,76,20,72 DB 10,76,10,76,10,76,10,76 DB 10,76,10,72,20,64 DB 10,64,10,76,20,76 DB 10,72,10,85,20,85 DB 10,96,10,76,10,64,10,64 DB 40,96 DB 0,0 ;============================== ; DELAY R0X10uS DELAY: MOV R7,#4 DJNZ R7,$ DJNZ R0,DELAY RET ; DELAY 50mS DLY50MS: MOV R5,#100 DL1: MOV R6,#249 DJNZ R6,$ DJNZ R5,DL1 END