本学期刚开学,我们进行了微机原理课程设计,我们小组选择了4×4小键盘按键检测识别这个综合性题目。
具体课设过程记录如下:
在本次课程设计中,我们使用了16个按键,4个10k电阻,1个8255A芯片,两位8段共阴极数码管,导线若干。
附有proteus安装包链接:
https://pan.baidu.com/s/1t5cMO02vTdWVEJ6qCNNO_Q
IOY0 EQU 0600H ;片选IOY0对应的端口始地址
MY8255_A EQU 0600H+00H*2 ;8255的A口地址
MY8255_B EQU 0600H+01H*2 ;8255的B口地址
MY8255_C EQU 0600H+02H*2 ;8255的C口地址
MY8255_CON EQU 0600H+03H*2 ;8255的控制寄存器地址
SSTACK SEGMENT STACK
DW 16 DUP(?)
SSTACK ENDS
DATA SEGMENT
ZIXINGMA DB 3FH,06H,5BH,4FH,66H,6DH,7DH,07H,7FH,6FH,77H,7CH,39H,5EH,79H,71H ;数码管的字形码
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE,DS:DATA
START:
MOV SI, 00H
;存储数码管将要显示的0-15对应的字形码
MOV [SI], 3FH
INC SI
MOV [SI],06H
INC SI
MOV [SI],5BH
INC SI
MOV [SI],4FH
INC SI
MOV [SI],66H
INC SI
MOV [SI],6DH
INC SI
MOV [SI],7DH
INC SI
MOV [SI],07H
INC SI
MOV [SI],7FH
INC SI
MOV [SI],6FH
INC SI
MOV [SI],77H
INC SI
MOV [SI],7CH
INC SI
MOV [SI],39H
INC SI
MOV [SI],5EH
INC SI
MOV [SI],79H
INC SI
MOV [SI],71H
MOV SI,3000H
MOV AL,00H
MOV [SI],AL
MOV DI,3000H
;写控制字,PC3-PC0引脚为方式0下的输入状态,其余引脚为方式0下的输出状态
MOV DX,MY8255_CON
MOV AL,81H
OUT DX,AL
BEGIN: CALL DIS ;调用显示子程序
CALL CLEAR ;清屏
CALL CCSCAN ;扫描
JNZ INK1
JMP BEGIN
INK1: CALL DIS
;消抖操作:防止将按键的抖动当做按键按下来处理
CALL DALLY
CALL DALLY
CALL CLEAR
CALL CCSCAN
JNZ INK2 ;如果键按下,转到INK2
JMP BEGIN
INK2: MOV CH,0FEH
MOV CL,00H
;依次扫描每一行,检测是否某一行有键按下
COLUM: MOV AL,CH
MOV DX,MY8255_A
OUT DX,AL
MOV DX,MY8255_C
IN AL,DX
L1: TEST AL,01H ;判断是否是第一行
JNZ L2
MOV AL,00H ;写入第一行第一个按键对应的数码管要显示的字形
JMP KCODE
L2: TEST AL,02H ;判断是否是第二行
JNZ L3
MOV AL,04H ;写入第二行第一个按键对应的数码管要显示的字形
JMP KCODE
L3: TEST AL,04H ;判断是否是第三行
JNZ L4
MOV AL,08H ;写入第三行第一个按键对应的数码管要显示的字形
JMP KCODE
L4: TEST AL,08H ;判断是否是第四行
JNZ NEXT
MOV AL,0CH ;写入第四行第一个按键对应的数码管要显示的字形
KCODE: ADD AL,CL
CALL PUTBUF
PUSH AX
KON: CALL DIS
CALL CLEAR
CALL CCSCAN
JNZ KON
POP AX
NEXT: INC CL
MOV AL,CH
TEST AL,08H
JZ KERR
ROL AL,1
MOV CH,AL
JMP COLUM
KERR: JMP BEGIN
CCSCAN: MOV AL,00H ;键盘扫描子程序
MOV DX,MY8255_A
OUT DX,AL
MOV DX,MY8255_C
IN AL,DX
NOT AL
AND AL,0FH
RET
DIS: PUSH AX ;显示子程序
MOV SI,3000H
MOV DL,0DFH
MOV AL,DL
MOV DX,MY8255_A
OUT DX,AL
MOV AL,[SI]
MOV BX,OFFSET ZIXINGMA
AND AX,00FFH
CMP AX,10
JNC OVER
ADD BX,AX
MOV AL,[BX]
MOV DX,MY8255_B
OUT DX,AL
CALL DALLY
MOV DL,0EFH
MOV AL,DL
MOV DX,MY8255_A
OUT DX,AL
MOV AL,3FH
MOV DX,MY8255_B
OUT DX,AL
CALL DALLY
JMP LATE
OVER:
MOV SI,3000H
MOV DL,0DFH
MOV AL,DL
MOV DX,MY8255_A
OUT DX,AL
MOV AL,[SI]
MOV BX,OFFSET ZIXINGMA
AND AX,00FFH
SUB AX,0AH
ADD BX,AX
MOV AL,[BX]
MOV DX,MY8255_B
OUT DX,AL
CALL DALLY
MOV DL,0EFH
MOV AL,DL
MOV DX,MY8255_A
OUT DX,AL
MOV AL,06H
MOV DX,MY8255_B
OUT DX,AL
CALL DALLY
LATE: POP AX
RET
DALLY: PUSH CX ;延时子程序
MOV CX,0006H
T1: MOV AX,009FH
T2: DEC AX
JNZ T2
LOOP T1
POP CX
RET
CLEAR: MOV DX,MY8255_B ;清屏子程序
MOV AL,00H
OUT DX,AL
RET
PUTBUF: MOV SI, DI ;存键盘值到相应位的缓冲中
MOV [SI], AL
RET
CODE ENDS
END START