如何编写51单片机音乐程序
时间:11-13
来源:互联网
点击:
187D3: MOV R3,#248DJNZ R3,$DJNZ R4,D3DJNZ R7,D2RET4.程序范例ORG 0000H ;主程序起始地址SJMP START ;跳至主程序ORG 000BH ;TIMER0中断起 始地址LJMP TIM0 ;跳至TIMER0中断子程序START: MOV TMOD,#01H ;设T0在M1MOV IE,#82H ;中断使能START0:MOV 30H,#00 ;取简谱码指针NEXT: MOV A,30H ;简谱码指针载入AMOV DPTR,#TAB ;至TAB取简谱码MOVC A,@A+DPTR ;MOV R2,A ;渠道的简谱码暂存于R2JZ END0 ;是否渠道00(结束码)ANL A,#0FH ;不是,则取低4位(节拍码)MOV R5,A ;将节拍码存入R5MOV A,R2 ;将取到的简谱码再载入ASWAP A ;高低4位交换ANL A,#0FH ;取低4位(音符码)JNZ SING ;取到的音符码是否为0?CLR TR0 ;开始,则不发音SJMP D1 ;跳至D1SING: DEC A ;取到的音符码减1(不含0)MOV 22H,A ;存入(22H)RL A ;乘2MOV DPTR,#TAB1 ;至TABLE1取相对的高位字节计数值MOVC A,@A+DPTR ;MOV TH0,A ;取到的高位字节存入TH0MOV 21H,A ;取到的高位字节存入(21H)MOV A,22H ;在载入取到的音符码RL A ;乘2INC A ;加1MOVC A,@A+DPTR ;至TABLE1取相对的低位字节计数值MOV TL0,A ;取到的低位字节存入TL0MOV 20H,A ;取到的低位字节存入(20H)SETB TR0 ;启动TIMER0D1: LCALL DELAY ;其本单位时间1/4拍187毫秒INC 30H ;取简谱码指针加1JMP NEXT ;取下一个简谱码END0: CLR TR0 ;停止TIMER0JMP START0 ;重复循环TIM0: PUSH ACC ;将A的值暂存于堆栈PUSH PSW ;将PSW的值暂存于堆栈MOV TL0,20H ;重设计数值MOV TH0,21H ;CPL P3.0 ;将P3.0位反相,控制蜂鸣器发声POP PSW ;至堆栈取回PSW的值POP ACC ;至堆栈取回A的值RETI DELAY:MOV R7,#02 D2: MOV R4,#187 D3: MOV R3,#248 DJNZ R3,$ DJNZ R4,D3 DJNZ R7,D2 RET TAB1: ;决定节拍DW 64260,64400,64521,64580 DW 64684,64777,64820,64898DW 64968,65030,65058,65110DW 65157,65178,65217 TAB: ;乐曲名称《梁祝》DB 02H,82H,62H,52H,48H,02H,52H,32H,22H,18HDB 83H,91H,72H,62H,51H,61H,71H,61H,83H,61HDB 81H,51H,61H,71H,61H,51H,46H,82H,32H,52HDB 22H,42H,16H,21H,41H,18H,0E4H,13H,21H,43HDB 51H,21H,41H,12H,83H,81H,61H,81H,58H,53HDB 61H,31H,22H,13H,21H,42H,52H,0E2H,42H,21HDB 11H,91H,41H,18H,63H,81H,32H,52H,21H,41H,DB 16H,0E4H,11H,21H,31H,51H,26H,11H,21H,43HDB 51H,82H,62H,52H,61H,51H,42H,21H,11H,0E4HDB 44H,21H,41H,21H,11H,0E1H,11H,21H,41H,18HDB 61H,81H,51H,61H,51H,41H,32H,21H,41H,18HDB 08H,0H,04H ;曲子最后静音5拍长的时间DB 00H ;乐曲结束END
51单片机音乐程 相关文章:
- Windows CE 进程、线程和内存管理(11-09)
- RedHatLinux新手入门教程(5)(11-12)
- uClinux介绍(11-09)
- openwebmailV1.60安装教学(11-12)
- Linux嵌入式系统开发平台选型探讨(11-09)
- Windows CE 进程、线程和内存管理(二)(11-09)