八音盒51单片机程序(汇编)

合集下载
相关主题
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

;//*****************************************************
;文件名: 八音盒.asm
;描 述: 八音盒,P1选择音乐,低电平有效,P3.0音乐输出口。
; 本程序使用的寄存器有R1至R7,40H,41H,42H单元。
; 标号TAB1处为T0的定时常数数据表。
;//*****************************************************

Speaker BIT P3.4
FLAG1 BIT 20H
FLAG2 BIT 21H

ORG 0000H
LJMP Start

ORG 0003H
LJMP EXINT0

ORG 000BH
LJMP INT_T0
ORG 0013H
LJMP EXINT1

ORG 001BH
DJNZ R4,GOON ;中断次数不够,继续。
CLR ET1 ;持续时间到,该音符停止。
CLR ET0
SJMP RE01
GOON: MOV TL1,40H
MOV TH1,41H
RE01: RETI

INT_T0: MOV C,FLAG1
JC TORET
DJNZ R0,TORET
MOV R0,#6
MOV C,FLAG2
JC TOADD
SJMP TOSUB
TOADD: MOV A,R3
CJNE A,#0FEH,ADD1
CLR FLAG2
SJMP TORET
ADD1: MOV A,R2
ADD A,#1
MOV R2,A
MOV A,R3
ADDC A,#0
MOV R3,A
SJMP TORET
TOSUB: MOV A,R3
CJNE A,#0FBH,SUB1
SETB FLAG2
SJMP TORET
SUB1: MOV A,R2
SUBB A,#1
MOV R2,A
MOV A,R3
SUBB A,#0
MOV R3,A
TORET: MOV TL0,R2
MOV TH0,R3
CPL Speaker ;音乐输出口.
RETI



ORG 0080H
EXINT0:
CLR ET0
CLR ET1
CLR A
PUSH ACC
PUSH ACC
RETI
EXINT1:
CLR ET0
CLR ET1
CLR A
PUSH ACC
PUSH ACC
RETI
Start:
mov TCON, #05h ; 外部中断0下降沿触发
SETB EX0 ; 打开外部中断允许位(EX0)及总中断允许位(EA)
SETB EX1
SETB EA

MOV A,P1
JNB ACC.0,Voice0
JNB ACC.1,Voice1
JNB ACC.2,Voice2
JNB ACC.3,Voice3
JNB ACC.4,Voice4
JNB ACC.5,Voice5
JNB ACC.6,Voice6
JNB ACC.7,Voice7
Sjmp Start

Voice0: MOV DPTR,#tab_0 ;乐曲数据表的表头
Sjmp Main
Voice1: MOV DPTR,#tab_1 ;乐曲数据表的表头
Sjmp Main
Voice2: MOV DPTR,#tab_2 ;乐曲数据表的表头
Sjmp Main
Voice3: MOV DPTR,#tab_3 ;乐曲数据表的表头
Sjmp Main
Voice4: LJMP Main2
;MOV DPTR,#tab_4 ;乐曲数据表的表头
;Sjmp Main
Voice5: MOV DPTR,#tab_5 ;乐曲数据表的表头
Sjmp Main
Voice6: MOV DPTR,#tab_6 ;乐曲数据表的表头
Sjmp Main
Voice7: MOV DPTR,#tab_7 ;乐曲数据表的表头

Main: MOV TMOD,#11H
MOV IP,#02H
MOV 41H,#4CH ;T1初值放在41H,40H单元
MOV 40H,#0B

0H ;值越大,演奏越快
SETB EA
SETB FLAG1
CLR A
MOV R1,A ;R1作为音符指针
L1: MOVC A,@A+DPTR
INC R1 ;指向音符的拍数字节
MOV R6,A ;音级字节暂存入R6
JZ L3
ACALL TONE
MOV TH0,R3
MOV TL0,R2
SETB TR0
SETB ET0
MOV A,R1
MOVC A,@A+DPTR
INC R1
CJNE R1,#00H,L2
MOV DPTR,#0400H ;表指针溢出,更改表头
L2: ACALL LONG ;
MOV A,R1
AJMP L1 ;
L3: MOV A,R1
MOVC A,@A+DPTR
MOV R7,A
INC R1
CJNE R1,#00H,L4
MOV DPTR,#0400H
L4: ACALL LONG ;
MOV A,R1
CJNE R7,#0FH,L1
LJMP Main ;重复播放


ORG 0200H
TONE: MOV A,R6 ;取出音级字节
RL A ;乘2,定时常数为双字节
MOV 42H,A ;暂存入42H单元
ADD A,#05H ;距离数表有7个字节,使查表正确
MOVC A,@A+PC ;取出TH0
MOV R3,A
MOV A,42H
INC A
MOVC A,@A+PC ;取出TL0
MOV R2,A ;
RET

TAB1: DB 0F8H,8EH,0F8H,0F9H,0F9H,5EH
DB 0F9H,0BEH,0FAH,18H,0FAH,6DH
DB 0FAH,0BEH,0FBH,09H,0FBH,51H
DB 0FBH,95H,0FBH,0D4H,0FCH,11H
DB 0FCH,49H,0FCH,7FH,0FCH,0B2H
DB 0FCH,0E1H,0FDH,0FH,0FDH,39H
DB 0FDH,61H,0FDH,87H,0FDH,0ABH
DB 0FDH,0CDH,0FDH,0EDH,0FEH,0BH
DB 0FEH,27H,0FEH,42H,0FEH,5BH
DB 0FEH,73H,0FEH,8AH,0FEH,9FH
DB 0FEH,0B3H,0FEH,0C6H,0FEH,0D8H
DB 0FEH,0E9H,0FEH,0F9H,0FFH,08H
DB 0FFH,16H,0FFH,24H,0FFH,30H
DB 0FFH,3CH,0FFH,47H,0FFH,52H
DB 0FFH,5CH,0FFH,66H,0FFH,6FH
DB 0FFH,77H,0FFH,7FH,0FFH,86H

ORG 0300H ;音长子程序
LONG: ADD A,#0FH ;使查表正确
MOVC A,@A+PC ;取出定时器T1应中断的次数
MOV R4,A ;T1应中断的次数存入R4中
MOV TH1,41H
MOV TL1,40H
SETB TR1
SETB ET1
CJNE R4,#00H,$
RET
;音长子程序结束
TAB2: DB 01,02,04,06 ;应中断的次数
DB 08,10,12,14
DB 16,20,24,28
DB 32,40,48,64

Main2: CLR FLAG1
MOV TMOD,#01H
MOV R3,#0FCH ; T1初值放在41H,40H单元
MOV R2,#000H ;值越大,演奏越快
MOV TL0,R2
MOV TH0,R3
SETB FLAG2
SETB ET0
SETB EA
SETB TR0
MOV R0,#6
sjmp $

TAB_0: ;《友谊天长地久》
DB 00H,04H,00H,04H,08H,04H,0DH,08H,0DH,04H
DB 0DH,08H,11H,04H,0FH,08H,0DH,04H
DB 0FH,08H,11H,02H,0FH,02H,0DH,08H,0DH,04H
DB 11H,08H,14H,04H,16H,0DH,16H,04H
DB 14H,08H,11H,04H,11H,08H,0DH,04H
DB 0FH,08H,0DH,04H,0FH,08H,11H,02H,0FH,02H
DB 0DH,08H,0AH,04H,0AH,08H,08H,04H
DB 0DH,0CH,00H,04H,16H,04H,14H,08H,11H,04H
DB 11H,08H,0DH,04H,0FH,08H,0DH,04H
DB 0FH,08H,16H,04H,14H,08H,11H,04H
DB 11H,08H,14H,04H,16H,0DH,19H,04H
DB 14H,08H,11H,04H,11H,08H,0DH,04H
DB 0FH,08H,0DH,04H,0FH,08H,11H,02H,0FH,02H
DB 0DH,08H,0AH,04H,0AH,08H,08H,04H
DB 0DH,0CH,00H,04H,00H,04H
DB 00H,0FH ;结束标记

TAB_1: ;《梁祝》
DB 00H,04H,14H,04H,11H,04H,0FH,04H
DB 0DH,0AH,0FH,04H
DB 0CH,04H,0AH,04H,08H,08H
DB 08H,0AH,18H,04H
DB 16H,04H,18H,04H,14H,06H,16H,02H
DB 18H,04H,16H,04H,14H,02H,16H,02H,18H,02H,16H,02H
DB 19H,06H,16H,02H,14H,02H,16H,02H,14H,02H,0FH,02

H
DB 11H,04H,12H,04H,11H,04H,0FH,02H
DB 0DH,0CH
DB 0DH,04H,14H,04H,18H,04H,0FH,04H
DB 0AH,04H,0DH,04H,08H,00H
DB 08H,04H,0AH,02H,0DH,02H,08H,08H
DB 08H,08H,05H,04H,05H,04H
DB 08H,06H,0AH,02H,0DH,06H,0FH,02H
DB 0AH,02H,0DH,02H,08H,04H,14H,06H,19H,02H
DB 16H,03H,14H,01H,11H,02H,14H,02H,0FH,08H
DB 0FH,08H,0FH,06H,11H,02H
DB 0CH,04H,0AH,04H,08H,06H,0AH,02H
DB 0DH,04H,0FH,04H,05H,04H,0DH,04H
DB 0AH,03H,08H,01H,0AH,02H,0DH,02H,08H,08H
DB 08H,08H,11H,06H,14H,02H
DB 0CH,04H,0FH,04H,0AH,02H,0DH,02H,08H,04H
DB 08H,08H,05H,03H,08H,01H,05H,04H
DB 08H,02H,0AH,02H,0CH,02H,0FH,02H,0AH,08H
DB 0AH,04H,08H,02H,0AH,02H,0DH,06H,0FH,02H
DB 14H,04H,11H,04H,0FH,04H,11H,02H,0FH,02H
DB 0DH,04H,0AH,02H,08H,02H,08H,02H,05H,08H
DB 0DH,08H,0AH,02H,0DH,02H,0AH,02H,08H,02H
DB 05H,02H,08H,02H,0AH,02H,0DH,02H,08H,08H
DB 08H,0CH
DB 00H,0FH ;结束标记

TAB_2: ;《兰花草》
DB 0AH,02H,11H,02H,11H,02H,11H,02H,11H,06H,0FH,02H
DB 0DH,03H,0FH,01H,0DH,02H,0CH,02H,0AH,08H
DB 16H,02H,16H,02H,16H,02H,16H,02H,16H,06H,14H,02H
DB 11H,02H,14H,02H,14H,02H,13H,02H,11H,08H
DB 11H,02H,16H,02H,16H,02H,14H,02H,11H,06H,0FH,02H
DB 0DH,02H,0FH,02H,0DH,02H,0CH,02H,0AH,04H,05H,04H
DB 06H,02H,0DH,02H,0DH,02H,0CH,02H,0AH,06H,11H,02H
DB 0FH,03H,0DH,01H,0CH,02H,08H,02H,0AH,08H
DB 00H,0CH
DB 0AH,02H,11H,02H,11H,02H,11H,02H,11H,06H,0FH,02H
DB 0DH,03H,0FH,01H,0DH,02H,0CH,02H,0AH,08H
DB 16H,02H,16H,02H,16H,02H,16H,02H,16H,06H,14H,02H
DB 11H,02H,14H,02H,14H,02H,13H,02H,11H,08H
DB 11H,02H,16H,02H,16H,02H,14H,02H,11H,06H,0FH,02H
DB 0DH,02H,0FH,02H,0DH,02H,0CH,02H,0AH,04H,05H,04H
DB 06H,02H,0DH,02H,0DH,02H,0CH,02H,0AH,06H,11H,02H
DB 0FH,03H,0DH,01H,0CH,02H,08H,02H,0AH,08H
DB 00H,0FH ;结束标记

TAB_3: ;《小草》
DB 0AH,02H,0AH,02H,0DH,02H,0CH,02H,0AH,06H,00H,02H
DB 0AH,02H,0AH,02H,11H,02H,0FH,02H,11H,06H,00H,02H
DB 11H,02H,11H,02H,14H,02H,11H,02H,0FH,02H,0FH,02H,0DH,02H,0CH,01H,0CH,01H
DB 0AH,04H,00H,02H,08H,02H,05H,06H,00H,02H
DB 0AH,02H,0AH,02H,0DH,02H,0CH,02H,0AH,06H,00H,02H
DB 0AH,02H,0AH,02H,11H,02H,0FH,02H,11H,06H,00H,02H
DB 11H,02H,11H,02H,14H,02H,11H,02H,0FH,02H,0FH,02H,0FH,02H,0DH,02H
DB 0CH,02H,0AH,02H,0AH,02H,08H,02H,0AH,06H,00H,02H

DB 18H,02H,16H,04H,11H,02H,18H,02H,16H,04H,11H,02H
DB 14H,02H,16H,02H,16H,02H,13H,02H,11H,08H
DB 18H,02H,16H,04H,11H,02H,18H,02H,16H,04H,11H,02H
DB 14H,02H,16H,02H,16H,02H,13H,02H,11H,08H
DB 0FH,02H,0FH,04H,0AH,02H,0FH,02H,0FH,04H,12H,02H
DB 11H,02H,0FH,02H,0FH,02H,0AH,02H,0FH,08H
DB 0FH,02H,0FH,04H,0AH,02H,0DH,02H,0DH,02H,00H,02H,0FH,02H
DB 0CH,02H,0AH,02H,0AH,02H,08H,02H,0AH,08H

; DB 18H,02H,16H,04H,11H,02H,18H,02H,16H,04H,11H,02H
; DB 14H,02H,16H,02H,16H,02H,13H,02H,11H,08H
; DB 18H,02H,16H,04H,11H,02H,18H,02H,16H,04H,11H,02H
; DB 14H,02H,16H,02H,16H,02H,13H,02H,11H,08H
; DB 0FH,

02H,0FH,04H,0AH,02H,0FH,02H,0FH,04H,12H,02H
; DB 11H,02H,0FH,02H,0FH,02H,0AH,02H,0FH,08H
; DB 0FH,02H,0FH,04H,0AH,02H,0DH,02H,0DH,02H,00H,02H,0FH,02H
; DB 0CH,02H,0AH,02H,0AH,02H,08H,02H,0AH,08H
DB 00H,0FH ;结束标记

TAB_4: ;《千里之外》
DB 1BH,04H,1BH,02H,1BH,02H,1BH,02H,1DH,06H,1BH,02H,19H,02H,19H,02H,14H,02H,14H,02H,16H,06H
DB 19H,04H,19H,02H,19H,02H,19H,02H,1BH,06H,1DH,0CH
DB 1BH,04H,1BH,02H,1BH,02H,1BH,02H,1DH,06H,1BH,02H,19H,02H,19H,02H,14H,02H,14H,02H,16H,06H
DB 19H,04H,19H,02H,19H,02H,19H,02H,1DH,06H,1DH,01H,1BH,02H,1BH,04H,1BH,06H,14H,02H
DB 1BH,04H,1BH,02H,1BH,02H,1BH,02H,1DH,06H,1BH,02H,19H,02H,19H,02H,14H,02H,14H,02H,16H,06H
DB 19H,04H,19H,02H,19H,02H,19H,02H,1BH,06H,1DH,0CH
DB 1BH,04H,1BH,02H,1BH,02H,1BH,02H,19H,02H,19H,04H,1BH,04H,1BH,02H,1BH,02H,1BH,02H,19H,02H,19H,04H
DB 16H,04H,19H,02H,19H,02H,19H,0CH,16H,04H,19H,02H,19H,02H,19H,0CH
DB 00H,0FH ;结束标记

TAB_5: ;《救护车》
DB 18H,06H,12H,06H,18H,06H,12H,06H
DB 18H,06H,12H,06H,18H,06H,12H,06H
DB 18H,06H,12H,06H,18H,06H,12H,06H
DB 18H,06H,12H,06H,18H,06H,12H,06H
DB 18H,06H,12H,06H,18H,06H,12H,06H
DB 00H,0FH ;结束标记

TAB_6: ;叮咚
DB 20H,01H,00H,02H,10H,01H ;叮咚
DB 00H,0FH ;结束标记

TAB_7:
DB 20H,01H,00H,02H,20H,01H,00H,02H ;嗲嗲
DB 20H,01H,00H,02H,20H,01H,00H,02H ;嗲嗲
DB 20H,01H,00H,02H,20H,01H,00H,02H ;嗲嗲
DB 20H,01H,00H,02H,20H,01H,00H,02H ;嗲嗲
DB 20H,01H,00H,02H,20H,01H,00H,02H ;嗲嗲
DB 20H,01H,00H,02H,20H,01H,00H,02H ;嗲嗲
DB 20H,01H,00H,02H,20H,01H,00H,02H ;嗲嗲
DB 20H,01H,00H,02H,20H,01H,00H,02H ;嗲嗲
DB 20H,01H,00H,02H,20H,01H,00H,02H ;嗲嗲
DB 20H,01H,00H,02H,20H,01H,00H,02H ;嗲嗲
DB 20H,01H,00H,02H,20H,01H,00H,02H ;嗲嗲
DB 00H,0FH ;结束标记

END

相关文档
最新文档