AT89C51音乐盒

下面是以AT89C51为基础设计的一个音乐播放器,能够控制8首音乐的播放以及歌名显示,基本电路设计图如下:
 
按键分别控制上一曲、下一曲,开机启动时显示欢迎辞后开始音乐播放 。以下是音乐盒的所有代码(暂时只安排了2首歌的乐谱):
$MOD51
;
RS EQU P3.0
RW EQU P3.1
EN EQU P3.4
;
ORG 00H
JMP START
ORG 03H
JMP BACK_INT
ORG 0BH
JMP TONE_INT ;音调
ORG 13H
JMP NEXT_INT
ORG 1BH
JMP BEAT_INT ;节奏
START:
MOV R0,00H
DJNZ R0,$
MOV SP,#60H
CALL READY_OK
CALL FIRST_SONG
LOOP1:
CALL LOAD_TONE
CALL LOAD_BEAT
JNB F0,$
CLR F0
JMP LOOP1
LOAD_TONE: ;载入音调
CALL TONE_DPTR
MOV A,R7
MOVC A,@A+DPTR
JNZ CONTINUE
MOV R6,#0
MOV R7,#0
JMP LOAD_TONE
CONTINUE:
CALL BABY_DPTR
RL A
DEC A
DEC A
MOV R4,A
MOVC A,@A+DPTR
MOV 20H,A
MOV TH0,A
INC R4
MOV A,R4
MOVC A,@A+DPTR
MOV 21H,A
MOV TL0,A
INC R7
SETB TR0
RET
LOAD_BEAT: ;载入节奏
CALL BEAT_DPTR
MOV A,R6
INC R6
MOVC A,@A+DPTR
MOV R5,A
MOV TH1,#11
MOV TL1,#220
SETB TR1
RET
TONE_INT:
CLR TR0
CPL P1.0
MOV A,20H
MOV TH0,A
MOV A,21H
MOV TL0,A
SETB TR0
RETI
BEAT_INT:
CLR TR1
DJNZ R5,AGAIN
CLR TR0
SETB F0
JMP EXIT
AGAIN: ;节奏为0.125s的倍数
MOV TH1,#11
MOV TL1,#220
SETB TR1
EXIT: RETI
;**************************************************************************************************
READY_OK: ;系统初始化
MOV TMOD,#11H
MOV IE,#8FH
MOV IP,#05H
MOV 22H,#01H
CLR F0
MOV R7,#0
MOV R6,#0
MOV A,#00111000B ;设定8BIT,2行,5X7字型
CALL WR_INST
MOV A,#00001000B ;关闭显示屏
CALL WR_INST
MOV A,#00000001B ;清除显示屏
CALL WR_INST
MOV A,#00001100B ;开启显示屏
CALL WR_INST
MOV A,#00000110B ;设定AC+1
CALL WR_INST
MOV A,#10000000B ;设定第一行起始地址
CALL WR_INST
MOV DPTR,#LINE
MOV R0,#40
CALL WR_STRING;
MOV A,#11000000B ;设定第二行起始地址
CALL WR_INST
MOV DPTR,#LINE0
MOV R0,#40
CALL WR_STRING
CALL WR_YIDONG
SETB C ;初始化完毕
RET
;
WR_YIDONG: ;欢迎词移动显示
MOV 27H,#23
LOOP2:
MOV A,#00011000B ;显示屏左移
CALL WR_INST
CALL DELAY2
DJNZ 27H,LOOP2
MOV A,#00000001B
CALL WR_INST
RET
;
WR_LINE:
MOV A,#10000000B ;设定第一行起始地址
CALL WR_INST
CALL LINE1_DPTR
MOV R0,#40
CALL WR_STRING;
MOV A,#11000000B ;设定第二行起始地址
CALL WR_INST
CALL LINE2_DPTR
MOV R0,#40
CALL WR_STRING
RET
;
FIRST_SONG:
MOV A,#10000000B ;设定第一行起始地址
CALL WR_INST
MOV DPTR,#LINE1
MOV R0,#40
CALL WR_STRING;
MOV A,#11000000B ;设定第二行起始地址
CALL WR_INST
MOV DPTR,#LINE9
MOV R0,#40
CALL WR_STRING
【AT89C51音乐盒】RET
;
WR_INST: ;显示器控制命令(写入、执行)
CALL CHECK_BF
CLR RS
CLR RW
SETB EN
MOV P2,A
CLR EN
RET
CHECK_BF:
MOV 23H,A
BUSY:
CLR RS
SETB RW
SETB EN
MOV A,P2
CLR EN
JB ACC.7,BUSY
MOV A,23H
CALL DELAY1
RET
;
WR_STRING: ;显示内容
MOV R1,#0
NEXT:
MOV A,R1
MOVC A,@A+DPTR
CALL WR_DATA
INC R1
DJNZ R0,NEXT
RET
;
WR_DATA:
CALL CHECK_BF
SETB RS
CLR RW
SETB EN
MOV P2,A
CLR EN
RET
;
DELAY1:
MOV 24H,#15
$1: MOV 25H,#200
DJNZ 25H,$
DJNZ 24H,$1
RET
;
DELAY2:
MOV 24H,#6
$3: MOV 25H,#0
$2: MOV 26H,#0
DJNZ 26H,$
DJNZ 25H,$2
DJNZ 24H,$3
RET
;**************************************************************************************************
NEXT_INT: ;下一曲(22H决定歌曲序列)
JNC NEXT_END ;查检是否初始化完毕
MOV A,22H
RL A
MOV 22H,A
MOV R6,#0
MOV R7,#0
CALL WR_LINE ;下一歌曲名
NEXT_END: RETI
;
BACK_INT:
JNC BACK_END
MOV A,22H
RR A
MOV 22H,A
MOV R6,#0
MOV R7,#0
CALL WR_LINE
BACK_END: RETI
;**************************************************************************************************
TONE_DPTR: ;确定哪首歌的乐谱,根据乐谱查表获得该调振动频率对应的时间表
JB 22H.0,L1
JB 22H.1,L2
JB 22H.2,L3
JB 22H.3,L4
JB 22H.4,L5
JB 22H.5,L6
JB 22H.6,L7
JB 22H.7,L8
L1: MOV DPTR,#TONE1
JMP TONE_END
L2: MOV DPTR,#TONE2
JMP TONE_END
L3: MOV DPTR,#TONE3
JMP TONE_END
L4: MOV DPTR,#TONE4
JMP TONE_END
L5: MOV DPTR,#TONE5
JMP TONE_END
L6: MOV DPTR,#TONE6
JMP TONE_END
L7: MOV DPTR,#TONE7
JMP TONE_END
L8: MOV DPTR,#TONE8
JMP TONE_END
TONE_END: RET
;**************************************************************************************************
BABY_DPTR: ;确定哪首歌的音调
JB 22H.0,LL1
JB 22H.1,LL2
JB 22H.2,LL3
JB 22H.3,LL4
JB 22H.4,LL5
JB 22H.5,LL6
JB 22H.6,LL7
JB 22H.7,LL8
LL1: MOV DPTR,#BABY1
JMP BABY_END
LL2: MOV DPTR,#BABY2
JMP BABY_END
LL3: MOV DPTR,#BABY3
JMP BABY_END
LL4: MOV DPTR,#BABY4
JMP BABY_END
LL5: MOV DPTR,#BABY5
JMP BABY_END
LL6: MOV DPTR,#BABY6
JMP BABY_END
LL7: MOV DPTR,#BABY7
JMP BABY_END
LL8: MOV DPTR,#BABY8
JMP BABY_END
BABY_END: RET
;**************************************************************************************************
BEAT_DPTR: ;确定哪首歌的节奏
JB 22H.0,LW1
JB 22H.1,LW2
JB 22H.2,LW3
JB 22H.3,LW4
JB 22H.4,LW5
JB 22H.5,LW6
JB 22H.6,LW7
JB 22H.7,LW8
LW1: MOV DPTR,#BEAT1
JMP BEAT_END
LW2: MOV DPTR,#BEAT2
JMP BEAT_END
LW3: MOV DPTR,#BEAT3
JMP BEAT_END
LW4: MOV DPTR,#BEAT4
JMP BEAT_END
LW5: MOV DPTR,#BEAT5
JMP BEAT_END
LW6: MOV DPTR,#BEAT6
JMP BEAT_END
LW7: MOV DPTR,#BEAT7
JMP BEAT_END
LW8: MOV DPTR,#BEAT8
JMP BEAT_END
BEAT_END: RET
;**************************************************************************************************
LINE1_DPTR: ;确定哪首歌的曲名
JB 22H.0,LN1
JB 22H.1,LN2
JB 22H.2,LN3
JB 22H.3,LN4
JB 22H.4,LN5
JB 22H.5,LN6
JB 22H.6,LN7
JB 22H.7,LN8
LN1: MOV DPTR,#LINE1
JMP LINE_END
LN2: MOV DPTR,#LINE2
JMP LINE_END
LN3: MOV DPTR,#LINE3
JMP LINE_END
LN4: MOV DPTR,#LINE4
JMP LINE_END
LN5: MOV DPTR,#LINE5
JMP LINE_END
LN6: MOV DPTR,#LINE6
JMP LINE_END
LN7: MOV DPTR,#LINE7
JMP LINE_END
LN8: MOV DPTR,#LINE8
JMP LINE_END
;
LINE2_DPTR:
JB 22H.0,LN9
JB 22H.1,LN10
JB 22H.2,LN11
JB 22H.3,LN12
JB 22H.4,LN13
JB 22H.5,LN14
JB 22H.6,LN15
JB 22H.7,LN16
LN9: MOV DPTR,#LINE9
JMP LINE_END
LN10: MOV DPTR,#LINE10
JMP LINE_END
LN11: MOV DPTR,#LINE11
JMP LINE_END
LN12: MOV DPTR,#LINE12
JMP LINE_END
LN13: MOV DPTR,#LINE13
JMP LINE_END
LN14: MOV DPTR,#LINE14
JMP LINE_END
LN15: MOV DPTR,#LINE15
JMP LINE_END
LN16: MOV DPTR,#LINE16
JMP LINE_END
LINE_END: RET
;**************************************************************************************************
LINE: DB ' welcome to baby.s music box '
LINE0: DB ' made by g_salamander '
LINE1: DB 'The 1 song '
LINE2: DB 'The 2 song '
LINE3: DB 'The 3 song '
LINE4: DB 'The 4 song '
LINE5: DB 'The 5 song '
LINE6: DB 'The 6 song '
LINE7: DB 'The 7 song '
LINE8: DB 'The 8 song '
LINE9: DB 'Happy birthday '
LINE10: DB 'Canghai xiao '
LINE11: DB
LINE12: DB
LINE13: DB
LINE14: DB
LINE15: DB
LINE16: DB
;**************************************************************************************************
TONE1:
DB 1,1,2,1,4,3,1,1,2,1,5,4,1,1,8,6,4,3,2,7,7,6,4,5,4,0
TONE2:
DB6,6,5,3,2,1,3,2,1,16,15,15,15,16,15,16,1,2,3,5,6,5,3,2,1,2,6,6,5,3,2,1,3,2,1,16,15,15,15,16,15,16,1,2,3,5,6,5,3,2,1,2,6,5,3,2,1,1,0
TONE3:
DB
TONE4:
DB
TONE5:
DB
TONE6:
DB
TONE7:
DB
TONE8:
DB
;****************************************************************************************************************************************************
BABY1:
DB252,68,252,173,253,10,253,52,253,131,253,200,254,6,254,34,254,87,254,133,254,155,254,194,254,228,255,3
BABY2:
DB250,104,251,5,251,144,252,12,252,68,252,173,253,10,253,52,253,131,253,200,254,6,254,34,254,87,254,133,248,140,249,92
BABY3:
DB
BABY4:
DB
BABY5:
DB
BABY6:
DB
BABY7:
DB
BABY8:
DB
;****************************************************************************************************************************************************
BEAT1:
DB 4,4,8,8,8,16,4,4,8,8,8,16,4,4,8,8,8,8,8,4,4,8,8,8,16
BEAT2:
DB4,2,2,6,2,16,6,2,4,2,2,16,6,2,6,2,6,2,4,4,6,2,2,2,4,16,4,2,2,6,2,16,6,2,4,2,2,16,6,2,6,2,6,2,4,4,6,2,2,2,4,16,6,2,2,2,4,16
BEAT3:
DB
BEAT4:
DB
BEAT5:
DB
BEAT6:
DB
BEAT7:
DB
BEAT8:
DB
;****************************************************************************************************************************************************
END

    推荐阅读