Skip to content

Instantly share code, notes, and snippets.

@clive819
Last active December 3, 2016 16:39
Show Gist options
  • Save clive819/1e9059748fa0b866596139d4dc055eab to your computer and use it in GitHub Desktop.
Save clive819/1e9059748fa0b866596139d4dc055eab to your computer and use it in GitHub Desktop.
A: odd B: even C: even
port4 EQU 0E8h
shift7LED EQU 0FEh
dispBuf EQU 40h
keyBuf1 EQU 44h
keyBuf2 EQU 45h
keyCode EQU 46h
keyCheck EQU 47h
debCounter EQU 48h
debTimes EQU 60
CLEAR BIT 05h
preValue EQU 50h
Negative BIT 02h
ORG 0000h
MOV keyCode,#0FFh
MOV keyCheck,#0FFh
MOV debCounter,#0
MOV A, #0FFh
MOV P2, A
MOV P3, A
MOV keyBuf1, #0FFh
MOV keyBuf2, #0FFh
MOV dispBuf, #0
MOV dispBuf+1, #0
MOV dispBuf+2, #0
MOV dispBuf+3, #0
MOV preValue, #0
MOV preValue+1, #0
MOV preValue+2, #0
MOV preValue+3, #0
SETB CLEAR
CLR Negative
SJMP SHOW3
RESET:
MOV R0, #dispBuf
MOV A, #shift7LED
SJMP LOOP
SHOW3:
MOV R0, #dispBuf+3
MOV A, #0F7h
LOOP:
MOV port4, A
ACALL CHECKNEG
ACALL COVLED
ACALL GETKEYCODE
ACALL DELAYKEYDEBOUNCE
JB CLEAR, SHOW3
INC R0
RL A
JB ACC.4, LOOP
SJMP RESET
CHECKNEG:
PUSH 0E0h
JNB Negative, EXITCHECKNEG
MOV P1, #07Fh
SJMP EXITCHECKNEG2
EXITCHECKNEG:
MOV P1, #0FFh
EXITCHECKNEG2:
POP 0E0h
RET
GETKEYCODE:
PUSH 0E0h
MOV A, P2
MOV keyBuf1, A
MOV A, p3
MOV keyBuf2, A
CJNE A, #0FFh, P3HASINPUT
MOV A, keyBuf1
MOV R2, #0
CHECKP2:
JNB 0E0h, GOTKEYCODE
INC R2
RR A
CJNE R2, #8, CHECKP2
SJMP EXITGETKEYCODE
P3HASINPUT:
MOV A, keyBuf2
MOV R2, #8
P3BIT:
JNB 0E0h, GOTKEYCODE
INC R2
RR A
CJNE R2, #16, P3BIT
SJMP EXITGETKEYCODE
GOTKEYCODE:
MOV keyCode, R2
EXITGETKEYCODE:
POP 0E0h
RET
DELAY:
MOV R4, #1
DELAY0:MOV R5, #2
DELAY1:MOV R6, #100
DELAY2:MOV R7, #100
DELAY3:DJNZ R7, DELAY3
DJNZ R6, DELAY2
DJNZ R5, DELAY1
DJNZ R4, DELAY0
RET
COVLED:
PUSH 0E0h
MOV A, @R0
ANL A, #0Fh
MOV DPTR, #LED_TABLE
MOVC A, @A+DPTR
MOV P0, A
POP 0E0h
RET
DELAYKEYDEBOUNCE:
PUSH 0E0h
ACALL DELAY
INC debCounter
MOV R5,debCounter
CJNE R5,#debTimes,DELAYEXIT
MOV debCounter,#0
MOV A,keyCode
CJNE A,#0FFh,HASINPUT
SJMP DELAYEXIT
EXIT:
MOV keyCheck,keyCode
DELAYEXIT:
POP 0E0h
RET
HASINPUT:
MOV A,keyCode
ANL A, #0Fh
MOV DPTR, #TRANSFORM
MOVC A, @A+DPTR
MOV keyCode, A
CJNE A,keyCheck,EXIT
CJNE A,#0Fh, SUBTRACTION
SETB CLEAR
CLR Negative
MOV A, #0
ACALL SETDIGITS
SJMP RESET2
SUBTRACTION:
CJNE A,#0Dh, EQUAL
SETB CLEAR
MOV preValue, dispBuf
MOV preValue+1, dispBuf+1
MOV preValue+2, dispBuf+2
MOV preValue+3, dispBuf+3
MOV dispBuf+3, A
MOV dispBuf+2, #0
MOV dispBuf+1, #0
MOV dispBuf, #0
SJMP RESET2
ISNUMBER:
JNB CLEAR, CONTINUE
CLR CLEAR
MOV dispBuf+3, #0
CONTINUE:
ADD A,#246
JC RESET2
ACALL DISPLAYKEYPATTERN
RESET2:
MOV keyCode,#0FFh
MOV keyCheck,#0FFh
SJMP DELAYEXIT
EQUAL:
CJNE A, #0Eh, ISNUMBER
CLR C
MOV A, preValue+3
SUBB A, dispBuf+3
ACALL DAAA
MOV preValue+3, A
MOV dispBuf+3, A
MOV A, preValue+2
SUBB A, dispBuf+2
ACALL DAAA
MOV preValue+2, A
MOV dispBuf+2, A
MOV A, preValue+1
SUBB A, dispBuf+1
ACALL DAAA
MOV preValue+1, A
MOV dispBuf+1, A
MOV A, preValue
SUBB A, dispBuf
ACALL DAAA
MOV preValue, A
MOV dispBuf, A
JNC EXITEQUAL
SETB Negative
EXITEQUAL:
JB Negative, CONVERT
SJMP RESET2
CONVERT:
CLR C
MOV A, #0
SUBB A, dispBuf+3
ACALL DAAA
MOV dispBuf+3, A
MOV preValue+3, A
MOV A, #0
SUBB A, dispBuf+2
ACALL DAAA
MOV dispBuf+2, A
MOV preValue+2, A
MOV A, #0
SUBB A, dispBuf+1
ACALL DAAA
MOV dispBuf+1, A
MOV preValue+1, A
MOV A, #0
SUBB A, dispBuf
ACALL DAAA
MOV dispBuf, A
MOV preValue, A
SJMP RESET2
SETDIGITS:
PUSH 0E0h
MOV dispBuf, A
MOV dispBuf+1, A
MOV dispBuf+2, A
MOV dispBuf+3, A
POP 0E0h
RET
DISPLAYKEYPATTERN:
PUSH 0E0h
MOV dispBuf,dispBuf+1
MOV dispBuf+1,dispBuf+2
MOV dispBuf+2,dispBuf+3
MOV dispBuf+3,keyCode
POP 0E0h
RET
DAAA:
ANL A, #0Fh
JNC EXITDAAA
SUBB A, #5
JNC LEAVEDAAA
ADD A, #6
LEAVEDAAA:
SETB C
EXITDAAA:
RET
TRANSFORM:
DB 0Eh
DB 0Ch
DB 0Ch
DB 0Dh
DB 0Ch
DB 03h
DB 06h
DB 09h
DB 0Fh
DB 02h
DB 05h
DB 08h
DB 00h
DB 01h
DB 04h
DB 07h
LED_TABLE:
DB 0C0h
DB 0F9h
DB 0A4h
DB 0B0h
DB 099h
DB 092h
DB 082h
DB 0D8h
DB 080h
DB 090h
DB 088h
DB 083h
DB 0FFh
DB 0BFh
DB 0FFh
DB 0C0h
END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment