p1.7口定义为蜂鸣器输出.
org 0000h
ajmp main
org 000bh/*T0中断入口*/
ajmp intt0/*T0中断服务子程序*/
org 0030h/*主程序起始地址*/
main: mov tmod,#01h/*设置T0为16位定时器*/
mov ie,#82h/*开中断*/
start: mov 30h,#00h/*取简谱码指针*/
next: mov a,30h/*简谱指针载入A*/
mov dptr,#tab
movc a,@a+dptr
mov r2,a/*取到的简码暂存R2*/
jz end0/*是否取到结束吗*/
anl a,#0fh/*不是,则取低4位*/
mov r5,a/*将节拍码存R5*/
mov a,r2/*将取到的简谱码再载入A*/
swap a/*高低四位交换*/
anl a,#0fh/*取低四位(音符码)*/
jnz sing/*取到的音符码为0吗*/
clr tr0/*是,则不发音*/
ajmp d1/*跳到D1*/
sing: dec a/*取到的音符码减1(不含0)*/
mov 22h,a/*存入22H*/
rl a/*乘2*/
mov dptr,#tab1/*至TAB1取相对的低位字节计数值*/
movc a,@a+dptr
mov th0,a/*取到的高位字节存入TH0*/
mov 21h,a/*取到高位字节存入21H*/
mov a,22h/*再载入取到的音符码*/
rl a/*乘2*/
inc a/*加1*/
movc a,@a+dptr/*至TAB1取相对的低字节计数值*/
mov tl0,a/*取到的低位字节存入TL0*/
mov 20h,a/*取到的低位字节存入20H*/
setb tr0/*启动T0*/
d1: acall delay/*基本单位时间1/4拍187毫秒*/
inc 30h/*取简码指针加1*/
ajmp next/*取下一个码*/
end0: clr tr0/*停止T0*/
ajmp start/*重复循环*/
intt0: push acc/*将A的值暂存于堆栈*/
push psw/*将PSW的值暂存于堆栈*/
mov tl0,20h/*重设计数值*/
mov th0,21h
cpl p1.7/*将P1.7位反相*/
pop psw
pop acc/*从堆栈取回A,PSW的值*/
reti/*中断返回*/
delay: mov r7,#02h/*187MS延时*/
dl1: mov r4,#187
dl2: mov r3,#248
djnz r3,$
djnz r4,dl2
djnz r7,dl1
djnz r5,delay/*决定节拍*/
ret
tab1: dw 64260,64400,64524,64580
dw 64684,64777,64820,64898
dw 64968,65030,65058,65110
dw 65157,65178,65217
/*1*/
tab: db 82h,01h,81h,94h,84h
db 0b4h,0a4h,04h
db 82h,01h,81h,94h,84h
db 0c4h,0b4h,04h
/*2*/
db 82h,01h,81h,0f4h,0d4h
db 0b4h,0a4h,94h
db 0e2h,01h,0e1h,0d4h,0b4h
db 0c4h,0b4h,04h
/*3*/
db 82h,01h,81h,94h,84h
db 0b4h,0a4h,04h
db 82h,01h,81h,94h,84h
db 0c4h,0b4h,04h
/*4*/
db 82h,01h,81h,0f4h,0d4h
db 0b4h,0a4h,94h
db 0e2h,01h,0e1h,0d4h,0b4h
db 0c4h,0b4h,04h
db 00
end