;;...............................
键盘扫描子程序---16键,D口低四位输出,高四位输入
;...............................
JIANPAN MOVLW 00H
MOVWF PORTD
MOVLW 0F0H
ANDWF PORTD,W
SUBLW 0F0H
BTFSC STATUS,Z;判断D口高四位是否全为“1”
RETURN ;是,无键输入,返回
CALL DELAY10MS;否,有键输入,延时20ms
CALL DELAY10MS
CALL DELAY10MS
MOVLW 00H ;再判断一次
MOVWF PORTD
MOVLW 0F0H
ANDWF PORTD,W
SUBLW 0F0H
BTFSC STATUS,Z;判断D口高四位是否全为“1”
RETURN ;是虚假输入,返回
MOVLW 0FEH ;确定有键输入,逐行判断,首先RD0=0
MOVWF PORTD
BTFSS PORTD,4 ;判键“0”
GOTO JIAN0
BTFSS PORTD,5 ;判键“1”
GOTO JIAN1
BTFSS PORTD,6 ;判键“2”
GOTO JIAN2
BTFSS PORTD,7 ;判键“3”
GOTO JIAN3
MOVLW 0FDH ;确定有键输入,逐行判断,首先RD1=0
MOVWF PORTD
BTFSS PORTD,4 ;判键“4”
GOTO JIAN4
BTFSS PORTD,5 ;判键“5”
GOTO JIAN5
BTFSS PORTD,6 ;判键“6”
GOTO JIAN6
BTFSS PORTD,7 ;判键“7”
GOTO JIAN7
MOVLW 0FBH ;确定有键输入,逐行判断,首先RD0=2
MOVWF PORTD
BTFSS PORTD,4 ;判键“8”
GOTO JIAN8
BTFSS PORTD,5 ;判键“9”
GOTO JIAN9
BTFSS PORTD,6 ;判键“A”
GOTO JIANA
BTFSS PORTD,7 ;判键“B”
GOTO JIANB
MOVLW 0F7H ;确定有键输入,逐行判断,首先RD0=3
MOVWF PORTD
BTFSS PORTD,4 ;判键“C”
GOTO JIANC
BTFSS PORTD,5 ;判键“D”
GOTO JIAND
BTFSS PORTD,6 ;判键“E”
GOTO JIANE
BTFSS PORTD,7 ;判键“F”
GOTO JIANF
RETURN
;.................
;10 ms延时子程序
;.................
DELAY10MS MOVLW 0FH
MOVFW 20H
LP2 MOVLW 0FFH
MOVWF 21H
LP1 DECFSZ 21H,F
GOTO LP1
DECFSZ 22H,F
GOTO LP2
RETURN
;........................
;键‘0’子程序,可自定义
;........................
JIAN0 CALL SFANG;调用键盘释放子程序
.
.
.
RETURN
;...................
;键盘释放等待子程序
;...................
SFANG MOVLW 00H
MOVWF PORTD
MOVLW 0F0H
ANDWF PORTD,W
SUBLW 0F0H
BTFSS STATUS,Z
GOTO SFANG;键未释放
CALL DELAY10MS; 已释放,延时
CALL DELAY10MS
CALL DELAY10MS
MOVLW 0F0H;再判断一次
ANDWF PORTD,W
SUBLW 0F0H
BTFSS STATUS,Z
GOTO SFANG;刚才为虚假释放,等待进一步判断
RETURN
以上为所有程序代码
另外一个汇编语言源代码:
我的键盘扫描程序:
key equ p2
disp equ p0
;7------+--+--+--+
;6------+--+--+--+
;5------+--+--+--+
;4------+--+--+--+
;3______| | | |
;2_________| | |
;1____________| |
;0_______________|
org 0000h
ajmp start
org 0100h
start: mov disp,#0
mov dptr,#tab
loopscan:acall keyin
acall keyoff
mov a,r0
movc a,@a+dptr
mov disp,a
ajmp loopscan
keyin: mov key,#0fh
mov a,key
cjne a,#0fh,select
sjmp keyin
select: mov a,key
cjne a,#0eh,next1
ajmp row1
next1: cjne a,#0dh,next2
ajmp row2
next2: cjne a,#0bh,next3
ajmp row3
next3: ajmp row4
row1: mov key,#0feh
mov a,key
cjne a,#0feh,row1scan
ajmp row2
row1scan:cjne a,#0eeh,row11
mov r0,#12
acall delay20ms
ajmp scanend
row11: cjne a,#0deh,row12
mov r0,#13
acall delay20ms
ajmp scanend
row12: cjne a,#0beh,row13
mov r0,#14
acall delay20ms
ajmp scanend
row13: mov r0,#15
acall delay20ms
ajmp scanend
row2: mov key,#0fdh
mov a,key
cjne a,#0fdh,row2scan
ajmp row3
row2scan:cjne a,#0edh,row21
acall delay20ms
mov r0,#8
ajmp scanend
row21: cjne a,#0ddh,row22
mov r0,#9
acall delay20ms
ajmp scanend
row22: cjne a,#0bdh,row23
mov r0,#10
acall delay20ms
ajmp scanend
row23: mov r0,#11
acall delay20ms
ajmp scanend
row3: mov key,#0fbh
mov a,key
cjne a,#0fbh,row3scan
ajmp row4
row3scan:cjne a,#0ebh,row31
mov r0,#4
acall delay20ms
ajmp scanend
row31: cjne a,#0dbh,row32
mov r0,#5
acall delay20ms
ajmp scanend
row32: cjne a,#0bbh,row33
mov r0,#6
acall delay20ms
ajmp scanend
row33: mov r0,#7
acall delay20ms
ajmp scanend
row4: mov key,#0f7h
mov a,key
cjne a,#0e7h,row41
mov r0,#0
acall delay20ms
ajmp scanend
row41: cjne a,#0d7h,row42
mov r0,#1
acall delay20ms
ajmp scanend
row42: cjne a,#0b7h,row43
mov r0,#2
acall delay20ms
ajmp scanend
row43: mov r0,#3
acall delay20ms
ajmp scanend
scanend:ret
;判断是否松开
keyoff: mov key,#0fh
mov a,key
cjne a,#0fh,keyret
sjmp keyoff
keyret: acall delay20ms
ret
;!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
;!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
;12Mhz Crystal!!!!!!!!!!!!!!!!!!!!!!!12周期单片机
delay20ms:mov r7,#250
delay20msd0:mov r6,#60
delay20msd1:djnz r6,$
djnz r7,delay20msd0
ret
tab:db 11100111b,10000100b,11010011b,11010110b,10110100b,
01110110b,01110111b,11000100b,11110111b,11110110b
db 11110101b,00110111b,01100011b,10010111b,01110011b,
01110001b,01100111b,10110101b,00100001b,10000111b
db 00100011b,00010101b,00010111b,11110001b,11110100b,10110110b
end
评论
查看更多