51单片机控制蜂鸣器播放5首歌曲汇编程序
2015-08-26 15:46:20 来源:eefocus
;****************定时器0中断服务程序****************
INT_T: PUSH ACC
CLR TR0
MOV TH0, PERIOD_H
MOV TL0, PERIOD_L
MOV A, CU_COUNTER
CJNE A, COUNTER_1, GO_ON_1
SETB FLAG_LAST
JMP READ
GO_ON_1: CJNE A, COUNTER, GO_ON ;音符时间未到跳转
CLR FLAG_LAST
MOV DPH, TAB_H ;读取音长表首地址
MOV DPL, TAB_L
MOV A, POSITION
MOVC A, @A+DPTR
MOV COUNTER, A
MOV COUNTER_1, A
DEC COUNTER_1
READ: MOV DPH, TAB_TONE_H ;读取音高表首地址
MOV DPL, TAB_TONE_L
MOV A, POSITION ;查音高值
MOVC A, @A+DPTR
JNB FLAG_LAST, NO_LAST
SUBB A, #07
NO_LAST: MOV TONE, A
MOV DPTR, #TAB_TONE ;查与音高相对印的延时值
MOV A, TONE ;查第一个延时值
SUBB A, #06H
RL A
MOVC A, @A+DPTR
MOV D_1, A
MOV A, TONE ;查第二个延时值
SUBB A, #06H
RL A
INC A
MOVC A, @A+DPTR
MOV D_2, A
JB FLAG_LAST, GO_ON_INC
INC POSITION
MOV CU_COUNTER, #01H
MOV A, TONE
JZ INT_T_OVER
JB FLAG_L, LT_0
SETB FLAG_L
CLR P3.4
SETB P3.3
JMP INT_T_OVER
LT_0: CLR FLAG_L
CLR P3.3
SETB P3.4
JMP INT_T_OVER
GO_ON_INC: MOV A, TONE
ADD A, #07
MOV TONE, A
GO_ON: INC CU_COUNTER
INT_T_OVER:
INC COUNTER_LOOP ;行走控制
MOV A, COUNTER_LOOP
CJNE A, #16, IF_GO
ORL P1, #0FH
JMP R
IF_GO: CJNE A, #25, IF_32
MOV A, TONE
; JZ R
MOV A, COUNTER
CLR C
SUBB A, #64
JC YES ;音长大于等于64时停止行走
MOV A, COUNTER
SUBB A, CU_COUNTER
CLR C
SUBB A, #33
JC YES
JMP R
YES: JB FLAG_GO, GO_1 ;不允许行走跳转
ORL P1, #0FH
JMP R
GO_1: JB STAT_0, GO_2 ;行走一步
SETB P1.0
SETB STAT_0
CLR P1.1
CLR STAT_1
JMP R
GO_2: JB STAT_1, GO_3
SETB P1.1
SETB STAT_1
CLR P1.2
CLR STAT_2
JMP R
GO_3: JB STAT_2, GO_4
SETB P1.2
SETB STAT_2
CLR P1.3
CLR STAT_3
JMP R
GO_4: SETB P1.3
SETB STAT_3
CLR P1.0
CLR STAT_0
JMP R
IF_32: MOV A, COUNTER_LOOP
CJNE A, #32, R
MOV COUNTER_LOOP, #00H
R: SETB TR0
POP ACC
RETI
;******************外部中断0服务程序*******************
INT_0: PUSH ACC
POP ACC
RETI
;******************长延时子程序(1S)*******************
LONG: MOV D_L1, P_L1
DL1: MOV D_L2, P_L2
DL2: MOV D_L3, #0FFH
DL3: DJNZ D_L3, DL3
DJNZ D_L2, DL2
DJNZ D_L1, DL1
MOV P_L1, #01H
RET
;*******************延时子程序**********************
DELAY: MOV P_1, D_1
D1: MOV P_2, D_2
D2: DJNZ P_2, D2
DJNZ P_1, D1
RET
;---------------音长表--------------------
;以64分音符为一个单位,即1为64分音符、2为32分...
TAB_L0: DB 32,16,16,32,16,16,32,16,16,24,8,64 ;《DINGO BELL 》
DB 16,16,32,16,16,32,16,16,16,16,64
DB 16,16,32,16,16,32,16,16,24,8,64
DB 16,16,32,16,16,32,16,16,16,16,32,32
TAB_L1: DB 32,32,32,32,32,32,32,32,32,32,32,32,32,48,16,64 ;欢乐颂
DB 32,32,32,32,32,32,32,32,32,32,32,32,48,16,64
DB 32,32,32,32,32,16,16,32,32,32,16,16,32,32,32,32,32,64
DB 32,32,32,32,32,32,32,32,32,32,32,32,16,16,64
TAB_L2: DB 32,32,16,16,16,16,32,32,32,32,16,16,16,16,32,32,32 ;小步舞曲
DB 32,16,16,16,16,32,16,16,16,16,32,16,16,16,16,32,64,32
DB 32,16,16,16,16,32,32,32,32,16,16,16,16,32,32,32
DB 32,16,16,16,16,32,16,16,16,16,32,16,16,16,16,64,32,32
TAB_L3: DB 32,16,16,32,16,16,32,16,16,16,16,16,16,32 ;小蜜蜂
DB 16,16,32,16,16,32,16,16,16,16,64
DB 16,16,16,16,16,16,32,16,16,16,16,16,16,32
DB 16,16,32,16,16,32,16,16,16,16,32
TAB_L4: DB 32,16,16,32,32,32,64,16,16,32,32,32,64 ;生日快乐
DB 16,16,32,32,32,32,64,16,16,32,32,32,64
TAB_L5: DB 32,32,16,16,64,32,32,64,32,16,16,32,16,16,96+32 ;送别
DB 32,16,16,48,16,32,32,64,32,16,16,48,16,128
DB 32,32,64,32,16,16,64,16,16,16,16,16,16,16,16,96+32
DB 32,16,16,48,16,32,32,64,32,16,16,48,16,96,32
TAB_L6: DB 32,32,32,64,32,32,64,32,32,32,32,32,16,16,64 ;樱花
DB 32,32,32,32,32,16,16,64,32,32,32,32,32,16,16,64
DB 32,32,32,32,32,16,16,64,32,32,64,32,32,64
DB 32,32,16,16,32,96
TAB_L7: DB 32,32,16,16,32,16,16,16,32,16,64,32,16,16,32,16,16,16,32,16,64 ;虹彩妹妹
DB 32,16,16,16,16,16,16,16,32,16,64,32,16,16,32,32,16,32,16,48
TAB_L8: DB 32,16,16,48,16, 16,16,16,32,16,16,16, 32,16,16,16,16,16,16, 128+32 ;童年
DB 16,16,32,16,16, 16,16,16,32,16,16,16, 32,16,16,16,16,16,16, 128
DB 16,16,16,32,16,16,16, 32,16,16,16,16,16,16, 32,16,16,16,16,16,16, 128
DB 16,32,16,32,32, 16,32,16,16,16,16,16, 16,32,16,16,16,16,16, 128
TAB_L9: DB 32,00H
TAB_L10: DB 32,00H
;---------------播放音符表(0表示休止,1FH为结束标志)----------------
;从6开始,6——低6,7——低7,8——1,9——2 ,10——3,11——4,12——5,13——6,14——7,15——高1
; 16——高2,17——高3,18——高4,19——高5,20——高6
TAB_TONE0: DB 0,10+7,10+7,10+7, 10+7,10+7,10+7, 10+7,12+7,8+7,9+7, 10+7 ;《DINGO BELL 》
DB 11+7,11+7,11+7, 11+7,10+7,10+7, 10+7,9+7, 9+7,10+7, 9+7
DB 10+7,10+7,10+7, 10+7,10+7,10+7, 10+7,12+7,8+7,9+7, 10+7
DB 11+7,11+7,11+7, 11+7,10+7,10+7, 12+7,12+7,11+7,9+7, 8+7
DB 1FH
TAB_TONE1: DB 0,17,17,18,19,19,18,17,16,15,15,16,17,17,16,16 ;欢乐颂
DB 17,17,18,19,19,18,17,16,15,15,16,17,16,15,15
DB 16,16,17,15,16,17,18,17,15,16,17,18,17,16,15,16,12,17
DB 17,18,19,19,18,17,16,15,15,16,17,16,12,15,15
DB 1FH
TAB_TONE2: DB 0,19,15,16,17,18,19,15,15,20,18,19,20,21,22,15,15 ;小步舞曲
DB 18,19,18,17,16,17,18,17,16,15,14,15,16,17,15,17,16,0
DB 19,15,16,17,18,19,15,15,20,18,19,20,21,22,15,15
DB 18,19,18,17,16,17,18,17,16,15,16,17,16,15,14,15
DB 1FH
TAB_TONE3: DB 0,19,17,17,18,16,16,15,16,17,18,19,19,19 ;小蜜蜂
DB 19,17,17,18,16,16,15,16,17,19,17
DB 16,16,16,16,16,17,18,17,17,17,17,17,18,19
DB 19,17,17,18,16,16,15,16,17,19,15
DB 1FH
TAB_TONE4: DB 0,12,12,13,12,15,14,12,12,13,12,16,15 ;生日快乐
DB 12,12,19,17,15,14,13,18,18,17,15,16,15
DB 1FH
TAB_TONE5: DB 0,19,17,19,22,20,22,19,19,15,16,17,16,15,16 ;送别
DB 19,17,19,22,21,20,22,19,19,16,17,18,14,15
DB 20,22,22,21,20,21,22,20,21,22,20,20,19,17,15,16
DB 19,17,19,22,21,20,22,19,19,16,17,18,14,15
DB 1FH
;从6开始,6——低6,7——低7,8——1,9——2 ,10——3,11——4,12——5,13——6,14——7,15——高1
; 16——高2,17——高3,18——高4,19——高5,20——高6
TAB_TONE6: DB 0,20,20,21,20,20,21,20,21,22,21,20,21,20,18 ;樱花
DB 17,15,17,18,17,17,15,14,20,21,22,21,20,21,20,18
DB 17,15,17,18,17,17,15,14,20,20,21,20,20,21
DB 17,18,21,20,18,17
DB 1FH
TAB_TONE7: DB 0,20,19,17,20,19,17,20,20,19,20,20,19,17,20,19,17,16,16,15,16 ;虹彩妹妹
DB 17,17,19,20,22,20,19,17,17,19,15,17,17,17,17,17,13,13,12,13
DB 1FH
TAB_TONE8: DB 0,10,12,12,10, 13,13,14,13,13,13,12, 15,15,15,15,13,15,13, 12
DB 10,12,12,12,10, 13,13,14,13,13,13,12, 15,15,15,15,13,13,15,16
DB 19,19,19,19,19,17,16, 15,15,15,13,15,13,15, 16,16,16,16,15,17,16, 16
DB 17,17,17,17,16,15,15,15,16,15,13,12, 12,12,12,13,12,16,17,15
DB 1FH
TAB_TONE9: DB 0,00H
DB 1FH
TAB_TONE10: DB 0,00H
DB 1FH
;-------(实际音高表,每个音高包括两个延时值)-----------------
;第一个音符为6,7。。。
TAB_TONE: DB 29, 41, 11, 99, 131,6, 233,2, 13, 62
DB 14, 54, 233,1, 89, 5, 79, 5, 18, 27 ;( 高1)
DB 15, 29, 18, 21, 17, 21, 12, 27, 103, 1
DB 13, 19, 10,24 , 11,19
;----------------------乐曲播放速度表-----------------------
TAB_SPEED: DB 00H , 0C3H, 84H, 0E0H, 84H, 0D0H, 0EFH, 0CDH, 84H, 0CDH
DB 00H, 0C7H, 84H, 0CDH, 84H, 0CFH, 84H, 0CDH, 84H, 6DH
;-----------------------乐曲初值地址表----------------------------
TAB_NUM_L: DW TAB_L0, TAB_L1, TAB_L2, TAB_L3, TAB_L4, TAB_L5
DW TAB_L6, TAB_L7, TAB_L8, TAB_L9, TAB_L10
TAB_NUM_T: DW TAB_TONE0, TAB_TONE1, TAB_TONE2, TAB_TONE3, TAB_TONE4
DW TAB_TONE5, TAB_TONE6, TAB_TONE7, TAB_TONE8, TAB_TONE9
DW TAB_TONE10
NOP
NOP
END