基于單片機(jī)的讀矩陣鍵盤(pán),用相應(yīng)鍵值
/*****************************************************
007.ASM
接線(xiàn):P0口接數(shù)碼管
P2接矩陣鍵盤(pán)
用定時(shí)中斷,若一段時(shí)間無(wú)操作,數(shù)碼管進(jìn)入自到循環(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 ;設(shè)置定時(shí)器T0工作模式
MOV TH0,#15H ;寫(xiě)計(jì)時(shí)初值
MOV TL0,#9FH
MOV P2,#0FFH ;P2口賦初值
MOV R4,#00H ;賦定時(shí)計(jì)數(shù)初值
SETB 00H ;00H為鍵值改變標(biāo)志位
SETB TR0 ;啟動(dòng)定時(shí)器T0
MOV IE,#82H ;開(kāi)中斷
LCALL SHOW ;數(shù)碼管初始化
loop: ;主循環(huán)
LCALL KEY_READ ;讀鍵盤(pán)
JBC 00H,LOOP0 ;判斷是否有鍵被按下,有則轉(zhuǎn)去刷新輸出并清零定時(shí)計(jì)數(shù)
CJNE R4,#0C8H,LOOP ;沒(méi)有鍵被按下,則判斷定時(shí)時(shí)間是否達(dá)到,到,則進(jìn)入數(shù)碼管循環(huán)輸出
LCALL SHOW1 ;沒(méi)到,則回到主循環(huán),重新掃描鍵盤(pán)
SJMP LOOP
LOOP0:
LCALL SHOW
MOV R4,#00H
SJMP LOOP ;輸出刷新完畢,回到主循環(huán),重新掃描鍵盤(pán)
/*鍵盤(pá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
/*延時(shí)子程序*/
DELAY:
D0: MOV R2,#0FH
D1: MOV R1,#0BFH
D2: DJNZ R1,D2
DJNZ R2,D1
DJNZ R0,D0
RET
/*定時(shí)中斷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
評(píng)論