51单片机 汇编 io发音系列 生日快乐歌
org 0000h
ajmp main
org 000bh
jmp tim0
org 0030h
main: ;主程序
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 p3.3
pop psw
pop acc
reti
delay: mov r7,#02h ;延时子程序
d2: mov r4,#187
d3: mov r3,#248
djnz r3,$
djnz r4,d3
djnz r7,d2
djnz r5,delay
ret
table1: ;决定音调的定时初值.
1