ENABLE   REG   P3.5
R_W      REG   P3.6
RS       REG   P3.7
               ORG   0H
               JMP   START
               ORG   50H
START:         MOV   SP,#70H
               CLR   P3.4
               CLR   ENABLE
               CALL  LCD_RESET
               MOV   A,#00111000B
               CALL  RS0
               MOV   A,#00001110B
               CALL  RS0
               MOV   A,#00000110B
               CALL  RS0
               MOV   A,#00000010B
               CALL  RS0
               MOV   A,#00000001B
               CALL  RS0
               CALL  DISP
               MOV   40H,#0FFH
               MOV   41H,#0FFH
               MOV   42H,#80H
               MOV   43H,#80H
               MOV   3FH,#0
               MOV   25H,#00000000B
               MOV   30H,#12H
               MOV   31H,#03H
ST5:           CALL  SCAN
               CJNE  A,#0AH,ST6
               JMP   SR0
ST6:           CJNE  A,#0BH,ST7
               JMP   SR1
ST7:           CJNE  A,#0CH,ST8
               JMP   SR1

ST8:           CJNE  A,#0DH,ST9
               JMP   SR1
ST9:           CJNE  A,#0EH,ST10
               JMP   SR1

ST10:          CJNE  A,#0FH,ST11
               JMP   SR1
ST11:          CALL  SETN
               JMP   ST5

SR0:           CALL  SHLF
               JMP   ST5
SR1:           JMP   ST5
SHLF:          MOV   A,25H
               ANL   A,#0FH
               JZ    SH1
               JNB   25H.3,SH2
               SETB  25H.2
               CLR   25H.3
               RET
SH2:           JNB   25H.2,SH3
               SETB  25H.1
               CLR   25H.2
               RET
SH3:           JNB   25H.1,SH4
               SETB  25H.0
               CLR   25H.1
               RET
SH4:           JNB   25H.0,SH1
               SETB  25H.3
               CLR   25H.0
               RET
SH1:           MOV   25H,#00001000B
               RET
SETN:
               JNB   25H.3,SET1
               SWAP  A
               ANL   31H,#0FH
               ORL   31H,A
               RET
SET1:          JNB   25H.2,SET2
               ANL   31H,#0F0H
               ORL   31H,A
               RET
SET2:          JNB   25H.1,SET3
               SWAP  A
               ANL   30H,#0FH
               ORL   30H,A
               RET
SET3:          JNB   25H.0,SET4
               ANL   30H,#0F0H
               ORL   30H,A
SET4:          RET
SCAN:
               CALL  SCAN1
               JNC   SCAN
SCA1           CALL  ST1

SCA4:          CALL  SCAN1
               JC    SCA1
               RET
ST1:

               CALL  READ
               CALL  CHPN
               CALL  ONV1
               MOV   A,#10000000B
               CALL  RS0
               MOV   A,#'P'
               CALL  RS1
               MOV   A,#'V'
               CALL  RS1
               MOV   A,#'='
               CALL  RS1
               MOV   A,35H
               CALL  RS1
               MOV   A,34H
               CALL  RS1
               MOV   A,33H
               CALL  RS1
               MOV   A,#'.'
               CALL  RS1
               MOV   A,32H
               CALL  RS1
               MOV   A,#'C'
               CALL  RS1
               MOV   A,#11000000B
               CALL  RS0
               MOV   A,#'S'
               CALL  RS1
               MOV   A,#'V'
               CALL  RS1
               MOV   A,#'='
               CALL  RS1
               MOV   A,39H
               CALL  RS1
               MOV   A,38H
               CALL  RS1
               MOV   A,37H
               CALL  RS1
               MOV   A,#'.'
               CALL  RS1
               MOV   A,36H
               CALL  RS1
               MOV   A,#'C'
               CALL  RS1
               CALL  SV_PV
               JB    26H.0,PON
               MOV   A,#92H
               CALL  RS0
               MOV   A,#' '
               CALL  RS1
               JMP   PON2
PON:           MOV   A,#92H
               CALL  RS0
               MOV   A,#2AH
               CALL  RS1
PON2:          JNB   25H.0,DS1
               MOV   A,#0C7H
               CALL  RS0
               JMP   PON1
DS1:           JNB   25H.1,DS2
               MOV   A,#0C5H
               CALL  RS0
               JMP   PON1
DS2:           JNB   25H.2,DS3
               MOV   A,#0C4H
               CALL  RS0
               JMP   PON1
DS3:           JNB   25H.3,PON1
               MOV   A,#0C3H
               CALL  RS0
PON1:          RET
RS0:
               CALL  CHECK_BUSY
               MOV   P1,A
               CLR   RS
               CLR   R_W
               SETB  ENABLE
               CLR   ENABLE
               RET
RS1:
               CALL  CHECK_BUSY
               MOV   P1,A
               SETB  RS
               CLR   R_W
               SETB  ENABLE
               CLR   ENABLE
               RET
CHECK_BUSY:
               MOV   P1,#0FFH
               CLR   RS
               SETB  R_W
               SETB  ENABLE
               MOV   C,P1.7
               CLR   ENABLE
               JC    CHECK_BUSY
               RET
LCD_RESET:
               MOV   A,#00110000B
               CALL  RS0
               MOV   R2,#41
$1:            MOV   R1,#50
               DJNZ  R1,$
               DJNZ  R2,$1
               MOV   A,#00110000B
               CALL  RS0
               MOV   R1,#50
               DJNZ  R1,$
               MOV   A,#00110000B
               CALL  RS0
               RET
READ:
               JNB   P2.4,READ
               MOV   A,P2
               ANL   A,#0FH
               MOV   24H,#0
               CJNE  A,#08,R1
               MOV   24H,#10H
R1:            JNB   P2.5,R1
               MOV   A,P2
               ANL   A,#0FH
               ORL   24H,A
R2:            JNB   P2.6,R2
               MOV   A,P2
               ANL   A,#0FH
               SWAP  A
               MOV   23H,A
R3:            JNB   P2.7,R3
               MOV   A,P2
               ANL   A,#0FH
               ORL   23H,A
               RET
CHPN:
               CLR   A
               MOV   20H,A
               MOV   21H,A
               MOV   A,24H
               ANL   A,0F0H
               ORL   21H,A
               JNB   24H.0,CH1
               SETB  21H.3
CH1:           JNB   24H.1,CH2
               SETB  21H.2
CH2:           JNB   24H.2,CH3
               SETB  21H.1
CH3:           JNB   24H.3,CH4
               SETB  21H.0
CH4:           JNB   23H.0,CH5
               SETB  20H.3
CH5:           JNB   23H.1,CH6
               SETB  20H.2
CH6:           JNB   23H.2,CH7
               SETB  20H.1
CH7:           JNB   23H.3,CH8
               SETB  20H.0
CH8:           JNB   23H.4,CH9
               SETB  20H.7
CH9:           JNB   23H.5,CH10
               SETB  20H.6
CH10:          JNB   23H.6,CH11
               SETB  20H.5
CH11:          JNB   23H.7,CH12
               SETB  20H.4
CH12:          RET
ONV1:
               MOV   A,21H
               SWAP  A
               ANL   A,#0FH
               ORL   A,#30H
               MOV   35H,A
               MOV   A,21H
               ANL   A,#0FH
               ORL   A,#30H
               MOV   34H,A
               MOV   A,20H
               SWAP  A
               ANL   A,#0FH
               ORL   A,#30H
               MOV   33H,A
               MOV   A,20H
               ANL   A,#0FH
               ORL   A,#30H
               MOV   32H,A
               MOV   A,31H
               SWAP  A
               ANL   A,#0FH
               ORL   A,#30H
               MOV   39H,A
               MOV   A,31H
               ANL   A,#0FH
               ORL   A,#30H
               MOV   38H,A
               MOV   A,30H
               SWAP  A
               ANL   A,#0FH
               ORL   A,#30H
               MOV   37H,A
               MOV   A,30H
               ANL   A,#0FH
               ORL   A,#30H
               MOV   36H,A
               RET
DISP:
               MOV   A,#80H
               CALL  RS0
               MOV   R2,20
DI1:           MOV   A,#' '
               CALL  RS1
               DJNZ  R2,DI1
               MOV   A,#0C0H
               CALL  RS0
               MOV   R2,20
DI2:           MOV   A,#' '
               CALL  RS1
               DJNZ  R2,DI2
               RET
SCAN1:
               MOV   3BH,#0
               MOV   22H,#11101111B
SC1:           MOV   P0,22H
               MOV   3CH,#04
               MOV   A,P0
SC2:           RRC   A
               JC    NOKEY
               MOV   A,3BH
               JMP   SC3
NOKEY:         INC   3BH
               DJNZ  3CH,SC2
               MOV   A,22H
               RL    A
               MOV   22H,A
               JB    22H.0,SC1
               MOV   A,3BH
               SETB  C
SC3:           RET
SV_PV:


               CLR   C
               MOV   A,30H
               SUBB  A,20H
               MOV   3DH,A
               MOV   A,31H
               SUBB  A,21H
               MOV   3EH,A
               JC    SV1
               SETB  P3.4
               SETB  26H.0
               JMP   SV2
SV1:           CLR   P3.4
               CLR   26H.0

SV2:           RET
               END
