按鍵與數(shù)碼管匯編例程
/*
程序效果:八個(gè)數(shù)碼管同時(shí)顯示,按鍵最后按下的八個(gè)數(shù)值
*/
ORG 0000H
LJMP START
ORG 0030H
START: MOV 30H,#00 //賦初值
MOV 31H,#00
MOV 32H,#00
MOV 33H,#00
MOV 34H,#00
MOV 35H,#00
MOV 36H,#00
MOV 37H,#00
MOV P1,#0FFH //打開(kāi)最右邊的數(shù)碼管
MOV P2,#00 //給P2口送0值
LOOP: LCALL KEY //不斷掃描鍵盤(pán)
LCALL DISPLAY //不斷顯示值
LJMP LOOP
//鍵盤(pán)掃描子程序
KEY: MOV R0,#0FEH //賦初值,用于循環(huán),不斷掃描鍵值
MOV R2,#04 //總循環(huán)4次
LRP: MOV A,R0 //暫存A的值
MOV P0,A
RL A //循環(huán)左移1位
XCH A,R0
MOV A,P0 //讀?P0口的值
CJNE A,#7EH,LKP //判斷按鍵0是否按下
MOV 40H,#3FH //是,則將鍵值暫存在40h單元中
LFP: MOV A,P0 //讀?P0口的值
CJNE A,#7EH, LKOP1 //判斷按鍵是否松手
LCALL DISPLAY //按鍵沒(méi)有松手,繼續(xù)顯示
LJMP LFP //等待按鍵松手,否則繼續(xù)循環(huán)
LKP: CJNE A,#0BEH,LKP1 //以下功能同上
MOV 40H,#66H
LFP1: MOV A,P0
CJNE A,#0BEH,LKOP1
LCALL DISPLAY
LJMP LFP1
LKP1: CJNE A,#0DEH,LKP2
MOV 40H,#7FH
LFP2: MOV A,P0
CJNE A,#0DEH,LKOP1
LCALL DISPLAY
LJMP LFP2
LKP2: CJNE A,#0EEH,LKP3
MOV 40H,#39H
LFP3: MOV A,P0
CJNE A,#0EEH,LKOP1
LCALL DISPLAY
LJMP LFP3
LKP3: CJNE A,#7DH,LKP4
MOV 40H,#06H
LFP4: MOV A,P0
CJNE A,#7DH,LKOP1
LCALL DISPLAY
LJMP LFP4
LKP4: CJNE A,#0BDH,LKP5
MOV 40H,#06DH
LFP5: MOV A,P0
CJNE A,#0BDH,LKOP1
LCALL DISPLAY
LJMP LFP5
LKP5: CJNE A,#0DDH,LKP6
MOV 40H,#6FH
LFP6: MOV A,P0
CJNE A,#0DDH,LKOP1
LCALL DISPLAY
LJMP LFP6
LKOP1: LJMP LKOP //加本條語(yǔ)句的作用是:擴(kuò)大CJNE的跳轉(zhuǎn)范圍
LKP6: CJNE A,#0EDH,LKP7
MOV 40H,#5EH
LFP7: MOV A,P0
CJNE A,#0EDH,LKOP1
LCALL DISPLAY
LJMP LFP7
LKP7: CJNE A,#7BH,LKP8
MOV 40H,#5BH
LFP8: MOV A,P0
CJNE A,#7BH,LKOP1
LCALL DISPLAY
LJMP LFP8
LKP8: CJNE A,#0BBH,LKP9
MOV 40H,#7DH
LFP9: MOV A,P0
CJNE A,#0BBH,LKOP
LCALL DISPLAY
LJMP LFP9
LKP9: CJNE A,#0DBH,LKP10
MOV 40H,#77H
LFP10: MOV A,P0
CJNE A,#0DBH,LKOP
LCALL DISPLAY
LJMP LFP10
LKP10: CJNE A,#0EBH,LKP11
MOV 40H,#79H
LFP11: MOV A,P0
CJNE A,#0EBH,LKOP
LCALL DISPLAY
LJMP LFP11
LKP11: CJNE A,#77H,LKP12
MOV 40H,#4FH
LFP12: MOV A,P0
CJNE A,#77H,LKOP
LCALL DISPLAY
LJMP LFP12
LKP12: CJNE A,#0B7H,LKP13
MOV 40H,#07H
LFP13: MOV A,P0
CJNE A,#0B7H,LKOP
LCALL DISPLAY
LJMP LFP13
LKP13: CJNE A,#0D7H,LKP14
MOV 40H,#7CH
LFP14: MOV A,P0
CJNE A,#0D7H,LKOP
LCALL DISPLAY
LJMP LFP14
LKP14: CJNE A,#0E7H,LKP15
MOV 40H,#71H
LFP15: MOV A,P0
CJNE A,#0E7H,LKOP
LCALL DISPLAY
LJMP LFP15
LKOP: MOV 37H,36H //以下語(yǔ)句的作用是:在有按鍵按下時(shí)
MOV 36H,35H //所有數(shù)據(jù)左移一位,空出30H單元
MOV 35H,34H
MOV 34H,33H
MOV 33H,32H
MOV 32H,31H
MOV 31H,30H
MOV 30H,40H
LKP15: DJNZ R2,LRP1 //以下三條語(yǔ)句的作用是:擴(kuò)大DJNZ的跳轉(zhuǎn)范圍
LJMP LRP2
LRP1: LJMP LRP
LRP2: NOP
RET
//顯示子程序
DISPLAY: MOV R1,#0FFH
MOV P1,R1 //點(diǎn)亮最右邊的數(shù)碼管
MOV P2,30H //顯示30H單元的內(nèi)容
LCALL DELAY //延時(shí),使數(shù)碼管更加亮,但時(shí)間要適宜
DEC R1 //準(zhǔn)備點(diǎn)亮下一個(gè)數(shù)碼管
MOV P1,R1 //點(diǎn)亮下一個(gè)數(shù)碼管
MOV P2,31H
LCALL DELAY
DEC R1
MOV P1,R1
MOV P2,32H
LCALL DELAY
DEC R1
MOV P1,R1
MOV P2,33H
LCALL DELAY
DEC R1
MOV P1,R1
MOV P2,34H
LCALL DELAY
DEC R1
MOV P1,R1
MOV P2,35H
LCALL DELAY
DEC R1
MOV P1,R1
MOV P2,36H
LCALL DELAY
DEC R1
MOV P1,R1
MOV P2,37H
LCALL DELAY
RET
//延時(shí)時(shí)間約為:2us*250=15ms
DELAY: MOV R6,#250
DEL1: DJNZ R6,DEL1
RET
END
評(píng)論