;代码段
CODE SEGMENT
ASSUME DS:DATA,CS:CODE
MAIN PROC FAR
;主程序
START:
CALL INIT ;调用初始化程序
NEW_SCAN:
CALL SCAN_KEY ;扫描所按下的键
CMP SONG,100 ;判断是否按下了自动播放的歌曲键
JE HAND_PLAY ;若不是,继续判断
CALL PLAY_SONG ;若是,演奏音乐
JMP NEW_SCAN ;演奏结束,重新扫描有无键按下
HAND_PLAY:
CMP KEYNUM,100 ;判断是否手动演奏
JZ NEW_SCAN ;若不是,重新扫描有无键按下
;若是,进行按键处理
PUSH BX
MOV BX,KEYNUM
CALL KEY_DOWN ;在屏幕上显示按下状态
POP BX
CALL LOAD_AUDIO ;判断高低音、C/D调,并装入对应频率段地址到BX中
PUSH SI
MOV SI,KEYNUM
MOV DI,[BX+SI]
CALL GEN_HAND ;发声
POP SI
PUSH BX
MOV BX,KEYNUM ;在屏幕上显示弹起状态
CALL KEY_UP
POP BX
JMP NEW_SCAN ;重新扫描有无键按下,直到按下ESC键
MAIN ENDP
GEN_HAND PROC NEAR
;手动演奏发声程序
;IN:DI
PUSH AX
PUSH BX
PUSH CX
PUSH DX
PUSH DI
MOV AL,0B6H ;定时器初始化
OUT 43H,AL
MOV DX,12H
MOV AX,348CH
DIV DI
OUT 42H,AL ;定时器送处值
IN AL,61H ;开扬声器
MOV AL,AH
OUT 42H,AL
IN AL,61H
MOV AH,AL
OR AL,3
OUT 61H,AL
WAIT1:
CALL WAITF ;调用延时程序
IN AL,60H
TEST AL,80H ;查询键是否放开
JZ WAIT1 ;未放开继续查询
MOV AL,AH ;放开则关扬声器
OUT 61H,AL
POP DI
POP DX
POP CX
POP BX
POP AX
RET
GEN_HAND ENDP
GEN_AUTO PROC NEAR
;自动播放发声程序
PUSH AX
PUSH BX
PUSH CX
PUSH DX
PUSH DI
MOV AL,0B6H
OUT 43H,AL
MOV DX,12H
MOV AX,348CH
DIV DI
OUT 42H,AL
IN AL,61H
MOV AL,AH
OUT 42H,AL
IN AL,61H
MOV AH,AL
OR AL,3
OUT 61H,AL
WAIT2:
CALL WAITF
DEC BX
JNZ WAIT2 ;判断是否播放完毕
MOV AL,AH ;完毕则关闭扬声器
OUT 61H,AL
POP DI
POP DX
POP CX
POP BX
POP AX
RET
GEN_AUTO ENDP
WAITF PROC NEAR
;延时程序
PUSH AX
MOV CX,DELAY_TIME
WAITF1:
IN AL,61H
AND AL,10H ;检查PB4口
CMP AL,AH ;判断AL变化否
JE WAITF1 ;等待AL变化
MOV AH,AL ;保存PB4的新状态(AL中)
LOOP WAITF1
POP AX
RET
WAITF ENDP
PIANO_DRAW PROC NEAR
;绘制琴键
LEA SI,ST_LX ;载入白色起始点地址
LEA DI,ED_LX ;载入白色结束点地址
MOV CX,21 ;白键数目
NEXT_DRAW:
DRAW [SI],ST_Y,[DI],ED_Y,0FH ;调用宏定义画白键
DRAW [SI],90,[DI],93,07H ;阴影
ADD SI,2 ;下一个键
ADD DI,2
LOOP NEXT_DRAW
LEA SI,BT_X ;载入黑色起始点地址
LEA DI,BD_X ;载入黑色结束点地址
MOV CX,15 ;黑键数目
NEXT_BLOCK:
DRAW [SI],BT_Y,[DI],BD_Y,00H
ADD SI,2
ADD DI,2
LOOP NEXT_BLOCK
RET
PIANO_DRAW ENDP
KEY_DOWN PROC NEAR
;键按下时,在屏幕上显示
PUSH SI
PUSH DI
CALL LOAD ;载入所按下键在屏幕上的位置
DRAW [SI+BX],90,[DI+BX],92,0FH
DRAW [SI+BX],92,[DI+BX],94,07H
POP DI
POP SI
RET
KEY_DOWN ENDP
KEY_UP PROC NEAR
;键弹起时,恢复正常状态
PUSH SI
PUSH DI
CALL LOAD ;载入所弹起键在屏幕上的位置
DRAW [SI+BX],90,[DI+BX],93,07H ;恢复阴影
DRAW [SI+BX],93,[DI+BX],94,05H ;恢复背景色
POP DI
POP SI
RET
KEY_UP ENDP
〈未完〉
本文地址:http://com.8s8s.com/it/it28918.htm