单片机电子琴电路图及源程序代码 第2页
程序如下:
ORG 0000H
SJMP FORT
ORG 0003H
SJMP INN0
ORG 000BH
SJMP ITT0
ORG 00013H
SJMP INN1
ORG 0033H
FORT: MOV TMOD, #01H
MOV IP, #02H /设定T0中断级别最高/
MOV IE, #87H /开中断/
SETB IT0 / INT0为负边沿触发/
SETB IT1 /INT1为负边沿触发/
SJMP $ /等待中断/
INN0: MOV R0, #01H /R0表示是哪个键按下/
JNB P2.0, NEXT
INC R0
JNB P2.1, NEXT
INC R0
JNB P2.2, NEXT
INC R0
JNB P2.3, NEXT
INC R0
JNB P2.4, NEXT
INC R0
JNB P2.5, NEXT
INC R0
JNB P2.6, NEXT
SJMP NOT
NEXT: MOV P0, R0 /显示是哪个键按下的/
DEC R0
MOV DPTR, #TABLE2
MOV A, R0
RL A
MOVC A, @A+DPTR
MOV R6, A / R6存放T值高字节/
MOV A, R0
RL A
INC A
MOVC A, @A+DPTR
MOV R7, A / R7存放T值低字节/
MOV TH0, R6
MOV TL0, R7
SETB TR0 /启动T0开始计时/
JXU: MOV A, P2
SMIAO: CJNE A,#0FFH,JXU/不相等表示键是一直按下/
MOV P0, #00H /对数码管清零/
SJMP NOT /相等表示按键已经松开/
NOT: RETI /返回中断,等待键下产生中断/
ITT0: PUSH ACC
PUSH PSW
MOV TH0, R6
MOV TL0, R7
CPL P1.0
POP PSW
POP ACC
RETI
TABLE2: DW 64580, 64684, 64777, 64820, 64898, 64968, 65030
END
唱歌程序如下:
ORG 0000H
SJMP FORT
ORG 0003H
SJMP INN0
ORG 000BH
SJMP ITT0
ORG 00013H
SJMP INN1
ORG 0033H
FORT: MOV TMOD, #01H
MOV IP, #02H /设定T0中断级别最高/
MOV IE, #87H /开中断/
SETB IT0 / INT0为负边沿触发/
SETB IT1 /INT1为负边沿触发/
SJMP $ /等待中断/
INN1: MOV P2,#08H /数码管显示8/
MOV 40H, #00H
D0: MOV A, #40H
MOV DPTR, #TABLE0
MOVC A, @A+DPTR
MOV R1, A /R1存放音符码/
JZ END0 /是否取到00(结束码)/
MOV A, #40H
INC A
MOVC A, @A+DPTR
MOV R2, A /R2存放节拍码/
MOV A, R1
CJNE A, #0FFH,SIG /音符码是否为0FFH/
CLR TR0
SJMP DEL0
SIG: MOV DPTR, #TABLE1
MOV 41H, A
RL A
MOVC A,@A+DPTR
MOV R6, A /T值高字节存入R6中/
MOV A, 41H
RL A
INC A
MOVC A, @A+DPTR
MOV R7, A /T值低字节存入R7中/
MOV TH0, 42H
MOV TL0, 43H
SETB TR0
DEL0: CALL DELAY
www.751com.cn
CPL P1.0
POP PSW
POP ACC
RETI
DELAY: MOV R3,#02
D0: MOV R4,#125
D1: MOV R5,#248
DJNZ R5, $
DJNZ R4, D1
DJNZ R3, D0
DJNZ R2, DELAY /R2 中存放节拍数/
RET
TABLE0: DW 63628,63731,63835,63928,64021,64103
DW 64185,64260,64331,64400,64463,64524
DW 64580,64633,64684,64732,64777,64820
DW 64860,64898,64934,64968,64994,65030
DW 65058,65085,65110,65134,65157,65178
DW 65198,65217,65235,65252,65268,65283
TABLE1: DB 09H,02H,10H,02H,10H,02H,10H,02H,
10H,04H,FFH,02H,07H,02H,05H,02H
0FH,02H,05H,02H,04H,02H,04H,08H
03H,02H,11H,02H,09H,02H,13H,02H
08H,02H,12H,02H,07H,02H,16H,02H
00H
END
演奏不同的歌曲只需要根据歌曲建立TABLE1即可。
完整的电子琴程序如:
ORG 0000H
SJMP FORT
ORG 0003H
SJMP INN0
ORG 000BH
SJMP ITT0
ORG 00013H
SJMP INN1
ORG 0033H
FORT: MOV TMOD, #01H
MOV IP, #02H /设定T0中断级别最高/
MOV IE, #87H /开中断/
SETB IT0 / INT0为负边沿触发/
SETB IT1 /INT1为负边沿触发/
SJMP $ /等待中断/
ORG 1000H
INN0: MOV R0, #01H /R0表示是哪个键按下/