找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 7743|回复: 0
打印 上一主题 下一主题
收起左侧

51单片机蜂鸣器播放歌曲汇编源码和仿真

[复制链接]
跳转到指定楼层
楼主
一个用51单片机播放歌曲的小程序


单片机汇编语言源程序如下:
  1.   org 0000H
  2. sjmp start

  3. org  0040H
  4. start: mov R3,#-1;

  5. loop4:
  6.        jb p1.0,stopzero ; the first step: see if button1 on. it is 0 jump to stopzero  (2us)
  7.            inc R3  ;R3=0 at first   (1us)
  8.        sjmp mod1;butoon=1  jump to paly the song (2us)

  9. stopzero:  ;(5us)
  10.          jb p1.1,stopone         ;see if button2 is on. it's 0 jump to stoptone(2us)
  11.                  inc R3  ;  R3=0 at first   (1us)
  12.          sjmp mod1         ; jump to paly the song (2us)
  13. stopone:; (5us)
  14.           jb p1.3,loop4         ;see if button3 is on. it's 0 jump to stoptwo(2us)
  15.           inc R3         
  16.                   sjmp mod1         ; jump to paly the song (2us)
  17.                  

  18.                              

  19. mod1:        mov a,R3       ;(1us)
  20. chec1:       jb p1.0,chec2         ;(2us)find which song we are playing (2us)
  21.                          sjmp b1                ;(2us)

  22. chec2:             jb p1.1,chec3
  23.                          sjmp b2

  24. chec3:             jb p1.3,chec1
  25.                          sjmp b3

  26. b1:              mov b,#11        ;(1us)  b=first song's notes number
  27.                          sjmp di ;(2us)

  28. b2:              mov b,#30 ;b=second song's notes number
  29.                          sjmp di

  30. b3:              mov b,#30         ;b= third song's notes number
  31.                      sjmp di                 

  32. di:          div ab         ;divide a by b ,remainder is in b (4us)
  33.                          mov a,b        ; put remainder in a  (1us)
  34.                          mov R3,b        ;put remainder in R3(which will be divided later  (1us)
  35.                         
  36.                           
  37. check1:      jb p1.0,check2 ;to find which song is playing and get the duration table (
  38.                          sjmp songdu1

  39. check2:             jb p1.1,check3
  40.                          sjmp songdu2

  41. check3:             jb p1.3,check1
  42.                          sjmp songdu3

  43. songdu1:                  mov dptr,#durationone         ;get duration table (2us)
  44.                                          sjmp get

  45. songdu2:                  mov dptr,#durationtwo         ;get duration table
  46.                      sjmp get

  47. songdu3:                  mov dptr,#durationthree         ;get duration table
  48.                      sjmp get
  49.                                           
  50. get:             movc a,@a+dptr          ;get the data in duration table one by one(2us)
  51.                                  mov R2,a; put a in R2 (1us)
  52.                                  
  53. ;loop3(24+3+notes's half period)
  54. loop3:             mov a,b                  ;give reminder to a agian to get notes table(1us)
  55. che1:      jb p1.0,che2
  56.                          sjmp songno1

  57. che2:             jb p1.1,che3
  58.                          sjmp songno2

  59. che3:             jb p1.3,che1
  60.                          sjmp songno3

  61. songno1:                  mov dptr,#notesone         ;get duration table (2us)
  62.                                          sjmp getn

  63. songno2:                  mov dptr,#notestwo         ;get duration table
  64.                      sjmp getn

  65. songno3:                  mov dptr,#notesthree         ;get duration table
  66.                      sjmp getn

  67.         
  68. getn:                 movc a,@a+dptr        ;  (2us)
  69.                  mov R1,a  ;put note now in R1                        (1us)

  70. loop2:mov R0,#10  ;the part of play every note:(2*R0+2+1)*R1
  71. loop1:  djnz R0,$
  72.            djnz R1,loop2
  73.                    cpl p3.1                  
  74.                    djnz R2, loop3  ;start make delay (2us) R2*(loop3+2us)
  75.               
  76.                    mov R1,255                  ;make rest between two notes (1us)
  77.                
  78. delay2:mov R0,#255
  79. delay1:djnz R0,$
  80.            djnz R1,delay2
  81.                    acall loop4 ; get all steps again and agian
  82.                  sjmp $

  83. notesone:  db 66,83,105,83,74,56,74,66,74,105,83   ; the delay time of the music score one by one      
  84. ……………………

  85. …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码

所有资料51hei提供下载:
lab2-8051.rar (65.72 KB, 下载次数: 40)


分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表