c51单片机音乐门铃C语言程序,单片机六首歌曲的音乐门铃程序

单片机六首歌曲的音乐门铃程序

来源:--

作者:--

浏览:1346

时间:2016-08-10 14:18

标签:

摘要:

buzzer        equ        p2.4 KEY        EQU

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

MOV c         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

MOV c         a,@a+dptr            ;至table取码,取t的值

MOV           th0,a                ;取到的高位字节存入th0

MOV           21h,a                ;取到的高位字节存入(21h)

MOV           a,22h                ;再载入取到的音符码

rl           a                    ;乘2

inc          a                    ;加1

MOV c         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

单片机 六首歌曲的音乐门铃程序

分享到:

90da879458bf0f4491d5f6f37b03a631.png

ORG 00H ;初始化程序 JMP START ORG 0BH LJMP TIM0 START:MOV TMOD,#00000001B ;主程序 MOV IE,#10000010B JB P3.5, $ CALL DELAY1 JNB P3.5, $ MOV 31H,#00 START0: MOV 30H,#SONG NEXT: MOV A,30H MOV DPTR,# TABLE MOVC A,@A+DPTR MOV R2,A JZ END0 ANL A,#0FH MOV R5,A MOV A,R2 SWAP A ANL A,#0FH JNZ SING CLR TR0 JMP D1 SING: DEC A MOV 22H,A RL A MOV DPTR,#TABLE MOVC A,@A+DPTR MOV TH0,A MOV 21H,A MOV A,22H RL A INC A MOVC A,@A+DPTR MOV TL0,A MOV 20H,A SETB TR0 D1: CALL DELAY INC 30H JMP NEXT END0: CLR TR0 MOV A,31H XRL A,#00H JNZ END1 JB P3.5,$ CALL DELAY1 JNB P3.5,$ INC 31H MOV 30H,#SONG1 JMP NEXT END1: MOV A,31H XRL A,#01H JNZ END2 JB P3.5,$ CALL DELAY1 JNB P3.5,$ INC 31H MOV 30H,#SONG2 JMP NEXT END2: MOV A,31H XRL A,#02H JNZ END3 JB P3.5,$ CALL DELAY1 JNB P3.5,$ INC 31H MOV 30H,#SONG3 JMP NEXT END3: MOV A,31H XRL A,#03H JNZ END4 JB P3.5,$ CALL DELAY1 JNB P3.5,$ INC 31H MOV 30H,#SONG4 JMP NEXT END4: MOV A,31H XRL A,#04H JNZ END5 JB P3.5,$ CALL DELAY1 JNB P3.5,$ INC 31H MOV 30H,#SONG5 JMP NEXT END5: JMP START TIM0: PUSH ACC ;定时器T0子程序 PUSH PSW SETB RS0 CLR RS1 MOV TL0,20H MOV TH0,21H CPL P1.0 POP PSW POP ACC RETI DELAY: ;延时子程序 MOV R7,#02 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 ;定时常数T值表 TABLE: DW 64260,64400,64524,64580 DW 64684,64777,64820,64898 DW 64968,65030,65058,65110 DW 65157,65178,65217 ;音符节拍码数据表 SONG: ;生日快乐 ;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 SONG1: ;两只老虎 ;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 DB 00H SONG2: ;三只小猫 ;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 SONG3: ;哈巴狗 ;1 DB 42H,42H,42H,52H,64H,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,64H,04H DB 62H,62H,62H,72H,84H,04H DB 92H,92H,82H,72H,64H,04H DB 82H,82H,52H,62H,44H,04H DB 00H SONG4: ;不倒翁 ;1 DB 84H,94H,88H DB 64H,54H,68H DB 84H,64H,54H,44H DB 62H,82H,42H,62H,58H ;2 DB 84H,94H,88H DB 64H,54H,68H DB 84H,64H,54H,44H DB 52H,42H,52H,62H,48H DB 00H SONG5: ;妹妹背着洋娃娃 ;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 END
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值