單片機(jī)演奏音樂原理與模擬實(shí)驗(yàn)
;標(biāo)題 '八月桂花香'發(fā)聲程序
ORG 0000H
LJMP START
ORG 000BH
INC 20H ;中斷服務(wù),中斷計(jì)數(shù)器加1
MOV TH0,#0D8H
MOV TL0,#0EFH ;12M晶振,形成10毫秒中斷
RETI
START:
MOV SP,#50H
MOV TH0,#0D8H
MOV TL0,#0EFH
MOV TMOD,#01H
MOV IE,#82H
MUSIC0:
NOP
MOV DPTR,#DAT ;表頭地址送DPTR
MOV 20H,#00H ;中斷計(jì)數(shù)器清0
MUSIC1:
NOP
CLR A ;A清零
MOVC A,@A+DPTR;查表取代碼
JZ END0 ;是00H,則結(jié)束
CJNE A,#0FFH,MUSIC5 ;如果是休止符,往下執(zhí)行
LJMP MUSIC3
MUSIC5:
NOP
MOV R6,A ;R6=18H音符的頻率
INC DPTR ;DPTR加一
MOV A,#0
MOVC A,@A+DPTR;取節(jié)拍代碼送R7
MOV R7,A ;R7=30H音符發(fā)音的時(shí)間
SETB TR0 ;啟動(dòng)計(jì)數(shù)
MUSIC2:
NOP
CPL P2.5 ;P2.5是音樂輸出引腳
MOV A,R6
MOV R3,A ;R3=R6=18H
LCALL DEL
MOV A,R7
CJNE A,20H,MUSIC2 ;中斷計(jì)數(shù)器(20H)=R7否?
;不等,則繼續(xù)循環(huán)
MOV 20H,#00H ;等于,則取下一代碼
INC DPTR
LJMP MUSIC1
MUSIC3:
NOP ;休止100毫秒
CLR TR0
MOV R2,#0DH ;R2=13
MUSIC4:
NOP
MOV R3,#0FFH ;R3=255
LCALL DEL
DJNZ R2,MUSIC4
INC DPTR
LJMP MUSIC1
END0:
NOP
MOV R2,#0FFH ;歌曲結(jié)束,延時(shí)1秒后繼續(xù)
MUSIC6:
MOV R3,#00H
LCALL DEL
DJNZ R2,MUSIC6
LJMP MUSIC0
DEL:
NOP
DEL3:
MOV R4,#02H
DEL4:
NOP
DJNZ R4,DEL4
NOP
DJNZ R3,DEL3
RET
NOP
DAT:
DB 18H, 30H, 1CH, 10H
DB 20H, 40H, 1CH, 10H
DB 18H, 10H, 20H, 10H
DB 1CH, 10H, 18H, 40H
DB 1CH, 20H, 20H, 20H
DB 1CH, 20H, 18H, 20H
DB 20H, 80H, 0FFH, 20H
DB 30H, 1CH, 10H , 18H
DB 20H, 15H, 20H , 1CH
DB 20H, 20H, 20H , 26H
DB 40H, 20H , 20H , 2BH
DB 20H, 26H, 20H , 20H
DB 20H, 30H , 80H , 0FFH
DB 20H, 20H, 1CH , 10H
DB 18H, 10H, 20H , 20H
DB 26H, 20H , 2BH , 20H
DB 30H, 20H , 2BH , 40H
DB 20H, 20H , 1CH , 10H
DB 18H, 10H, 20H, 20H
DB 26H, 20H , 2BH, 20H
DB 30H, 20H, 2BH , 40H
DB 20H, 30H, 1CH , 10H
DB 18H, 20H , 15H , 20H
DB 1CH, 20H , 20H , 20H
DB 26H, 40H, 20H , 20H
DB 2BH, 20H, 26H , 20H
DB 20H, 20H, 30H , 80H
DB 20H, 30H, 1CH , 10H
DB 20H, 10H, 1CH , 10H
DB 20H, 20H, 26H , 20H
DB 2BH, 20H, 30H , 20H
DB 2BH, 40H, 20H , 15H
DB 1FH, 05H, 20H , 10H
DB 1CH, 10H, 20H , 20H
DB 26H, 20H, 2BH , 20H
DB 30H, 20H, 2BH , 40H
DB 20H, 30H, 1CH , 10H
DB 18H, 20H , 15H , 20H
DB 1CH, 20H , 20H , 20H
DB 26H, 40H, 20H , 20H
DB 2BH, 20H, 26H , 20H
DB 20H, 20H, 30H, 30H
DB 20H, 30H, 1CH, 10H
DB 18H, 40H, 1CH, 20H
DB 20H, 20H, 26H, 40H
DB 13H, 60H, 18H , 20H
DB 15H, 40H, 13H , 40H
DB 18H, 80H, 00H
END
評(píng)論