;------------------------------------------
; Experiment lab no.5 INCLUDED FILE
;------------------------------------------
;
; Scan display and delay
; IN:  DISBUF, R2 = DELAY Value.
;
SCAN_AND_DELAY:
        MOV     A,R2            ;DELAY VALUE
        MOV     R4,A
SCAND1:
        MOV     R5,#4           ;LOOP
        MOV     R6,#0           ;DIGIT
        MOV     R7,#DISBUF
SCAND2:
        LCALL   SCAN_SEGMENT
        DJNZ    R5,SCAND2
        DJNZ    R4,SCAND1
        RET
;
; Scan a specified digit and delay for a while
; IN: R6 = DIGIT, R7 = INDEX TO DISBUF
; OUT: R6 = R6+1, R7 = R7 + 1
;
SCAN_SEGMENT:
        MOV     A,P1
        ANL     A,#11111100B    ;clear p1.0 & p1.1
        ORL     A,R6            ;active select digit
        MOV     P1,A            ;and sent to port
        MOV     A,R7            ;get data to be displayed
        MOV     R0,A
        MOV     A,@R0
        MOV     DPTR,#PA_8255
        MOVX    @DPTR,A         ;sent out to port a
;
        MOV     R2,#4           ;DELAY
SCNSG1: MOV     R3,#0
        DJNZ    R3,$
        DJNZ    R2,SCNSG1
;
        CLR     A               ;clear data in this digit
        MOVX    @DPTR,A
SCNSG2: INC     R6              ;step to next digit
        INC     R7              ;and next index in DISBUF
        RET
;
; Scan display and wait for key press
; IN: DISBUF, SCANPF (KEY PRESS FLAG)
; OUT = A
;
SCAN_AND_WAIT:
        MOV     R4,#4           ;LOOP
        MOV     R5,#4           ;RELEASE KEY COUNT
        MOV     R6,#0           ;KEY COUNT
        MOV     R7,#DISBUF
SCAN1:
        LCALL   SCAN_SEGMENT    ;OUT SEGMENT
        MOV     DPTR,#PC_8255   ;CHECK KEY
        MOVX    A,@DPTR
        SWAP    A
        ANL     A,#0F0H
        CJNE    A,#0F0H,SCAN3
SCAN11:
        DJNZ    R5,SCAN2        ;CHECK KEY RELEASE
        CLR     SCANPF
SCAN2:  DJNZ    R4,SCAN1
        SJMP    SCAN_AND_WAIT
SCAN3:
        JB      SCANPF,SCAN2    ;KEY NOT RELEASE
        SETB    SCANPF
        ORL     A,R6
        MOV     B,A
;
        MOV     R2,#16          ;FIND TABLE
        MOV     R3,#0
        MOV     DPTR,#KEYTABLE
SCAN4:  MOV     A,R3
        MOVC    A,@A+DPTR
        CJNE    A,B,SCAN5
        MOV     A,R3
        RET                     ;EXIT NEW PRESS
SCAN5:  INC     R3
        DJNZ    R2,SCAN4
        MOV     A,#0FFH
        RET                     ;EXIT ERROR
;
KEYTABLE:
        DB      0E1H, 0E2H, 0E3H, 0E4H
        DB      0D1H, 0D2H, 0D3H, 0D4H
        DB      0B1H, 0B2H, 0B3H, 0B4H
        DB      071H, 072H, 073H, 074H
;
; One hex byte to segment byte conversion
; IN: R2 = hex value (0-F)
; OUT: A = SEGMENT CODE
;
HTOSX:
        MOV     A,R2            ;MAKE SURE 0-F
        ANL     A,#0FH
        MOV     R2,A
        MOV     DPTR,#SEGTABLE  ;TABLE
        MOV     A,DPL
        ADD     A,R2
        MOV     DPL,A
        MOV     A,DPH
        ADDC    A,#0
        MOV     DPH,A
        CLR     A
        MOVC    A,@A+DPTR
        RET
;
SEGTABLE:
        DB      3FH, 06H, 5BH, 4FH         ;0 1 2 3
        DB      66H, 6DH, 7DH, 07H         ;4 5 6 7
        DB      7FH, 6FH, 77H, 7CH         ;8 9 A B
        DB      39H, 5EH, 79H, 71H         ;C D E F
;
; Covert hex bytes in HEXBUF to segment code and store in DISBUF
; IN: HEXBUF
; OUT: DISBUF
;
HEXBUF_TO_SEGMENT:
        MOV     R0,#HEXBUF      ;source buffer
        MOV     R1,#DISBUF      ;destination buffer
        LCALL   HTOSS
        LCALL   HTOSS
        RET
;
HTOSS:
        MOV     A,@R0           ;left digit
        PUSH    ACC
        SWAP    A
        ANL     A,#0FH
        MOV     R2,A
        LCALL   HTOSX
        MOV     @R1,A
        INC     R1
        POP     ACC             ;right digit
        ANL     A,#0FH
        MOV     R2,A
        LCALL   HTOSX
        MOV     @R1,A
        INC     R1
        INC     R0
        RET
;
; Initial 8255 mode of operation
;
INIT_8255:
        MOV     DPTR,#CTL_8255  ;Init 8255 mode
        MOV     A,#81H          ;PC = input
        MOVX    @DPTR,A
        RET
