微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > 基于51单片机的音乐播放程序

基于51单片机的音乐播放程序

时间:11-26 来源:互联网 点击:

;本程序的单片机晶振采用11.0592MHZ

;l程序中共有两首歌,后面一首为世上只有妈妈好.两首歌曲表的格式不同,若有任何疑问请留言.

;刀郎的<2002年的第一场雪>>
SPK BIT P0.0

ORG 0000H
LJMP MAIN
ORG 000BH
LJMP TIMER0

MAIN:
MOV TMOD,#01H ;置CT0定时工作方式1
MOV IE,#82H;CPU开中断,CT0开中断

MOV R5,#0
PLAY:
MOV A,R5
MOV DPTR,#TABLE1
MOVC A,@A+DPTR
MOVR6,A
INC R5
MOV A,R5
MOVC A,@A+DPTR
MOVR7,A
INC R5
MOV A,R5
MOVC A,@A+DPTR
JNZ PLAY1
MOV R5,#0
LJMP NEXT
PLAY1:
MOV B,#3
MUL AB
MOV R2,A
MOV B,#7
MOV A,R7
MUL AB
ADD A,R6
DEC A
MOV R6,A
MOV DPTR,#FREQH
MOVC A,@A+DPTR
MOV R4,A
MOV DPTR,#FREQL
MOV A,R6
MOVC A,@A+DPTR
MOV R3,A
LCALL SONG;调用音符播放程序
INC R5
SJMP PLAY;转入调用下一个音符OV R3,A


MOV R5,#00H
NEXT:
MOV A,R5;R5存放取数的变地址
MOV DPTR,#TABLE ;DPTR置音高节拍表首地址
MOVC A,@A+DPTR ;从音高节拍表取中取音高高位参数
MOV R4,A;R4存放音高高位参数
INC R5
MOV A,R5
MOVC A,@A+DPTR ;从音高节拍表取中取音高低位参数
MOV R3,A;R3存放音高低位参数
INC R5
MOV A,R5
MOVC A,@A+DPTR ;从音高节拍表取中取节拍参数
JNZ NEXT1
MOV R5,#00H
LJMP PLAY;唱完后循环(节拍参数为#00H表示歌曲结束)
NEXT1:
MOV R2,A;R2存放节拍参数
LCALL SONG;调用音符播放程序
INC R5
SJMP NEXT;转入调用下一个音符
SONG:
MOV TH0,R4
MOV TL0,R3
SETB TR0;启动CT0(开始唱某一音符)
DELAY:
MOV R1,#85H;节拍延时(即某一音符唱多长时间)
DEL1:
MOV R0,#0FFH
DEL0:
NOP
NOP
NOP
DJNZ R0,DEL0
DJNZ R1,DEL1
DJNZ R2,DELAY
CLR TR0;禁止CT0(某一音符唱完后停止)
RET
TIMER0:;定时中断程序(用于决定某一音符唱多高)
CPL SPK
MOV TH0,R4
MOV TL0,R3
RETI
TABLE: ;音高节拍表,休止符用"FFH,FFH",结束用节拍为00H表示
DB 0FFH,0FFH,01H,0FDH, 08H,02H,0FDH, 08H,02H,0FDH, 08H,02H,0FDH, 08H,02H,0FDH, 08H,02H,0FDH, 08H,02H
DB 0FDH, 5BH,04H,0FDH, 08H,02H,0FCH,0ABH,02H,0FCH, 0BH,02H,0FCH, 0BH,02H,0FFH,0FFH,01H,0FCH, 0BH,02H
DB 0FCH,0ABH,02H,0FCH,0ABH,02H,0FCH,0ABH,02H,0FCH,0ABH,02H,0FCH,0ABH,04H,0FCH,0ABH,02H,0FCH,0ABH,02H
DB 0FBH, 8FH,02H,0FCH, 0BH,02H,0FCH, 0BH,04H,0FFH,0FFH,01H
DB 0FDH, 08H,02H,0FDH, 08H,02H,0FDH, 08H,02H,0FDH, 08H,04H,0FDH, 08H,02H,0FDH, 08H,02H
DB 0FDH, 5BH,04H,0FDH, 08H,02H,0FCH,0ABH,04H,0FDH, 08H,06H
DB 0FBH,08FH,02H,0FBH,08FH,02H,0FBH,08FH,02H,0FBH,08FH,02H,0FBH,08FH,02H,0FBH, 03H,02H,0FAH, 14H,02H
DB 0FCH,0ABH,04H,0FCH,0ABH,02H,0FCH,0ABH,02H,0FCH,0ABH,02H,0FCH, 0BH,02H,0FCH, 0BH,04H
DB 0FFH,0FFH,01H,0FDH, 08H,02H,0FDH, 08H,02H,0FDH, 08H,02H,0FDH, 08H,02H,0FDH, 08H,02H,0FDH, 08H,02H
DB 0FDH, 5BH,04H,0FDH, 08H,02H,0FCH,0ABH,04H,0FDH, 08H,06H
DB 0FBH,08FH,02H,0FBH,08FH,02H,0FBH,08FH,02H,0FBH,08FH,02H,0FBH,08FH,02H,0FBH, 03H,02H,0FAH, 14H,02H
DB 0FCH,0ABH,04H,0FCH,0ABH,02H,0FCH,0ABH,02H,0FCH,0ABH,02H,0FCH, 0BH,02H,0FCH, 0BH,04H, 00H, 00H,00H

TABLE1:
DB6,2,3,5,2,1,3,2,2, 5,2,2, 1,3,2, 6,2,1, 5,2,1
DB6,2,4,3,2,2,5,2,1, 6,2,1, 5,2,2, 3,2,2, 1,2,1
DB6,1,1,5,2,1,3,2,1, 2,2,4, 2,2,3, 3,2,1, 5,2,2
DB5,2,1,6,2,1,3,2,2, 2,2,2, 1,2,4, 5,2,3, 3,2,1
DB2,2,1,1,2,1,6,1,1, 1,2,1, 5,1,6, 0,0,0
; 音阶频率表
FREQH:
DB0F2H,0F3H,0F5H,0F5H,0F6H,0F7H,0F8H
DB0F9H,0F9H,0FAH,0FAH,0FBH,0FBH,0FCH, 0FCH ;1,2,3,4,5,6,7,8,i
DB0FCH,0FDH,0FDH,0FDH,0FDH,0FEH
DB0FEH,0FEH,0FEH,0FEH,0FEH,0FEH,0FFH
FREQL:
DB42H, 0C1H, 17H, 0B6H,0D0H,0D1H,0B6H
DB21H, 0E1H, 8CH, 0D8H,68H, 0E9H, 5BH,8FH ;1,2,3,4,5,6,7,8,i
DB0EEH,44H,6BH,0B4H,0F4H,2DH
DB47H, 77H,0A2H,0B6H,0DAH,0FAH,16H
END


END


Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top