单片机汇编四个开关控制0-f
单片机汇编4*4个开关控制0-f
LED EQU P3;;;;;LED灯显示定义
KEY EQU P1;;;;;键盘接口定义
TIME EQU 30H
CNTA EQU 31H
CNTB EQU 32H
;--------------------程序入口定义
ORG 0000H
JMP START;;;;;;;;芯片复位后的程序执行处
ORG 000BH
JMP T0LED;;;;;;;;8x8 点阵扫描显示,T0 中断
ORG 0030H
;--------------------初始化处理
START:
MOV TIME, #00H
MOV CNTA, #00H
MOV CNTB, #00H
MOV TMOD, #01H
MOV TH0, #0F0H
MOV TL0, #60H
SETB TR0
SETB ET0
SETB EA
MOV LED,#00H;;;;LED灯初始化全灭
;--------------------扫描循环体
LOOP:
MOV KEY,#0FH;;;;;装入键盘首次的扫描值
MOV A,KEY;;;;;;;;读出键盘值放入累加器A
CJNE A,#0FH,GO1;;;读出的键值是否有变化,有变则跳到有键按下处理程序
JMP LOOP;;;;;;;;;无键按下跳回主循环
GO1:
CALL DL20MS;;;;;;;有键按下延时20毫秒去抖
MOV A,KEY;;;;;;;;将键值给累加器
CJNE A,#0FH,GO2;;;重新判断键盘现状
JMP LOOP;;;;;;;;;是抖动则跳回主循环
GO2:
MOV KEY,#0F0H;;;;确定有键按下之后装入二次的扫描值
MOV B,KEY;;;;;;;;将二次扫描值放入寄存器B中
ORL A,B;;;;;;;;;;A与B相或,得到一个字节的数据在A中
;-----------------------键值对比处理
NE1:
CJNE A,#0EEH,NE2;;;将A中的数据与键值表对比,不同则跳到下一个对比
MOV A,#0;;;;;;;;;将输出显示数据送到寄存器
JMP KEYEND;;;;;;;;跳出对比程序
NE2:
CJNE A,#0EDH,NE3;;;1
MOV A,#1
JMP KEYEND
NE3:
CJNE A,#0EBH,NE4;;;2
MOV A,#2
JMP KEYEND
NE4:
CJNE A,#0E7H,NE5;;;3
MOV A,#3
JMP KEYEND
NE5:
CJNE A,#0DEH,NE6;;;4
MOV A,#4
JMP KEYEND
NE6:
CJNE A,#0DDH,NE7;;;5
MOV A,#5
JMP KEYEND
NE7:
CJNE A,#0DBH,NE8;;;6
MOV A,#6
JMP KEYEND
NE8:
CJNE A,#0D7H,NE9;;;7
MOV A,#7
JMP KEYEND
NE9:
CJNE A,#0BEH,NE10;;;8
MOV A,#8
JMP KEYEND
NE10:
CJNE A,#0BDH,NE11;;;9
MOV A,#9
JMP KEYEND
NE11:
CJNE A,#0BBH,NE12;;;A
MOV A,#10
JMP KEYEND
NE12:
CJNE A,#0B7H,NE13;;;B
MOV A,#11
JMP KEYEND
NE13:
CJNE A,#07EH,NE14;;;C
MOV A,#12
JMP KEYEND
NE14:
CJNE A,#07DH,NE15;;;D
MOV A,#13
JMP KEYEND
NE15:
CJNE A,#07BH,NE16;;;E
MOV A,#14
JMP KEYEND
NE16:
CJNE A,#077H,KEYEND;;;F
MOV A,#15
KEYEND: MOV CNTB, A
MOV DPTR, #PDATA
MOVC A, @A+DPTR
MOV LED,A;;;;;;;;;;;;;;;;;;将输出显示数据送出显示
JMP LOOP;;;;;;;;;;;;;;;;;;;跳回主循环
PDATA: DB 80H,0F9H, 24H, 30H, 59H, 12H, 02H,0F8H ; 显示码数据表
DB 00H, 10H, 08H, 03H, 86H, 21H, 06H, 0EH
;-------------------20毫秒延时,主要用于去抖动。(100,100)
DL20MS:
MOV R6,#100;;;;;;;;;
DL20MS_1:
MOV R7,#100;;;;;;;;;
DJNZ R7,$;;;;;;;;;;;;
DJNZ R6,DL20MS_1;;;;;
RET
T0LED: ; 8x8 点阵扫描显示,T0 中断
MOV TH0, #0F0H ;
MOV TL0, #60H
MOV DPTR, #TAB ; X
MOV A, CNTA
MOVC A, @A+DPTR
MOV P0, A ; P3
MOV DPTR, #DIGIT ; Y
MOV A, CNTB
MOV B, #8
MUL AB
ADD A, CNTA
MOVC A, @A+DPTR
MOV P2, A ; P1
INC CNTA
MOV A, CNTA
CJNE A, #8, NEXT
MOV CNTA, #00H
NEXT:
RETI
TAB:
DB 0FEH,0FDH,0FBH,0F7H,0EFH,0DFH,0BFH,07FH
DIGIT:
DB 00H,00H,3EH,41H,41H,41H,3EH,00H ;0
DB 00H,00H,00H,00H,21H,7FH,01H,00H ;1
DB 00H,00H,21H,43H,45H,49H,31H,00H ;2
DB 00H,00H,42H,49H,49H,59H,66H,00H ;3
DB 00H,00H,0CH,14H,24H,7FH,04H,00H ;4
DB 00H,00H,72H,51H,51H,51H,4EH,00H ;5
DB 00H,00H,3EH,49H,49H,49H,26H,00H ;6
DB 00H,00H,40H,40H,40H,4FH,70H,00H ;7
DB 00H,00H,36H,49H,49H,49H,36H,00H ;8
DB 00H,00H,32H,49H,49H,49H,3EH,00H ;9
DB 00H,00H,07H,38H,48H,38H,07H,00H ;A
DB 00H,00H,7FH,49H,49H,49H,36H,00H ;B
DB 00H,00H,3EH,41H,41H,41H,22H,00H ;C
DB 00H,00H,7FH,41H,41H,41H,3EH,00H ;D
DB 00H,00H,7FH,49H,49H,49H,41H,00H ;E
DB 00H,00H,7FH,48H,48H,48H,40H,00H ;F
END