基于單片機的讀矩陣鍵盤,用數(shù)碼管顯示相應鍵值
/*****************************************************
007.ASM
接線:P0口接數(shù)碼管
P2接矩陣鍵盤
用定時中斷,若一段時間無操作,數(shù)碼管進入自到循環(huán)顯示
2010.09.05
*******************************************************/
ORG 0000H
LJMP MAIN
ORG 000BH
LJMP INT_T0
ORG 0020H
MAIN: ;主程序
MOV SP,#50H
MOV DPTR,#LED_DATA ;取表首地址
MOV TMOD,#01H ;設置定時器T0工作模式
MOV TH0,#15H ;寫計時初值
MOV TL0,#9FH
MOV P2,#0FFH ;P2口賦初值
MOV R4,#00H ;賦定時計數(shù)初值
SETB 00H ;00H為鍵值改變標志位
SETB TR0 ;啟動定時器T0
MOV IE,#82H ;開中斷
LCALL SHOW ;數(shù)碼管初始化
loop: ;主循環(huán)
LCALL KEY_READ ;讀鍵盤
JBC 00H,LOOP0 ;判斷是否有鍵被按下,有則轉(zhuǎn)去刷新輸出并清零定時計數(shù)
CJNE R4,#0C8H,LOOP ;沒有鍵被按下,則判斷定時時間是否達到,到,則進入數(shù)碼管循環(huán)輸出
LCALL SHOW1 ;沒到,則回到主循環(huán),重新掃描鍵盤
SJMP LOOP
LOOP0:
LCALL SHOW
MOV R4,#00H
SJMP LOOP ;輸出刷新完畢,回到主循環(huán),重新掃描鍵盤
/*鍵盤掃描程序*/
KEY_READ:
CLR P2.6 ;P2.6輸出低
JB P2.0,N0_0
MOV R0,#01H
LCALL DELAY
JB P2.0,N0_0
JNB P2.0,$
MOV R5,#04H
SETB 00H
SETB P2.6
LJMP NEAT
N0_0:
JB P2.1,N0_1
MOV R0,#01H
LCALL DELAY
JB P2.1,N0_1
MOV R5,#03H
JNB P2.1,$
SETB 00H
SETB P2.6
LJMP NEAT
N0_1:
JB P2.2,N0_2
MOV R0,#01H
LCALL DELAY
JB P2.2,N0_2
JNB P2.2,$
MOV R5,#02H
SETB 00H
SETB P2.6
LJMP NEAT
N0_2:
JB P2.3,N1_0
MOV R0,#01H
LCALL DELAY
JB P2.3,N1_0
JNB P2.3,$
MOV R5,#01H
SETB 00H
SETB P2.6
LJMP NEAT
N1_0:
SETB P2.6
CLR P2.5
JB P2.0,N1_1
MOV R0,#01H
LCALL DELAY
JB P2.0,N1_1
JNB P2.0,$
MOV R5,#08H
SETB 00H
SETB P2.5
LJMP NEAT
N1_1:
JB P2.1,N1_2
MOV R0,#01H
LCALL DELAY
JB P2.1,N1_2
JNB P2.1,$
MOV R5,#07H
SETB 00H
SETB P2.5
LJMP NEAT
N1_2:
JB P2.2,N1_3
MOV R0,#01H
LCALL DELAY
JB P2.2,N1_3
JNB P2.2,$
MOV R5,#06H
SETB 00H
SETB P2.5
LJMP NEAT
N1_3:
JB P2.3,N2_0
MOV R0,#01H
LCALL DELAY
JB P2.3,N2_0
JNB P2.3,$
MOV R5,#05H
SETB 00H
SETB P2.5
LJMP NEAT
N2_0:
SETB P2.5
CLR P2.4
JB P2.0,N2_1
MOV R0,#01H
LCALL DELAY
JB P2.0,N2_1
JNB P2.0,$
MOV R5,#0CH
SETB 00H
SETB P2.4
LJMP NEAT
N2_1:
JB P2.1,N2_2
MOV R0,#01H
LCALL DELAY
JB P2.1,N2_2
JNB P2.1,$
MOV R5,#0BH
SETB 00H
SETB P2.4
LJMP NEAT
N2_2:
JB P2.2,N2_3
MOV R0,#01H
LCALL DELAY
JB P2.2,N2_3
JNB P2.2,$
MOV R5,#0AH
SETB 00H
SETB P2.4
LJMP NEAT
N2_3:
JB P2.3,NEAT
MOV R0,#01H
LCALL DELAY
JB P2.3,NEAT
JNB P2.3,$
MOV R5,#09H
SETB 00H
SETB P2.4
LJMP NEAT
NEAT:
SETB P2.4
RET
/*顯示子程序*/
SHOW:
MOV A,R5
MOVC A,@A+DPTR
MOV P0,A
RET
/*循環(huán)顯示子程序*/
SHOW1: ;
MOV R3,#00H
LOOP1:
MOV A,R3
MOVC A,@A+DPTR
MOV P0,A
MOV R0,#35H
LCALL DELAY
INC R3
LCALL KEY_READ
JB 00H,RETUN1
CJNE R3,#10H,LOOP1
MOV R3,#00H
SJMP LOOP1
RETUN1:
RET
/*延時子程序*/
DELAY:
D0: MOV R2,#0FH
D1: MOV R1,#0BFH
D2: DJNZ R1,D2
DJNZ R2,D1
DJNZ R0,D0
RET
/*定時中斷T0處理子程序*/
INT_T0:
CLR EA
PUSH ACC
INC R4
MOV TH0,#15H
MOV TL0,#9FH
POP ACC
SETB EA
RETI
/*數(shù)碼管顯示代碼,0~F*/
LED_DATA:
DB 03H,9FH,25H,0DH,99H,49H,41H,1FH
DB 01H,09H,11H,0C1H,63H,85H,61H,71H
END
DIY機械鍵盤相關社區(qū):機械鍵盤DIY
評論