;30h=步进值，21h低8位 22h高8位
org 0000h
ajmp main
org 000bh
ajmp tim0
org 0030h
main:  ;主程序
mov tmod,#01h ;设定时方式
mov ie,#82h ;开中断
start0:
mov 30h,#00h ;
next:  mov a,30h
jnz sing ;根据音调决定是否演奏.
clr tr0
ajmp 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
jz end0 ;如果到了00则停止发音
setb tr0 ;启动定时
d1: call delay
inc 30h ;取下一个码
ajmp next
end0: clr tr0
ajmp start0 ;重新开始
tim0: ;定时器中断服务程序
push acc
push psw
mov tl0,20h ;重赋初值.
mov th0,21h
cpl p1.0
pop psw
pop acc
reti
delay: mov r7,#03h ;延时子程序 决定每个音的时间
d2: mov r4,#187
d3: mov r3,#248
djnz r3,$
djnz r4,d3
djnz r7,d2
ret
table1: ;决定音调的定时初值.
dw 64260,64400,64524,64580
dw 64684,64777,64820,64898
dw 64968,65030,65058,65110
dw 65157,65178,65217,00
end
