一、实验目的和任务

1、掌握利用定时/计数器产生音频信号的方法。

2、理解音乐门铃的程序结构,加深对定时中断应用的认识。

二、实验原理简介

1、蜂鸣器(由MCU1的P1.0口控制)在不同频率信号的驱动下,会产生不同的声调,通过程序将不同的声调组合起来,就能够产生音乐。

2、利用定时器可以产生不同频率的信号。

3、可以将曲调预先编辑好,给程序查表用。

三、实验内容和步骤

编写音乐门铃程序,在按键KX0~KX3控制下,播放4首不同的乐曲。

参考程序如下:

SONG1 EQU 3EH
SONG2 EQU 3DH
KX0 EQU P3.2
KX1 EQU P3.4
KX2 EQU P3.5
KX3 EQU P3.7
L101 EQU P0.0
L102 EQU P0.1
L103 EQU P0.2
L104 EQU P0.3
ORG 0000H
JMP MAIN
ORG 000BH
JMP INTT0
ORG 0100H
MAIN: MOV SP,#60H
MOV TMOD,#01H
MOV SONG1,#00H
MOV SONG2,#00H
LCALL DELAY2
LCALL DELAY2
CHKKX0: JB KX0,CHKKX1 ;KX0未按下,则检测KX1
LCALL DELAY2 ;软件延时
JB KX0,CHKKX1 ;KX0未按下,则检测KX1
CPL L101 ;KX0按下,则求反L101
MOV DPTR,#TABLE_1
JNB KX0,$ ;等待KX0松开
SJMP START
CHKKX1: JB KX1,CHKKX2
LCALL DELAY2
JB KX1,CHKKX2
CPL L102
MOV DPTR,#TABLE_2
JNB KX2,$
SJMP START
CHKKX2: JB KX2,CHKKX3
LCALL DELAY2
JB KX2,CHKKX3
CPL L103
MOV DPTR,#TABLE_3
JNB KX2,$
SJMP START
CHKKX3: JB KX3,MAIN
LCALL DELAY2
JB KX3,MAIN
CPL L104
MOV DPTR,#TABLE_4
JNB KX3,$
SJMP START
START: MOV SONG1,DPL
MOV SONG2,DPH
SETB EX1
SETB ET0
SETB EA
START0: MOV 30H,#00H
NEXT: MOV A,30H
MOV DPL,SONG1
MOV DPH,SONG2
MOVC A,@A+DPTR
MOV R2,A
JZ ENDD
ANL A,#0FH
MOV R5,A
MOV A,R2
SWAP A
ANL A,#0FH
JNZ SING
CLR TR0
JMP D1
SING: DEC A
MOV 22H,A
RL A
MOV DPTR,#TABLE1
MOVC A,@A+DPTR
MOV TH0,A
MOV 21H,A
MOV A,22H
RL A
INC A
MOVC A,@A+DPTR
MOV TL0,A
MOV 20H,A
SETB TR0
D1: CALL DELAY
INC 30H
JMP NEXT
ENDD: CLR TR0
SETB P1.0
JMP MAIN
INTT0: PUSH PSW
PUSH ACC
MOV TL0,20H
MOV TH0,21H
CPL P1.0
POP ACC
POP PSW
RETI
DELAY: MOV R7,#12
DELAY0:MOV R4,#187
DELAY1:MOV R3,#238
DJNZ R3,$
DJNZ R4,DELAY1
DJNZ R7,DELAY0
DJNZ R5,DELAY
RET
DELAY2: MOV R7,#04
DELAY3: MOV R4,#187
DELAY4: MOV R3,#248
DJNZ R3,$
DJNZ R4,DELAY4
DJNZ R7,DELAY3
RET
TABLE_1:DB 02H,11H,21H,32H,32H,22H,32H,21H,11H,0A1H,91H,92H,0D1H,91H
DB 0A2H,91H,81H,94H,11H,0A1H,91H,0A1H
DB 11H,93H,94H,03H,81H,11H,0A1H,91H,81H,92H,0A1H,91H,94H,08H
DB 03H,81H,32H,21H,11H,22H,31H,21H,21H,83H
DB 01H,11H,91H,0A1H,11H,61H,51H,11H,34H,01H,31H,21H,31H,48H
DB 23H,31H,32H,42H,38H,03H,11H,21H,52H,31H
DB 33H,31H,31H,41H,51H,61H,23H,21H,21H,41H,31H,21H,13H,0D1H
DB 21H,11H,0A1H,11H,91H,92H,91H,11H,0A1H,91H
DB 0A1H,31H,31H,22H,0A1H,11H,14H,01H,11H,21H,31H,63H,31H
DB 42H,51H,31H,22H,11H,21H,52H,31H,33H,31H,31H,41H
DB 51H,61H,23H,21H,21H,41H,31H,21H,13H,0D1H,0D1H,21H,11H
DB 0A1H,11H,91H,92H,91H,11H,0A1H,91H
DB 0A2H,41H,31H,22H,31H,21H,12H,12H,11H,11H,21H,31H
DB 63H,51H,32H,11H,21H,26H,11H,0A1H,18H
DB 00H
TABLE_2:DB 82H,01H,81H,94H,84H,0B4H,0A4H,04H
DB 82H,01H,81H,94H,84H,0C4H,0B4H,04H
DB 82H,01H,81H,0F4H,0D4H,0B4H,0A4H,94H
DB 0E2H,01H,0E1H,0D4H,0B4H,0C4H,0B4H,04H
DB 82H,01H,81H,94H,84H,0B4H,0A4H,04H
DB 82H,01H,81H,94H,84H,0C4H,0B4H,04H
DB 82H,01H,81H,0F4H,0D4H,0B4H,0A4H,94H
DB 0E2H,01H,0E1H,0D4H,0B4H,0C4H,0B4H,04H
DB 00H
TABLE_3:DB 66H,52H,36H,22H,12H,12H,22H,32H,98H,84H,82H,92H,14H,12H,22H
DB 32H,52H,22H,42H,38H,66H,52H,36H,22H,12H,22H,22H,32H,98H
DB 24H,22H,32H,54H,52H,32H,92H,12H,92H,82H,98H,66H,52H,36H,22H
DB 12H,12H,22H,32H,98H,84H,82H,92H,14H,12H,22H,32H,52H,22H,42H,38H
DB 66H,52H,36H,22H,12H,22H,22H,32H,98H,24H,22H,32H,54H,52H,32H
DB 22H,12H,92H,82H,98H,14H,24H,34H,44H,64H,0B4H,64H,32H,52H
DB 62H,0B2H,62H,52H,38H,24H,22H,32H,54H,52H,62H,52H,32H,32H,22H,38H
DB 64H,0B4H,64H,32H,52H,62H,0B2H,62H,52H,38H,22H,22H,22H,32H,52H,52H
DB 52H,62H,7FH,74H,04H,58H,6FH
DB 00H
TABLE_4:DB 02H,82H,16H,32H,54H,02H,52H,0A6H,32H,54H,02H,52H
DB 64H,74H,0B6H,64H,52H,5CH,32H,22H,16H,12H,14H,32H,22H
DB 16H,12H,14H,22H,32H,26H,12H,94H,22H,32H,2CH
DB 32H,52H,36H,22H,14H,54H,0ACH,92H,0A2H
DB 96H,0A2H,96H,82H,3CH,54H,36H,22H,14H,54H
DB 0ACH,92H,0A2H,16H,12H,14H,22H,32H,2CH,02H,82H
DB 16H,32H,56H,12H,0A6H,32H,56H,52H,66H,72H,0B6H,62H
DB 62H,52H,58H,32H,22H,16H,12H,14H,32H,22H
DB 16H,12H,14H,22H,32H,26H,92H,0A4H,12H,22H,1CH
DB 00H
TABLE1: DW 64260,64400,64524,64580,64684,64777,64820,64898
DW 64968,65030,65058,65110,65157,65178,65217
END