按下数字键,移位显示;
按下运算键,保存当前数据,显示全零;
按下数字键,移位显示;
按下等号键,显示结果。
------------------
做计算器,用数码管显示,档次偏低,不值得费劲。应该用 LCD。
用数码管显示,练习编写一个时钟、计数器、等等,就可以了。
我以前做的,你可以参考一下:
KEYVAL EQU 30H
KEYTM EQU 31H
KEYSCAN EQU 32H
DAT EQU 33H
SCANLED EQU 37H
S_DAT EQU 38H
D_DAT EQU 39H
R_DATL EQU 3AH
R_DATH EQU 3BH
CALFLAG EQU 3CH
FLAG BIT 00H
ORG 0000H
LJMP MAIN
ORG 000BH
LJMP T0ISR
ORG 0030H
MAIN:
MOV SP,#5FH
MOV TMOD,#01H
MOV TH0,#0D8H
MOV TL0,#0F0H
MOV KEYVAL,#0
MOV P2,#00H
MOV SCANLED,#0
MOV 33H,#0H
MOV 34H,#0H
MOV 35H,#0H
MOV 36H,#0H
MOV S_DAT,#0
MOV D_DAT,#0
CLR FLAG
SETB EA
SETB ET0
SETB TR0
LOOP:
LCALL KEYSEL
MOV A,KEYVAL
CJNE A,#0FFH,LOOP1
SJMP LOOP
LOOP1:
CJNE A,#11,LOOP2 ;“=”
MOV A,33H
MOV B,#10
MUL AB
ADD A,34H
MOV D_DAT,A
LCALL DEALDAT
LCALL HB2
MOV A,R5
ANL A,#0FH
MOV 34H,A
MOV A,R5
SWAP A
ANL A,#0FH
MOV 33H,A
MOV A,R4
ANL A,#0FH
MOV 36H,A
MOV A,R4
SWAP A
ANL A,#0FH
MOV 35H,A
SJMP LOOP
LOOP2:
CJNE A,#15,LOOP3 ;“+”
LCALL GETDAT
SJMP LOOP
LOOP3:
CJNE A,#14,LOOP4 ;“-”
MOV CALFLAG,#1
LCALL GETDAT
SJMP LOOP
LOOP4:
CJNE A,#13,LOOP5 ;“*”
MOV CALFLAG,#2
LCALL GETDAT
SJMP LOOP
LOOP5:
CJNE A,#12,LOOP6 ;"/"
MOV CALFLAG,#3
LCALL GETDAT
SJMP LOOP
LOOP6:
CJNE A,#10,LOOP7 ;数字键
LOOP7:
JC LOOP8
LJMP LOOP
LOOP8:
MOV 33H,34H
MOV 34H,KEYVAL
LJMP LOOP
;------------------
HB2:
MOV R6,R_DATH
MOV R7,R_DATL
CLR A ;BCD码初始化
MOV R3,A
MOV R4,A
MOV R5,A
MOV R2,#10H ;转换双字节十六进制整数
HB3:
MOV A,R7 ;从高端移出待转换数的一位到 CY 中
RLC A
MOV R7,A
MOV A,R6
RLC A
MOV R6,A
MOV A,R5 ;BCD码带进位自身相加,相当于乘2
ADDC A,R5
DA A ;十进制调整
MOV R5,A
MOV A,R4
ADDC A,R4
DA A
MOV R4,A
MOV A,R3
ADDC A,R3
MOV R3,A ;双字节十六进制数的万位数不超过6,不用调整
DJNZ R2,HB3 ;处理完16bit
RET
;------------------
GETDAT:
MOV A,33H
MOV B,#10
MUL AB
ADD A,34H
MOV S_DAT,A
MOV 33H,#0
MOV 34H,#0
MOV 35H,#0
MOV 36H,#0
RET
;------------------
DEALDAT:
MOV A,CALFLAG
JNZ DEAL01
DEAL00: ;+
MOV A,S_DAT
ADD A,D_DAT
MOV R_DATL,A
CLR A
ADDC A,#0
MOV R_DATH,A
RET
DEAL01: ;=
DEC A
JNZ DEAL02
CLR C
MOV A,S_DAT
SUBB A,D_DAT
MOV R_DATL,A
SUBB A,#0
MOV R_DATH,A
RET
DEAL02: ;*
DEC A
JNZ DEAL03
MOV A,S_DAT
MOV B,D_DAT
MUL AB
MOV R_DATL,A
MOV R_DATH,B
RET
DEAL03: ;/
MOV A,S_DAT
MOV B,D_DAT
DIV AB
MOV R_DATL,A
MOV R_DATH,#0
RET
;------------------
KEYSEL:
MOV KEYVAL,#0
MOV KEYSCAN,#0EFH
LCALL GETKEY
MOV A,KEYTM
JZ KEYS1
MOV KEYVAL,A
SJMP KEYRTN
KEYS1:
MOV KEYSCAN,#0DFH
LCALL GETKEY
MOV A,KEYTM
JZ KEYS2
CLR C
ADD A,#4
MOV KEYVAL,A
SJMP KEYRTN
KEYS2:
MOV KEYSCAN,#0BFH
LCALL GETKEY
MOV A,KEYTM
JZ KEYS3
CLR C
ADD A,#8
MOV KEYVAL,A
SJMP KEYRTN
KEYS3:
MOV KEYSCAN,#7FH
LCALL GETKEY
MOV A,KEYTM
JZ KEYRTN
CLR C
ADD A,#12
MOV KEYVAL,A
KEYRTN:
LCALL CHGKEY
RET
;--------------------
GETKEY:
MOV KEYTM,#0
MOV A,KEYSCAN
MOV P3,A
NOP
MOV A,P3
ANL A,#0FH
XRL A,#0FH
JZ NOKEY
MOV R2,#10
LCALL DELAY
MOV A,P3
ANL A,#0FH
XRL A,#0FH
JZ NOKEY
MOV A,P3
ANL A,#0FH
MOV R7,A
SF:
MOV A,P3
ANL A,#0FH
XRL A,#0FH
JNZ SF
MOV A,R7
CJNE A,#0EH,NK1
MOV KEYTM,#1
SJMP NOKEY
NK1:
CJNE A,#0DH,NK2
MOV KEYTM,#2
SJMP NOKEY
NK2:
CJNE A,#0BH,NK3
MOV KEYTM,#3
SJMP NOKEY
NK3:
CJNE A,#07H,NOKEY
MOV KEYTM,#4
NOKEY: RET
;--------------------
DELAY:
MOV R3,#50
DELAY1:
MOV R4,#100
DJNZ R4,$
DJNZ R3,DELAY1
DJNZ R2,DELAY
RET
;--------------------
T0ISR:
PUSH ACC
CLR TR0
MOV TH0,#0D8H
MOV TL0,#0F0H
SETB TR0
MOV DPTR,#LEDTAB
T000: ;数字显示
MOV R0,#DAT
MOV A,SCANLED
ADD A,R0
MOV R0,A
MOV A,SCANLED
JNZ T001
MOV P2,#01H
SJMP T0DIS
T001:
DEC A
JNZ T002
MOV P2,#02H
SJMP T0DIS
T002:
DEC A
JNZ T003
MOV P2,#04H
SJMP T0DIS
T003:
MOV P2,#08H
T0DIS:
MOV A,@R0
MOVC A,@A+DPTR
; CPL A
MOV P0,A
INC SCANLED
MOV A,SCANLED
ANL A,#03H
MOV SCANLED,A
POP ACC
RETI
;--------------------
CHGKEY:
MOV A,KEYVAL
JZ KV16
DEC A
JNZ KV01
MOV KEYVAL,#7
RET
KV01:
DEC A
JNZ KV02
MOV KEYVAL,#4
RET
KV02:
DEC A
JNZ KV03
MOV KEYVAL,#1
RET
KV03:
DEC A
JNZ KV04
MOV KEYVAL,#10
RET
KV04:
DEC A
JNZ KV05
MOV KEYVAL,#8
RET
KV05:
DEC A
JNZ KV06
MOV KEYVAL,#5
RET
KV06:
DEC A
JNZ KV07
MOV KEYVAL,#2
RET
KV07:
DEC A
JNZ KV08
MOV KEYVAL,#0
RET
KV08:
DEC A
JNZ KV09
MOV KEYVAL,#9
RET
KV09:
DEC A
JNZ KV10
MOV KEYVAL,#6
RET
KV10:
DEC A
JNZ KV11
MOV KEYVAL,#3
RET
KV11:
DEC A
JNZ KV12
MOV KEYVAL,#11 ;=
RET
KV12:
DEC A
JNZ KV13
MOV KEYVAL,#12 ;/
RET
KV13:
DEC A
JNZ KV14
MOV KEYVAL,#13 ;*
RET
KV14:
DEC A
JNZ KV15
MOV KEYVAL,#14 ;-
RET
KV15:
DEC A
JNZ KV16
MOV KEYVAL,#15 ;+
RET
KV16:
MOV KEYVAL,#0FFH
RET
;--------------------
LEDTAB:
DB 0C0H ;"0" 00H
DB 0F9H ;"1" 01H
DB 0A4H ;"2" 02H
DB 0B0H ;"3" 03H
DB 99H ;"4" 04H
DB 92H ;"5" 05H
DB 82H ;"6" 06H
DB 0F8H ;"7" 07H
DB 80H ;"8" 08H
DB 90H ;"9" 09H
DB 88H ;"A" 0AH
DB 83H ;"B" 0BH
DB 0C6H ;"C" 0CH
DB 0A1H ;"D" 0DH
DB 86H ;"E" 0EH
DB 8EH ;"F" 0FH
DB 0FFH ;" " 10H
;--------------------
END