51單片機音樂門鈴的設(shè)計
buzzerequp2.4
KEYEQUP3.7
org00h;主程序起始地址
jmpstart;跳動主程序
org0bh;timer0中斷起始地址
jmptim0;跳至timer0在mode1
start: MOV tmod,#00000001b;設(shè)timer0在mode1
MOV ie,#10000010b;中斷使能
jbKEY,$;第一次按to?
calldelay1;消除抖動
jnbKEY,$;to放開?
MOV 31h,#00;按to次數(shù)計數(shù)指針初值為00h
start0:
MOV 30h,#lowsong;取簡譜碼指針(第1首)
next: MOV a,30h
MOV dptr,#table
MOV ca,@a+dptr;至相關(guān)頁碼,高4位為音符的高低
MOV r2,a;低4位為音符的節(jié)拍
jzend0;檢查簡譜碼是否已結(jié)束(有無00?)
anla,#0fh;取節(jié)拍(低4位)
MOV r5,a;存入r5,節(jié)拍的時間
MOV a,r2
swapa
anla,#0fh;取音頻值(高4位)
jnzsing;是否為0,是0則不發(fā)音
clrtr0
jmpd1
sing:deca;因0不列入
MOV 22h,a;存入(22h)
rla;乘2
MOV dptr,#table
MOV ca,@a+dptr;至table取碼,取t的值
MOV th0,a;取到的高位字節(jié)存入th0
MOV 21h,a;取到的高位字節(jié)存入(21h)
MOV a,22h;再載入取到的音符碼
rla;乘2
inca;加1
MOV ca,@a+dptr;至table取相對的低位字節(jié)計數(shù)值
MOV tl0,a;取到的低位字節(jié)存入tl0
MOV 20h,a;取到的低位字節(jié)存入(20h)
setbtr0;啟動timer0
d1:calldelay
inc30h;取簡譜碼指針加1
jmpnext
end0:clrtr0;停止計數(shù)器
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;設(shè)工作寄存器庫1,rs0=1,rs1=0
clrrs1
MOV tl0,20h;重設(shè)計數(shù)值
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;決定節(jié)拍
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