;祝你生日快乐
ORG 0000H
LJMP START
ORG 000BH
INC 20H ;中断服务,中断计数器加1
MOV TH0,#0bbH ;d8
MOV TL0,#0EFH ;12M晶振,形成10毫秒中断
RETI
START:
INITIAL:MOV SP,#50H
MOV TH0,#0D8H
MOV TL0,#0EFH
MOV TMOD,#01H
MOV IE,#82H
CPL P1.6
MUSIC0: NOP
MOV DPTR,#MUSIC_DAT ;表头地址送DPTR
MOV 20H,#00H ;中断计数器清0
MOV B,#00H ;表序号清0
MUSIC1: NOP
CLR A
MOVC A,@A+DPTR ;查表取代码
JZ END0 ;是00H,则结束
CPL P1.7
CPL P1.6
CJNE A,#0FFH,MUSIC5 ;是休止符则继续执行
LJMP MUSIC3
MUSIC5: NOP
MOV R6,A ;r6=18H音符的频率
INC DPTR
MOV A,B
MOVC A,@A+DPTR ;取节拍代码送R7
MOV R7,A ;r7=30H音符发音的时间
SETB TR0 ;启动计数
MUSIC2: NOP
CPL P2.3 ;音乐输出的管脚
MOV A,R6
MOV R3,A
LCALL DEL
MOV A,R7
CJNE A,20H,MUSIC2 ;中断计数器(20H)=R7否?
;不等,则继续循环
MOV 20H,#00H ;等于,则取下一代码
INC DPTR
LJMP MUSIC1
MUSIC3: NOP
CLR TR0 ;休止100毫秒
MOV R2,#0DH
MUSIC4: NOP
MOV R3,#0FFH
LCALL DEL
DJNZ R2,MUSIC4
INC DPTR
LJMP MUSIC1
END0: NOP
MOV R2,#64H ;歌曲结束,延时1秒后继续
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
MUSIC_DAT:
; 猪你生日快乐曲谱
DB 3FH,10H
DB 0FFH
DB 3FH, 10H
DB 39H, 20H, 3FH, 20H
DB 30H, 20H, 33H, 40H
DB 3FH, 10H
DB 0FFH
DB 3FH, 10H
DB 39H, 20H, 3FH, 20H
DB 2BH, 20H, 30H, 40H
DB 3FH, 10H
DB 0FFH
DB 3FH, 10H
DB 20H, 20H, 26H, 20H
DB 30H, 20H, 33H, 10H
db 39H, 30H
DB 23H, 10H
DB 0FFh
DB 23H, 10H
DB 26H, 20H, 30H, 20H, 2BH
DB 20H, 30H, 40H
DB 00H
end--