org 0000h
ajmp main
org 000bh
jmp tim0
org 0030h
main: ;主程序
setb p1.1
clr p1.4
setb p3.0
mov tmod,#01h ;设定时方式
mov ie,#82h ;开中断
start0:
mov 30h,#00h ;
next:mov a,30h
mov dptr,#table
movc a,@a+dptr ;查表取音调和节拍
mov r2,a
jz end0 ;为零,不进行处理.
anl a,#0fh
mov r5,a ;将节拍存入R5
mov a,r2
swap a
anl a,#0fh
jnz sing ;根据音调决定是否演奏.
clr tr0
jmp d1
sing:dec a
mov 22h,a ;将音调存入22H.
rl a
mov dptr,#table1
movc a,@a+dptr ;根据音调决定定时器初值.
mov th0,a ;放入TH0
mov 21h,a
mov a,22h
rl a
inc a
movc a,@a+dptr
mov tl0,a ;放入TL0
mov 20h,a
setb tr0 ;启动定时
d1: call delay
inc 30h
jmp next
end0:clr tr0
jmp start0
tim0: ;定时器中断服务程序
push acc
push psw
mov tl0,20h ;重赋初值.
mov th0,21h
cpl p1.7
pop psw
pop acc
retidelay:mov r7,#02h ;延时子程序
d2: mov r4,#130
d3: mov r3,#248
djnz r3,$
djnz r4,d3
djnz r7,d2
djnz r5,delay
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 |