前言:
如今兄弟们对“c语言单片机音乐”可能比较看重,兄弟们都需要剖析一些“c语言单片机音乐”的相关文章。那么小编同时在网络上汇集了一些关于“c语言单片机音乐””的相关资讯,希望各位老铁们能喜欢,朋友们一起来学习一下吧!;音乐门铃程序
buzzer equ p2.4
KEY EQU P3.7
org 00h ;主程序起始地址
jmp start ;跳动主程序
org 0bh ;timer0中断起始地址
jmp tim0 ;跳至timer0在mode1
start: MOV tmod,#00000001b ;设timer0在mode1
MOV ie,#10000010b ;中断使能
jb KEY,$ ;第一次按to?
call delay1 ;消除抖动
jnb KEY,$ ;to放开?
MOV 31h,#00 ;按to次数计数指针初值为00h
start0:
MOV 30h,#low song ;取简谱码指针(第1首)
next: MOV a,30h
MOV dptr,#table
MOVc a,@a+dptr ;至相关页码,高4位为音符的高低
MOV r2,a ;低4位为音符的节拍
jz end0 ;检查简谱码是否已结束(有无00?)
anl a,#0fh ;取节拍(低4位)
MOV r5,a ;存入r5,节拍的时间
MOV a,r2
swap a
anl a,#0fh ;取音频值(高4位)
jnz sing ;是否为0,是0则不发音
clr tr0
jmp d1
sing: dec a ;因0不列入
MOV 22h,a ;存入(22h)
rl a ;乘2
MOV dptr,#table
MOVc a,@a+dptr ;至table取码,取t的值
MOV th0,a ;取到的高位字节存入th0
MOV 21h,a ;取到的高位字节存入(21h)
MOV a,22h ;再载入取到的音符码
rl a ;乘2
inc a ;加1
MOVc a,@a+dptr ;至table取相对的低位字节计数值
MOV tl0,a ;取到的低位字节存入tl0
MOV 20h,a ;取到的低位字节存入(20h)
setb tr0 ;启动timer0
d1: call delay
inc 30h ;取简谱码指针加1
jmp next
end0: clr tr0 ;停止计数器
MOV a,31h ;载入计次指针
xrl a,#00h ;是否按第1次?
jnz end1 ;不是则跳至end1
jb KEY,$ ;按第2次?
call delay1 ;消除抖动
jnb KEY,$ ;放开否?
inc 31h ;计次地址(31h)加1
MOV 30h,#low song1 ;第2首歌指针
jmp next
end1:
MOV a,31h ;载入计次指针
xrl a,#01h ;是否按第2次?
jnz end2 ;不是则跳至end2
jb KEY,$ ;按第3次?
call delay1 ;消除抖动
jnb KEY,$ ;放开否?
inc 31h ;计次地址(31h)加1
MOV 30h,#low song2 ;第3首歌指针
jmp next
end2:
MOV a,31h ;载入计次指针
xrl a,#02h ;是否按第3次?
jnz end3 ;不是则跳至end3
jb KEY,$ ;按第4次?
call delay1 ;消除抖动
jnb KEY,$ ;放开否?
inc 31h ;计次地址(31h)加1
MOV 30h,#low song3 ;第4首歌指针
jmp next
end3:
MOV a,31h ;载入计次指针
xrl a,#03h ;是否按第4次?
jnz end4 ;不是则跳至end4
jb KEY,$ ;按第5次?
call delay1 ;消除抖动
jnb KEY,$ ;放开否?
inc 31h ;计次地址(31h)加1
MOV 30h,#low song4 ;第5首歌指针
jmp next
end4:
MOV a,31h ;载入计次指针
xrl a,#01h ;是否按第5次?
jnz end5 ;不是则跳至end2
jb KEY,$ ;按第6次?
call delay1 ;消除抖动
jnb KEY,$ ;放开否?
inc 31h ;计次地址(31h)加1
MOV 30h,#low song5 ;第6首歌指针
jmp next
end5:
jmp start ;回到第1次位置
tim0: push acc ;将a的值暂存于堆栈
push psw ;将psw的值暂存于堆栈
setb rs0 ;设工作寄存器库1,rs0=1,rs1=0
clr rs1
MOV tl0,20h ;重设计数值
MOV th0,21h
cpl buzzer ;将p10位反相
clr rs0
pop psw ;至堆栈取回psw的值
pop acc ;至堆栈取回a的值
reti ;返回主程序
delay: MOV r7,#02 ;125秒
d2: MOV r4,#125
d3: MOV r3,#248
djnz r3,$
djnz r4,d3
djnz r7,d2
djnz r5,delay ;决定节拍
ret
delay1: MOV r4,#20
d4: MOV r3,#248
djnz r3,$
djnz r4,d4
ret
org 300h
table:
dw 64260,64400,64524,64580
dw 64684,64777,64820,64898
dw 64968,65030,65058,65110
dw 65157,65178,65217
song: ;第一首歌 哈 巴 狗
;1
db 42h,42h,42h,52h,62h,04h
db 62h,62h,62h,72h,84h,04h
db 92h,92h,82h,72h,64h,04h
db 82h,82h,52h,62h,44h,04h
;2
db 42h,42h,42h,52h,62h,04h
db 62h,62h,62h,72h,84h,04h
db 92h,92h,82h,72h,64h,04h
db 82h,82h,52h,62h,44h,04h
db 00h
song1: ;第2首歌 生 日 快 乐
;1
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
db 00h
song2: ;第3首歌 不 倒 翁
;1
db 84h,94h,88h
db 64h,54h,68h
db 84h,64h,54h,44h
db 52h,42h,52h,62h,48h
;2
db 84h,94h,88h
db 64h,54h,68h
db 84h,64h,54h,44h
db 52h,42h,52h,62h,48h
db 00h
song3: ;第4首歌 妹妹背着洋娃娃
;1
db 84h,02h,82h,64h,54h
db 64h,54h,48h
db 64h,02h,52h,44h,24h
db 14h,24h,18h
;2
db 24h,02h,22h,42h,24h
db 44h,54h,68h
db 54h,02h,52h,84h,84h
db 54h,64h,48h
db 00h
song4: ;第5首歌 两只老虎
;1
db 44h,54h,64h,44h
db 44h,54h,64h,44h
db 64h,74h,88h
db 64h,74h,88h
;2
db 82h,92h,82h,72h,64h,44h
db 82h,92h,82h,72h,64h,44h
db 44h,84h,48h
db 44h,14h,48h,00H
song5: ;第6首歌 三只小猫
;1
db 62h,82h,82h,62h,98h
db 92h,0b2h,0b2h,82h,98h
db 62h,82h,82h,52h,68h
db 92h,0b2h,0b2h,82h,98h
;2
db 62h,82h,82h,62h,92h,92h,94h
db 92h,0b2h,0b2h,92h,84h,94h
db 0b8h,0b4h,04h
db 00h
end