51单片机音乐门铃的设计
扫描二维码
随时随地手机看文章
buzzerequp2.4
KEYEQUP3.7
org00h;主程序起始地址
jmpstart;跳动主程序
org0bh;timer0中断起始地址
jmptim0;跳至timer0在mode1
start: MOV tmod,#00000001b;设timer0在mode1
MOV ie,#10000010b;中断使能
jbKEY,$;第一次按to?
calldelay1;消除抖动
jnbKEY,$;to放开?
MOV 31h,#00;按to次数计数指针初值为00h
start0:
MOV 30h,#lowsong;取简谱码指针(第1首)
next: MOV a,30h
MOV dptr,#table
MOV ca,@a+dptr;至相关页码,高4位为音符的高低
MOV r2,a;低4位为音符的节拍
jzend0;检查简谱码是否已结束(有无00?)
anla,#0fh;取节拍(低4位)
MOV r5,a;存入r5,节拍的时间
MOV a,r2
swapa
anla,#0fh;取音频值(高4位)
jnzsing;是否为0,是0则不发音
clrtr0
jmpd1
sing:deca;因0不列入
MOV 22h,a;存入(22h)
rla;乘2
MOV dptr,#table
MOV ca,@a+dptr;至table取码,取t的值
MOV th0,a;取到的高位字节存入th0
MOV 21h,a;取到的高位字节存入(21h)
MOV a,22h;再载入取到的音符码
rla;乘2
inca;加1
MOV ca,@a+dptr;至table取相对的低位字节计数值
MOV tl0,a;取到的低位字节存入tl0
MOV 20h,a;取到的低位字节存入(20h)
setbtr0;启动timer0
d1:calldelay
inc30h;取简谱码指针加1
jmpnext
end0:clrtr0;停止计数器
MOV a,31h;载入计次指针
xrla,#00h;是否按第1次?
jnzend1;不是则跳至end1
jbKEY,$;按第2次?
calldelay1;消除抖动
jnbKEY,$;放开否?
inc31h;计次地址(31h)加1
MOV 30h,#lowsong1;第2首歌指针
jmpnext
end1:
MOV a,31h;载入计次指针
xrla,#01h;是否按第2次?
jnzend2;不是则跳至end2
jbKEY,$;按第3次?
calldelay1;消除抖动
jnbKEY,$;放开否?
inc31h;计次地址(31h)加1
MOV 30h,#lowsong2;第3首歌指针
jmpnext
end2:
MOV a,31h;载入计次指针
xrla,#02h;是否按第3次?
jnzend3;不是则跳至end3
jbKEY,$;按第4次?
calldelay1;消除抖动
jnbKEY,$;放开否?
inc31h;计次地址(31h)加1
MOV 30h,#lowsong3;第4首歌指针
jmpnext
end3:
MOV a,31h;载入计次指针
xrla,#03h;是否按第4次?
jnzend4;不是则跳至end4
jbKEY,$;按第5次?
calldelay1;消除抖动
jnbKEY,$;放开否?
inc31h;计次地址(31h)加1
MOV 30h,#lowsong4;第5首歌指针
jmpnext
end4:
MOV a,31h;载入计次指针
xrla,#01h;是否按第5次?
jnzend5;不是则跳至end2
jbKEY,$;按第6次?
calldelay1;消除抖动
jnbKEY,$;放开否?
inc31h;计次地址(31h)加1
MOV 30h,#lowsong5;第6首歌指针
jmpnext
end5:
jmpstart;回到第1次位置[!--empirenews.page--]
tim0:pushaCC;将a的值暂存于堆栈
pushpsw;将psw的值暂存于堆栈
setbrs0;设工作寄存器库1,rs0=1,rs1=0
clrrs1
MOV tl0,20h;重设计数值
MOV th0,21h
cplbuzzer;将p10位反相
clrrs0
poppsw;至堆栈取回psw的值
popacc;至堆栈取回a的值
reti;返回主程序
delay: MOV r7,#02;125秒
d2: MOV r4,#125
d3: MOV r3,#248
djnzr3,$
djnzr4,d3
djnzr7,d2
djnzr5,delay;决定节拍
ret
delay1: MOV r4,#20
d4: MOV r3,#248
djnzr3,$
djnzr4,d4
ret
org300h
table:
dw64260,64400,64524,64580
dw64684,64777,64820,64898
dw64968,65030,65058,65110
dw65157,65178,65217
song:;第一首歌哈巴狗
;1
db42h,42h,42h,52h,62h,04h
db62h,62h,62h,72h,84h,04h
db92h,92h,82h,72h,64h,04h
db82h,82h,52h,62h,44h,04h
;2
db42h,42h,42h,52h,62h,04h
db62h,62h,62h,72h,84h,04h
db92h,92h,82h,72h,64h,04h
db82h,82h,52h,62h,44h,04h
db00h
song1:;第2首歌生日快乐
;1
db82h,01h,81h,94h,84h
db0b4h,0a4h,04h
db82h,01h,81h,94h,84h
db0c4h,0b4h,04h
;2
db82h,01h,81h,0f4h,0d4h
db0b4h,0a4h,94h
db0e2h,01h,0e1h,0d4h,0b4h
db0c4h,0b4h,04h
db00h
song2:;第3首歌不倒翁
;1
db84h,94h,88h
db64h,54h,68h
db84h,64h,54h,44h
db52h,42h,52h,62h,48h
;2
db84h,94h,88h
db64h,54h,68h
db84h,64h,54h,44h
db52h,42h,52h,62h,48h
db00h
song3:;第4首歌妹妹背着洋娃娃
;1
db84h,02h,82h,64h,54h
db64h,54h,48h
db64h,02h,52h,44h,24h
db14h,24h,18h
;2
db24h,02h,22h,42h,24h
db44h,54h,68h
db54h,02h,52h,84h,84h
db54h,64h,48h
db00h
song4:;第5首歌两只老虎
;1
db44h,54h,64h,44h
db44h,54h,64h,44h
db64h,74h,88h
db64h,74h,88h
;2
db82h,92h,82h,72h,64h,44h
db82h,92h,82h,72h,64h,44h
db44h,84h,48h
db44h,14h,48h,00H
song5:;第6首歌三只小猫
;1
db62h,82h,82h,62h,98h
db92h,0b2h,0b2h,82h,98h
db62h,82h,82h,52h,68h
db92h,0b2h,0b2h,82h,98h
;2
db62h,82h,82h,62h,92h,92h,94h
db92h,0b2h,0b2h,92h,84h,94h
db0b8h,0b4h,04h
db00h
end