资源描述:
《单片机播放音乐》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、单片机播放音乐 以8051单片机为例,任选两个定时器/计数器T0,T1中的一个,设为定时方式1。利用定时器产生各音符对应频率的方波,有P1.0口驱动喇叭发音。D200为200ms延时程序,以每拍800ms为例,一拍需循环调用D200延时子程序四次。同理,半拍,3/4拍,1/4拍都可用同样的方法实现。1,2,3,4,5,6,7各音符的频率依次为524,588,660,698,784,880,988(HZ),因此可算出各音符对定时器的初始值。结果依次为FE25H,FE57H,FE84H,FE98H,FEC0H,FE
2、E3H,FF01H。在电脑上用Proteus软件仿真非常方便。以“飘雪”这首歌为例,汇编语言程序如下: ORG 0000H LJMP START ORG 001BH MOV TH1,R1 MOV TL1,R0 CPL P1.0 RETI ORG 0100HSTART:MOV TMOD,#10H MOV IE,#88H MOV DPTR,#TABLOOP:CLR A MOVC A,@A+DPTR MOV R1,A INC DPTR CLR A MOVC A,@A+DPTR
3、 MOV R0,A ORL A,R1 JZ NEXT0 MOV A,R0 ANL A,R1 CJNE A,#0FFH,NEXT SJMPSTARTNEXT:MOV TH1,R1 MOV TL1,R0 SETB TR1 SJMP NEXT1NEXT0:CLR TR1NEXT1:CLR A INC DPTR MOVC A,@A+DPTR MOV R2,ALOOP1:LCALL D200 DJNZ R2,LOOP1 INC DPTR AJMP LOOPD20
4、0: MOV R4,#81HD200B:MOV A,#0FFHD200A:DEC A JNZ D200A DEC R4 CJNE R4,#00H,D200B RETTAB: DB 0FDH,08H,02H,0FDH,80H,02H,0FDH,0C6H,02H,0FEH,25H,02H,0FEH,84H,02H,0FEH,0C0H,02H,0FEH,0E3H,02H,0FFH,10H,02H DB 0FFH,42H,02H,0FEH,0E3H,04H,0FDH,80H,02H,0
5、FDH,0C6H,04H,0FFH,42H,02H,0FDH,80H,02H DB 0FDH,0C6H,02H,0FDH,80H,02H,0FDH,0C6H,02H,0FEH,25H,02H,0FEH,98H,02H,0FEH,84H,02H,0FEH,0E3H,02H,0FEH,0C0H,02H DB 0FFH,42H,02H,0FEH,98H,04H,0FFH,60H,02H,0FFH,42H,04H,0FFH,10H,02H,0FEH,98H,02H DB 0FEH,0E3H
6、,02H,0FFH,01H,02H,0FEH,0C0H,02H,0FEH,0E3H,02H,0FEH,98H,02H,0FEH,0C0H,02H,0FEH,84H,04H DB 0FEH,84H,04H,0FEH,57H,02H,0FEH,84H,02H,0FEH,25H,04H,00H,00H,04H DB 0FEH,57H,04H,0FEH,0C0H,02H,0FEH,57H,02H,0FEH,84H,04H,0FEH,57H,04H DB 0FEH,25H,04H,00H,0
7、0H,04H,0FEH,57H,04H,0FDH,0C6H,04H DB 0FEH,07H,04H,00H,00H,04H,0FEH,25H,04H,0FEH,57H,04H DB0FEH,84H,02H,0FEH,0C0H,02H,0FEH,0E3H,01H,0FFH,10H,03H,0FEH,0C0H,04H,0FEH,57H,02H,0FEH,84H,02H DB0FEH,57H,02H,0FEH,57H,02H,0FEH,25H,02H,0FEH,57H,02H,0FEH,
8、84H,04H,00H,00H,04H DB0FEH,84H,02H,0FEH,0C0H,02H,0FEH,0E3H,01H,0FFH,10H,03H,0FEH,0C0H,04H,0FEH,57H,02H,0FEH,84H,02H DB0FEH,57H,02H,0FEH,84H,02H,0FEH,25H,02H,0FEH,57H,01H,0FEH,25H,01H,0FEH,25