BUZZER EQU P2.4
org 0000h ;主程序起始地址
ajmp main ;跳至主程序
org 000bh ;timer0中断起始地址
jmp tim0 ;跳至timer0中断子程序
org 0030h
main:
MOV tmod,#01h ;设timer0在MODE1
MOV ie,#82h ;中断使能
checkkey:
lcall key ;8ms when no key pressed.
cpl a
jz checkkey
MOV a,34h
cjne a,#0ah,checkkey
ajmp start0
start0:
MOV 30h,#00h ;取简谱码指针
next:
lcall key ;8ms when no key pressed.
cpl a
jz begm
MOV a,34h
cjne a,#0bh,begm
ajmp end0
begm:
MOV a,30h ;简谱码指针载入A
MOV dptr,#table ;至table取简谱码
MOV c a,@a+dptr
MOV r2,a ;取到的简谱码暂存于R2
jz end0 ;是否取到00(结束码)?
anl a,#0fh ;不是,则取低4位(节拍码)
MOV r5,a ;将节拍码存入R5
MOV a,r2 ;将取到的简谱码再载入A
swap a ;高低四位交换
anl a,#0fh ;取低四位(音符码)
jnz sing ;取到的音符码是否为零?
clr tr0 ;是,则不发音
jmp d1
sing: dec a ;取到的音符码减1(不含0).
MOV 22h,a ;存入(22H).
rl a ;乘2
MOV dptr,#table1 ;至table1取相对的高位字节计数值
MOV c a,@a+dptr
MOV th0,a ;取到的高位字节存入TH0
MOV 21h,a ;取到的高位字节存入(21H)
MOV a,22h ;再载入取的的音符码
rl a ;乘2
inc a ;加1
MOV c a,@a+dptr ;至table1取相对的低位字节计数值
MOV tl0,a ;取到的高位字节存入TL0
MOV 20h,a ;取到的高位字节存入(20H)
setb tr0 ;启动timer0
d1: call delay ;基本单位时间1/4拍187毫秒
inc 30h ;取简谱码指针加1
jmp next ;取下一个码
end0: clr tr0 ;停止timer0
jmp checkkey ;重复循环
tim0:
push aCC ;将A的值暂存于堆栈
push psw ;将psw的值暂存于堆栈
MOV tl0,20h ;重设计数值
MOV th0,21h
cpl BUZZER ;将蜂鸣器反相
pop psw ;至堆栈取回psw的值
pop acc ;至堆栈取回a的值
reti ;返回主程序
delay: MOV r7,#02h ;187毫秒
d2: MOV r4,#187
d3: MOV r3,#248
djnz r3,$
djnz r4,d3
djnz r7,d2
djnz r5,delay ;决定节拍
ret
KEY: LCALL KS
JNZ K1
ljmp key_exit
K1: LCALL delay4ms
LCALL delay4ms
LCALL KS
JNZ K2
ljmp key_exit
K2: MOV R2,#0EFH
MOV R4,#00H
K3: MOV P1,R2
L6: JB P1.0,L1
MOV A,#00H
AJMP LK
L1: JB P1.1,L2
MOV A,#04H
AJMP LK
L2: JB P1.2,L3
MOV A,#08H
AJMP LK
L3: JB P1.3,NEXT1
MOV A,#0cH
LK: ADD A,R4
MOV 34h,a
K4: ;LCALL delay4ms
; lcall display
; LCALL KS
; JNZ K4
RET
NEXT1:
INC R4
MOV A,R2
JNB ACC.7,key_exit
RL A
MOV R2,A
AJMP K3
key_exit:
MOV a,#0ffh
ret
KS: MOV P1,#0FH
MOV A,P1
XRL A,#0FH
RET
delay4ms: ;4ms
MOV R5,#08H
L7: MOV R6,#0FAH
L8: DJNZ R6,L8
DJNZ R5,L7
RET
table1:
dw 64260,64400,64524,64580
dw 64684,64777,64820,64898
dw 64968,65030,65058,65110
dw 65157,65178,65217
table:
;1
db 82h,01h,81h,94h,84h,0b4h,0a4h,04h,82h,01h,81h,94h,84h,0c4h,0b4h,04h
;2
db 82h,01h,81h,0f4h,0d4h,0b4h,0a4h,94h,0e2h,01h,0e1h,0d4h,0b4h,0c4h,0b4h,04h
;3
db 82h,01h,81h,94h,84h,0b4h,0a4h,04h,82h,01h,81h,94h,84h,0c4h,0b4h,04h
;4
db 82h,01h,81h,0f4h,0d4h,0b4h,0a4h,94h,0e2h,01h,0e1h,0d4h,0b4h,0c4h,0b4h,04h,00
end
实现按键蜂鸣器演奏歌曲功能,按“A”键开始播放,按“B”键停止播放.