/*
程序效果:八个数码管同时显示,按键最后按下的八个数值
*/
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 //打开最右边的数码管
MOV P2,#00 //给P2口送0值
LOOP: LCALL KEY //不断扫描键盘
LCALL DISPLAY //不断显示值
LJMP LOOP
//键盘扫描子程序
KEY: MOV R0,#0FEH //赋初值,用于循环,不断扫描键值
MOV R2,#04 //总循环4次
LRP: MOV A,R0 //暂存A的值
MOV P0,A
RL A //循环左移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 //按键没有松手,继续显示
LJMP LFP //等待按键松手,否则继续循环
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 //加本条语句的作用是:扩大CJNE的跳转范围
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 //以下语句的作用是:在有按键按下时
MOV 36H,35H //所有数据左移一位,空出30H单元
MOV 35H,34H
MOV 34H,33H
MOV 33H,32H
MOV 32H,31H
MOV 31H,30H
MOV 30H,40H
LKP15: DJNZ R2,LRP1 //以下三条语句的作用是:扩大DJNZ的跳转范围
LJMP LRP2
LRP1: LJMP LRP
LRP2: NOP
RET
//显示子程序
DISPLAY: MOV R1,#0FFH
MOV P1,R1 //点亮最右边的数码管
MOV P2,30H //显示30H单元的内容
LCALL DELAY //延时,使数码管更加亮,但时间要适宜
DEC R1 //准备点亮下一个数码管
MOV P1,R1 //点亮下一个数码管
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
//延时时间约为:2us*250=15ms
DELAY: MOV R6,#250
DEL1: DJNZ R6,DEL1
RET
END
上一篇:简单的4*4矩阵键盘扫描程序
下一篇:单片机编程中C语言和汇编的差异
推荐阅读最新更新时间:2024-03-16 13:13