正文

51单片机基础实验板-六首唱歌的音乐程序按P2.4循环2012-10-23 08:54:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/enc999/53848.html

分享到:

51单片机基础实验板-六首唱歌的音乐程序按P2.4循环

;六首唱歌的音乐程序按P2.4循环
 K1 bit P2.4
 SPK bit P1.7
            org          00h                  ;主程序起始地址
            jmp          start                ;跳动主程序
            org          0bh                  ;timer0中断起始地址
            jmp          tim0                 ;跳至timer0在mode1
start:      mov          tmod,#00000001b      ;设timer0在mode1
            mov          ie,#10000010b        ;中断使能
            jb           K1,$               ;第一次按to?
            call         delay1               ;消除抖动
            jnb          K1,$               ;to放开?
   mov          p2,#0f9h             ;数码管显示1
   clr          p0.1                 ;数码管显示
            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           K1,$               ;按第2次?
            call         delay1               ;消除抖动
            jnb          K1,$               ;放开否?   
   mov          p2,#0a4h             ;数码管显示2
   clr          p0.1                 ;数码管显示
            inc          31h                  ;计次地址(31h)加1
            mov          30h,#low song1       ;第2首歌指针
            jmp          next
end1:
            mov          a,31h                ;载入计次指针
            xrl          a,#01h               ;是否按第2次?
            jnz          end2                 ;不是则跳至end2
            jb           K1,$               ;按第3次?
            call         delay1               ;消除抖动
            jnb          K1,$               ;放开否?   
     mov          p2,#0b0h             ;数码管显示3
     clr          p0.1                 ;数码管显示   
            inc          31h                  ;计次地址(31h)加1
            mov          30h,#low song2       ;第3首歌指针
            jmp          next
end2:
            mov          a,31h                ;载入计次指针
            xrl          a,#02h               ;是否按第3次?
            jnz          end3                ;不是则跳至end3
            jb           K1,$               ;按第4次?
            call         delay1               ;消除抖动
            jnb          K1,$               ;放开否?   
   mov          p2,#99h             ;数码管显示4
   clr          p0.1                 ;数码管显示   
            inc          31h                  ;计次地址(31h)加1
            mov          30h,#low song3       ;第4首歌指针
            jmp          next
end3:
            mov          a,31h                ;载入计次指针
            xrl          a,#03h               ;是否按第4次?
            jnz          end4                 ;不是则跳至end4
            jb           K1,$               ;按第5次?
            call         delay1               ;消除抖动
            jnb          K1,$               ;放开否?   
   mov          p2,#92h             ;数码管显示5
   clr          p0.1                 ;数码管显示   
            inc          31h                  ;计次地址(31h)加1
            mov          30h,#low song4      ;第5首歌指针
            jmp          next
end4:
            mov          a,31h                ;载入计次指针
            xrl          a,#04h               ;是否按第5次?
            jnz          end5                 ;不是则跳至end2
            jb           K1,$               ;按第6次?
            call         delay1               ;消除抖动
            jnb          K1,$               ;放开否?   
   mov          p2,#82h             ;数码管显示6
   clr          p0.1                 ;数码管显示
            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          SPK                 ;将扬声器 蜂鸣器 SPK 位反相
            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,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,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
            db           00h
            ;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
   db           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

阅读(1784) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册