; FILENAME      VC51MQC.ASM
; DESCRIPTION   MCS51 QC BOARD V-C51M V1.0
; HARDWARE      SILA MCS-51 BOARD V-C51M V1.0
; ASSEMBLER     SXA51
; START-DATE    28/04/40
; SOFTWARE ENG. APICHIT B.
; COMPANY       SILA RESEARCH CO.,LTD.

; ************** INTERNAL RAM **********
         ORG    00H
         DS     8
SYSSTK:  DS     24                ;SYSTEM STACK
DISBUF:  DS     8                 ;DISPLAY BUFFER
TIMBUF:  DS     7                 ;DD MM YY HH MM SS WW
RAMBUF:  DS     32                ;DS2223 RAM BUFFER

; **************** VARIABLE ************
WTDBIT   EQU    P1.7              ;WATCH DOG
BRAT96   EQU    0FDH              ;9600
LED1     EQU    P1.5              ;LED1
LED2     EQU    P1.6              ;LED2
ERAMDA   EQU    P3.2              ;DS2223 DATA
RELAY    EQU    P3.3              ;RELAY /INT1
RTCDAT   EQU    P3.6              ;SERIAL DATA DS1202 RTC
RTCCLK   EQU    P3.5              ;SERIAL CLK  DS1202 RTC
RTCRST   EQU    P3.7              ;SERIAL RST  DS1202 RTC
SOUNDB   EQU    P1.4              ;SEND SOUND
COMDRV   EQU    T0                ;RS485 DRIVE

; ************ V-C51M V1.0 *************
         ORG    0000H
         MOV    SP,#SYSSTK

MAIN:    MOV    R2,#5H            ;POWER UP DELAY
         CALL   DTSEC
         CALL   PBEEP             ;OPEN PROGRAM
         MOV    TMOD,#20H         ;SET BAUD RATE(TIMER 1,MODE 2)
         MOV    SCON,#52H         ;SET MODE 1
         MOV    TH1,#BRAT96       ;BAUD RATE 9600
         SETB   TR1
         CLR    ERAMDA            ;P3.2
         CALL   ERAMST            ;START UP DS2223
         CLR    RTCCLK            ;P3.5
         CLR    RTCDAT            ;P3.6
         CLR    RTCRST            ;P3.7
         LCALL  SHOW              ;SEND SHOW MONITOR
         CALL   SOFF              ;RS485 OFF
         CALL   HBEEP             ;SOUND HBEEP

; *** CONDITION CHECK DIPSW CONTROL ****
MAIN1:   MOV    P1,#0FFH
         MOV    A,P1
         ANL    A,#0FH
         CJNE   A,#0FH,$+6
         LJMP   TELS              ;TEST LED AND SOUND
         CJNE   A,#0EH,$+6
         LJMP   TEPD              ;TEST PORT USER
         CJNE   A,#0DH,$+6
         LJMP   TEK               ;TEST KEY
         CJNE   A,#0CH,$+6
         LJMP   TER               ;TEST RELAY
         CJNE   A,#0BH,$+6
         LJMP   TEDS              ;TEST DS1202 AND DS2223
         CJNE   A,#0AH,$+6
         LJMP   TERS              ;TEST RS232 AND RS485
         CJNE   A,#9H,$+6
         LJMP   TEB               ;TEST BACKUP AND WATCH DOG
         CJNE   A,#8H,$+6
         LJMP   TESE              ;TEST SEGMENT
; *** CONDITION CHECK DIPSW RUN TEST ***
         CJNE   A,#7H,$+6
         LJMP   RTELS             ;RUN TEST LED AND SOUND
         CJNE   A,#6H,$+6
         LJMP   RTEPD             ;RUN TEST PORT USER
         CJNE   A,#5H,$+6
         LJMP   RTEK              ;RUN TEST KEY
         CJNE   A,#4H,$+6
         LJMP   RTER              ;RUN TEST RELAY
         CJNE   A,#3H,$+6
         LJMP   RTEDS             ;RUN TEST DS1202 AND DS2223
         CJNE   A,#2H,$+6
         LJMP   RTERS             ;RUN TEST RS232 AND RS485
         CJNE   A,#1H,$+6
         LJMP   RTEB              ;RUN TEST BACKUP AND WATCH DOG
         CJNE   A,#0H,$+6
         LJMP   RTESE             ;RUN TEST SEGMENT
         MOV    R7,#4H
         MOV    DPTR,#SHO34E
         LCALL  RSLCD
         DJNZ   R7,$-6
         MOV    R2,#25
         CALL   DTSEC
         LJMP   MAIN1
; *********** END CONDITION ************

; ****** START TEST LED AND SOUND ******
TELS:    CALL   SLF               ;CLEAR
         MOV    DPTR,#SHO1F       ;MONITOR SHOW
         CALL   RSLCD             ;SEND SERIAL SHOW
         MOV    DPTR,#SHO2T1
         CALL   RSLCD
         MOV    DPTR,#SHO3F
         CALL   RSLCD
         MOV    DPTR,#SHO4F
         CALL   RSLCD
         CALL   LBEEP
         MOV    R2,#20
         CALL   DTSEC
         SJMP   RTELS
         LJMP   MAIN1
         CJNE   A,#0FH,$-3
RTELS:   MOV    P1,#0FFH
         MOV    A,P1
         ANL    A,#0FH
         CJNE   A,#7H,RTELS-3
         CALL   SLF
         MOV    DPTR,#SHO1F
         CALL   RSLCD
         MOV    DPTR,#SHO2T1
         CALL   RSLCD
         MOV    DPTR,#SHO3T1
         CALL   RSLCD
         MOV    DPTR,#SHO4T1
         CALL   RSLCD

RTELS1:  MOV    R0,#4H            ;TRIG LED1 AND LED2
         CLR    LED1
         CLR    LED2
         CALL   RBEEP
         MOV    R2,#3H
         CALL   DTSEC
         SETB   LED1
         CALL   LBEEP
         MOV    R2,#3H
         CALL   DTSEC
         SETB   LED2
         CALL   LBEEP
         MOV    R2,#3H
         CALL   DTSEC
         DJNZ   R0,RTELS1+2
         CALL   PBEEP
         SJMP   RTELS2
         LJMP   MAIN1
         CJNE   A,#0FH,$-3
         LJMP   TELS
RTELS2:  MOV    P1,#0FFH          ;CONDITION CHECK
         MOV    A,P1
         ANL    A,#0FH
         CJNE   A,#7H,RTELS2-6
         LJMP   RTELS1

; ******* END TEST LED AND SOUND *******

; ******** START TEST PORT USER ********
TEPD:    CALL   SLF               ;CLEAR
         MOV    DPTR,#SHO1F       ;MONITOR SHOW
         CALL   RSLCD             ;SEND SERIAL SHOW
         MOV    DPTR,#SHO2T2
         CALL   RSLCD
         MOV    DPTR,#SHO3F
         CALL   RSLCD
         MOV    DPTR,#SHO4F
         CALL   RSLCD
         CALL   LBEEP
         MOV    R2,#20
         CALL   DTSEC
         SJMP   RTEPD
         LJMP   MAIN1
         CJNE   A,#0EH,$-3
RTEPD:   MOV    P1,#0FFH
         MOV    A,P1
         ANL    A,#0FH
         CJNE   A,#6H,RTEPD-3
         CALL   SLF
         MOV    DPTR,#SHO1F
         CALL   RSLCD
         MOV    DPTR,#SHO2T2
         CALL   RSLCD
         MOV    DPTR,#SHO34R
         CALL   RSLCD
         MOV    DPTR,#SHO34K
         CALL   RSLCD
         CALL   LBEEP

RTEPD0:  MOV    R5,#4             ;OUT TO PORT USER
         MOV    R6,#2
         MOV    R7,#4
         MOV    A,#0H
         CALL   RTEPD3
         MOV    A,#0AAH
RTEPD1:  LCALL  RTEPD3
         RL     A
         DJNZ   R5,RTEPD1
         MOV    R5,#4
         DJNZ   R6,RTEPD1
         MOV    R6,#2
         MOV    A,#1H
         DJNZ   R7,RTEPD1
         MOV    R2,#5H            ;DELAY & NEXT LOOP
         LCALL  DTSEC
         MOV    A,#0FFH
         CALL   RTEPD3
         MOV    P0,#00H
         CALL   LBEEP
         MOV    R2,#9H
         CALL   DTSEC
         MOV    R2,#9H
         CALL   DTSEC
         MOV    P2,#00H
         CALL   PBEEP
         MOV    R2,#9H
         CALL   DTSEC
         SJMP   RTEPD2
         LJMP   MAIN1
         CJNE   A,#0EH,$-3
         LJMP   TEPD
RTEPD2:  MOV    P1,#0FFH          ;CONDITION CHECK
         MOV    A,P1
         ANL    A,#0FH
         CJNE   A,#6H,RTEPD2-6
         LJMP   RTEPD0

RTEPD3:  MOV    P2,A              ;MOVE DATA TO PORT USER
         MOV    P0,A
         MOV    R2,#6H            ;DELAY & NEXT LOOP
         LCALL  DTSEC
         RET

; ******** END TEST PORT USER **********

; ******* START TEST KEY 3 x 4 *********
TEK:     CALL   SLF
         MOV    DPTR,#SHO1F       ;MONITER SHOW
         CALL   RSLCD
         MOV    DPTR,#SHO2T3
         CALL   RSLCD
         MOV    DPTR,#SHO3F
         CALL   RSLCD
         MOV    DPTR,#SHO4F
         CALL   RSLCD
         CALL   LBEEP
         MOV    R2,#20
         CALL   DTSEC
         SJMP   RTEK
         LJMP   MAIN1
         CJNE   A,#0DH,$-3
RTEK:    MOV    P1,#0FFH
         MOV    A,P1
         ANL    A,#0FH
         CJNE   A,#5H,RTEK-3
         CALL   SLF
         MOV    DPTR,#SHO1F
         CALL   RSLCD
         MOV    DPTR,#SHO2T3
         CALL   RSLCD
         MOV    DPTR,#SHO3T3
         CALL   RSLCD
         MOV    DPTR,#SHO4T3
         CALL   RSLCD
         CALL   LBEEP

SCANK:   MOV    R6,#0FH
         MOV    R2,#3H
         MOV    A,R6              ;OUT DIGIT
         MOV    B,R6
         MOV    P2,A              ;CONTROL LINE 1
         MOV    A,P2
         MOV    R5,A              ;GET DATA FROM KEY
         ANL    A,R6              ;NOT PUSH KEY (A=R6) = R6=B
         MOV    R7,A              ;GET DATA
         MOV    A,B
         ADD    A,#10H            ;INC CONTROL COLUMN
         MOV    R6,A
         MOV    A,R7
         CJNE   A,B,SCANK0        ;OUT = R5
         DEC    R2
         CJNE   R2,#00H,SCANK+4
         SJMP   SCANK             ;LOOP SCAN KEY WHEN NOT PUSH KEY

SCANK0:  MOV    R1,#0H            ;INCREMENT DATA PUSH KEY
         MOV    R6,#7H
         CALL   SCANK1            ;CALL COMPARE VALUE KEY
         JNZ    SCANKC            ;ACC = 0H NOT JUMP
         MOV    R6,#0BH
         CALL   SCANK1
         JNZ    SCANKC
         MOV    R6,#0DH
         CALL   SCANK1
         JNZ    SCANKC
         MOV    R6,#0EH
         CALL   SCANK1
         JNZ    SCANKC
         LJMP   SCANK             ;KEY ERROR JUMP SCAN KEY

SCANKC:  MOV    A,R1              ;CHECK KEY AND CONVER DATA SHOW
         CJNE   A,#10,SCANKC0
         CALL   SLF
         LJMP   TEK
SCANKC0: CJNE   A,#12,SCANKC1
         MOV    A,#'#'
         SJMP   SCANKC2
SCANKC1: CJNE   A,#11,$+5         ;KEY = 11 GET OUT TEST KEY
         MOV    A,#00H
         ADD    A,#30H
SCANKC2: CALL   SBYTE
         MOV    R2,#2
         CALL   DTSEC
         SJMP   SCANK

SCANK1:  MOV    R2,#3H
         MOV    B,R5              ;R5 GET OUTPUT KEY
SCANK10: MOV    A,R6              ;R6 TABLE
         MOV    R7,A
         ADD    A,#10H
         MOV    R6,A
         MOV    A,R7
         INC    R1
         CJNE   A,B,SCANK2        ;TABLE = DATAKEY GETOUT
         SJMP   SCANK3
SCANK2:  DEC    R2
         CJNE   R2,#00H,SCANK10
         MOV    A,#00H
SCANK3:  RET

; ********** END TEST KEY 3x4 **********

; ********** START TEST RELAY **********
TER:     CALL   SLF
         MOV    DPTR,#SHO1F       ;MONITER SHOW
         CALL   RSLCD
         MOV    DPTR,#SHO2T4
         CALL   RSLCD
         MOV    DPTR,#SHO3F
         CALL   RSLCD
         MOV    DPTR,#SHO4F
         CALL   RSLCD
         CALL   LBEEP
         MOV    R2,#20
         CALL   DTSEC
         SJMP   RTER
         LJMP   MAIN1
         CJNE   A,#0CH,$-3
RTER:    MOV    P1,#0FFH
         MOV    A,P1
         ANL    A,#0FH
         CJNE   A,#4H,RTER-3
         CALL   SLF
         MOV    DPTR,#SHO1F
         CALL   RSLCD
         MOV    DPTR,#SHO2T4
         CALL   RSLCD
         MOV    DPTR,#SHO3T1
         CALL   RSLCD
         MOV    DPTR,#SHO4T1
         CALL   RSLCD
         CALL   LBEEP
         SETB   RELAY
         MOV    R7,#8
RTER1:   CPL    RELAY
         MOV    R2,#18
         LCALL  DTSEC
         DJNZ   R7,RTER1
         SJMP   RTER2
         LJMP   MAIN1
         CJNE   A,#0CH,$-3
         LJMP   TER
RTER2:   MOV    P1,#0FFH
         MOV    A,P1
         ANL    A,#0FH
         CJNE   A,#4H,RTER2-6
         LJMP   RTER1-2

; *********** END TEST RELAY ***********

; **** START TEST DS1202 AND DS2223 ****
TEDS:    CALL   SLF
         MOV    DPTR,#SHO1F       ;MONITER SHOW
         CALL   RSLCD
         MOV    DPTR,#SHO2T5
         CALL   RSLCD
         MOV    DPTR,#SHO3F
         CALL   RSLCD
         MOV    DPTR,#SHO4F
         CALL   RSLCD
         CALL   LBEEP
         MOV    R2,#20
         CALL   DTSEC
         SJMP   RTEDS
         LJMP   MAIN1
         CJNE   A,#0BH,$-3
RTEDS:   MOV    P1,#0FFH
         MOV    A,P1
         ANL    A,#0FH
         CJNE   A,#3H,RTEDS-3
         MOV    TIMBUF+0,#31H     ;SET DATE
         MOV    TIMBUF+1,#12H     ;SET MONTH
         MOV    TIMBUF+2,#96H     ;SET YEAR
         MOV    TIMBUF+3,#23H     ;SET HOUR
         MOV    TIMBUF+4,#59H     ;SET MINUTE
         MOV    TIMBUF+5,#54H     ;SET SECOND
         MOV    TIMBUF+6,#3H      ;SET WEEK-DAY
         CALL   TIMEWR
         CALL   TIMERD
         MOV    A,TIMBUF+0
         CJNE   A,#31H,RTEDS0     ;ERROR JUMP SHOW RSLCD
         MOV    A,TIMBUF+2
         CJNE   A,#96H,RTEDS0     ;ERROR JUMP SHOW RSLCD
         CALL   LBEEP
         SJMP   RTEDS1

RTEDS0:  MOV    DPTR,#SHO1F       ;RSLCD FIX SHOW
         CALL   RSLCD
         MOV    DPTR,#SHO2T5      ;RSLCD FIX SHOW
         CALL   RSLCD
         MOV    DPTR,#SHO3TE      ;RSLCD ERROR DS1202
         CALL   RSLCD
         MOV    DPTR,#SHO4F       ;RSLCD FIX SHOW
         CALL   RSLCD
         MOV    R2,#25
         CALL   DTSEC
         MOV    R0,#8
         LCALL  EBEEP             ;DS1202 ERROR
         MOV    R2,#3
         LCALL  DTSEC
         DJNZ   R0,$-8            ;8 LOOP
         SJMP   RTEDS

RTEDS1:  MOV    DPTR,#SHO1F       ;RSLCD FIX
         CALL   RSLCD
         MOV    DPTR,#SHO2T5      ;RSLCD TEST DS1202
         CALL   RSLCD
         CALL   TIMERD
         MOV    R1,#TIMBUF        ;RSLCD SHOW TIME
         CALL   RTCLCD
         MOV    DPTR,#SHO34R      ;RSLCD SHOW SAFT TEST
         CALL   RSLCD
         MOV    A,TIMBUF
         CJNE   A,#1H,RTEDS2      ;CHECK END
         MOV    A,TIMBUF+1
         CJNE   A,#1H,RTEDS2      ;CHECK END
         MOV    A,TIMBUF+2
         CJNE   A,#97H,RTEDS2     ;CHECK END
         MOV    A,TIMBUF+3
         CJNE   A,#00H,RTEDS2     ;CHECK END
         MOV    A,TIMBUF+4
         CJNE   A,#00H,RTEDS2     ;CHECK END
         MOV    A,TIMBUF+5
         CJNE   A,#5H,RTEDS2      ;CHECK END
         MOV    A,TIMBUF+6
         CJNE   A,#4H,RTEDS2      ;CHECK END
         MOV    R0,#4
         LCALL  RBEEP
         MOV    R2,#2
         LCALL  DTSEC
         DJNZ   R0,$-8
         LJMP   RTEDS3            ;LOOP TEST DS1202

RTEDS2:  LCALL  LBEEP             ;SOUND WHEN ERROR
         MOV    R2,#9
         CALL   DTSEC
         SJMP   RTEDS1

; ********** DS2223 **********
; TEST RAM,DS2223
; WRITE TO RAMBUF
; REG  = A,R0
RTEDS3:  MOV    DPTR,#SHO3TK
         MOV    R0,#32
         MOV    R1,#RAMBUF
         CLR    A
         MOVC   A,@A+DPTR
         MOV    @R1,A
         INC    R1
         INC    DPTR
         DJNZ   R0,$-5

         CALL   RTEDS5
         LCALL  LBEEP
         MOV    R2,#20
         CALL   DTSEC
         LCALL  LBEEP
         CALL   ERAMWR            ;WRITE DATA IN RAMBUF TO DS2223

         MOV    R4,#32
         MOV    R0,#RAMBUF
         CLR    A                 ;CLEAR DATA IN RAMBUF
         MOV    @R0,A
         INC    R0
         DJNZ   R4,$-3

         CALL   ERAMRD
         LCALL  LBEEP
         CALL   RTEDS5
         LCALL  LBEEP
         MOV    DPTR,#SHO3TK
         MOV    R0,#RAMBUF
         MOV    R3,#32
RTEDS4:  CLR    A                 ;COMPARE DATA IN DS2223
         MOVC   A,@A+DPTR         ;DATA ASCII
         CPL    A                 ;COMPLEMENT ACC
         ANL    A,@R0             ;AND (ACC = @R0) = 00
         INC    R0
         INC    DPTR
         DEC    R3
         CJNE   A,#00H,RTEDS3R    ;RAMBUF = DATA ASCII DS2223 FAIL
         CJNE   R3,#00,RTEDS4     ;TEST 32 LOOP

RTEDS3W: MOV    DPTR,#SHO3TK      ;END TEST DS2223 GOOD
         CALL   RTED00
         CALL   RTEDS5
         LCALL  LBEEP
         MOV    DPTR,#SHO4TK
         CALL   RTED16
         CALL   RTEDS5
         LCALL  LBEEP
         MOV    R2,#20
         CALL   DTSEC
         MOV    DPTR,#SHO3F
         MOV    R0,#32
         MOV    R1,#RAMBUF
         CLR    A
         MOVC   A,@A+DPTR
         MOV    @R1,A
         INC    R1
         INC    DPTR
         DJNZ   R0,$-5
         CALL   ERAMWR
         LJMP   TEDS

RTEDS3R: MOV    DPTR,#SHO3TK      ;END TEST DS2223 FAIL
         CALL   RTED00
         CALL   RTEDS5
         LCALL  EBEEP
         MOV    DPTR,#SHO4TE
         CALL   RTED16
         CALL   RTEDS5
         LCALL  EBEEP
         MOV    R2,#5H
         LCALL  DTSEC
         SJMP   $-8

RTED00:  MOV    R0,#16            ;CLEAR DATA RAMBUF 0 - 15
         MOV    R1,#RAMBUF
         CLR    A
         MOVC   A,@A+DPTR
         MOV    @R1,A
         INC    R1
         INC    DPTR
         DJNZ   R0,$-5
         RET

RTED16:  MOV    R0,#16            ;CLEAR DATA RAMBUF 16 - 32
         MOV    R1,#RAMBUF+16
         CLR    A
         MOVC   A,@A+DPTR
         MOV    @R1,A
         INC    R1
         INC    DPTR
         DJNZ   R0,$-5
         RET

RTEDS5:  MOV    DPTR,#SHO1F       ;GET DATA RAMBUF TO RSLCD TEST DS2223
         CALL   RSLCD
         MOV    DPTR,#SHO2T5
         CALL   RSLCD
         MOV    R1,#RAMBUF
         CALL   RSLCD2
         MOV    R1,#RAMBUF+16
         CALL   RSLCD2
         MOV    R2,#5
         CALL   DTSEC
         RET

; ***** END TEST DS1202 AND DS2223 *****

; ********** START TEST RS232 **********
TERS:    CALL   SLF
         CLR    COMDRV            ;TRIG RS485 = RXD
         MOV    DPTR,#SHO1F
         CALL   RSLCD
         MOV    DPTR,#SHO2T6
         CALL   RSLCD
         MOV    DPTR,#SHO3F
         CALL   RSLCD
         MOV    DPTR,#SHO4F
         CALL   RSLCD
         CALL   LBEEP
         MOV    R2,#20
         CALL   DTSEC
         SJMP   RTERS
         LJMP   MAIN1
         CJNE   A,#0AH,$-3
RTERS:   MOV    P1,#0FFH
         MOV    A,P1
         ANL    A,#0FH
         CJNE   A,#2H,RTERS-3
         CALL   SLF
         MOV    DPTR,#SHO1F
         CALL   RSLCD
         MOV    DPTR,#SHO2T6
         CALL   RSLCD
         MOV    DPTR,#SHO34K
         CALL   RSLCD
         MOV    DPTR,#SHO4T3
         CALL   RSLCD
         MOV    R2,#25
         CALL   DTSEC
;******** TEST RS232 *********
RTERS1:  MOV    B,#'0'
RTERS2:  MOV    A,B               ;LOOP (TXD)
         CALL   SBYTE
         MOV    R2,#2
         CALL   DTSEC
         INC    B
         MOV    A,B
         CJNE   A,#'Z'+6,RTERS2
         CALL   SLF
         MOV    R7,#16
RTERSY:  CALL   SON               ;TXD
         CALL   RBYTE             ;RXD
         CLR    COMDRV            ;RXD
         CALL   SBYTE             ;TXD
         CJNE   A,#20H,RTERSZ
         SJMP   RTS485
RTERSZ:  CALL   KBEEP
         DJNZ   R7,RTERSY

;******** TEST RS485 *********
RTS485:  CALL   SLF
         MOV    DPTR,#SHO1F
         CALL   RSLCD
         MOV    DPTR,#SHO2T6
         CALL   RSLCD
         MOV    DPTR,#SHO34K
         CALL   RSLCD
         MOV    DPTR,#SHO4T3
         CALL   RSLCD
         MOV    R2,#20
         CALL   DTSEC
         MOV    R7,#8
         CALL   SON               ;OFF RXD IT'S TXD
RTERS3:  MOV    B,#'0'
RTERS4:  MOV    A,B               ;LOOP (TXD)
         CALL   SBYTE
         MOV    R2,#3
         CALL   DTSEC
         INC    B
         MOV    A,B
         CJNE   A,#'Z'+6,RTERS4
         DJNZ   R7,RTERS3
         CALL   SLF
         CLR    COMDRV            ;RXD
         SJMP   RTERS5
         LCALL  SLF
         LJMP   MAIN1
         CJNE   A,#0AH,$-6
         LJMP   TERS
RTERS5:  MOV    P1,#0FFH
         MOV    A,P1
         ANL    A,#0FH
         CJNE   A,#2H,RTERS5-6
         LJMP   RTERS1

; ****** END TEST RS232 AND RS485 ******

; ****** START TEST BACKUP,LB,WD *******
TEB:     CALL   SLF
         MOV    DPTR,#SHO3F       ;MONITER SHOW
         MOV    R0,#32
         MOV    R1,#RAMBUF        ;INCREMENT DATA TO RAMBUF
         CLR    A
         MOVC   A,@A+DPTR
         MOV    @R1,A
         INC    R1
         INC    DPTR
         DJNZ   R0,$-5
         CALL   ERAMWR
         CALL   SLF
         MOV    DPTR,#SHO1F       ;MONITER SHOW GET OFFPOWER
         CALL   RSLCD             ;AND DIPSW = 4 RUN
         MOV    DPTR,#SHO2T7      ;AND ONPOWER
         CALL   RSLCD
         MOV    DPTR,#SHO3F
         CALL   RSLCD
         MOV    DPTR,#SHO4T7
         CALL   RSLCD
         LCALL  LBEEP
         MOV    R2,#20
         CALL   DTSEC
         SJMP   RTEB
         LJMP   MAIN1
         CJNE   A,#9H,$-3
RTEB:    MOV    P1,#0FFH          ;OFF POWER
         MOV    A,P1
         ANL    A,#0FH
         CJNE   A,#1H,RTEB-3
         CALL   ERAMRD
         MOV    DPTR,#SHO3F
         MOV    R0,#RAMBUF
         MOV    R3,#32
RTEB1:   CLR    A
         MOVC   A,@A+DPTR         ;DATA ASCII
         CPL    A                 ;COMPLEMENT ACC
         ANL    A,@R0             ;AND (ACC = @R0) = 00
         INC    R0
         INC    DPTR
         DEC    R3
         CJNE   A,#00H,RTEB3      ;JUMP IF BACKUP DS2223 FAIL
         CJNE   R3,#00H,RTEB1     ;TEST 32 LOOP
         MOV    DPTR,#SHO1F       ;BACKUP GOOD
         CALL   RSLCD
         MOV    DPTR,#SHO2T7
         CALL   RSLCD
         MOV    DPTR,#SHO3B7
         CALL   RSLCD
         MOV    DPTR,#SHO34K
         CALL   RSLCD
         CALL   RBEEP
         MOV    R2,#20
         CALL   DTSEC
         SJMP   RTEBC
         LJMP   MAIN1
         CJNE   A,#9H,$-3
         LJMP   TEB
RTEBC:   MOV    P1,#0FFH          ;CHECK SW DIP 4
         MOV    A,P1
         ANL    A,#0FH
         CJNE   A,#1H,RTEBC-6
         LJMP   RTEB

RTEB3:   CALL   SLF               ;MONITER SHOW BATTERY ERROR
         MOV    DPTR,#SHO1F
         CALL   RSLCD
         MOV    DPTR,#SHO2T7
         CALL   RSLCD
         MOV    DPTR,#SHO3BE
         CALL   RSLCD
         MOV    DPTR,#SHO34E
         CALL   RSLCD
         LCALL  EBEEP
         MOV    R2,#5H
         LCALL  DTSEC
         LCALL  EBEEP
         SJMP   $-11

; ******* END TEST BACKUP,LB,WD ********

; ****** START TEST PORT SEGMENT *******
TESE:    CALL   SLF
         MOV    DPTR,#SHO1F       ;SHOW MONITER PROGRAM
         CALL   RSLCD
         MOV    DPTR,#SHO2T8
         CALL   RSLCD
         MOV    DPTR,#SHO3F
         CALL   RSLCD
         MOV    DPTR,#SHO4F
         CALL   RSLCD
         CALL   LBEEP
         MOV    R2,#20
         CALL   DTSEC
         SJMP   RTESE
         LJMP   MAIN1
         CJNE   A,#8H,$-3
RTESE:   MOV    P1,#0FFH
         MOV    A,P1
         ANL    A,#0FH
         CJNE   A,#00H,RTESE-3
         CALL   SLF
         MOV    DPTR,#SHO1F
         CALL   RSLCD
         MOV    DPTR,#SHO2T8
         CALL   RSLCD
         MOV    DPTR,#SHO3T1
         CALL   RSLCD
         MOV    DPTR,#SHO4T1
         CALL   RSLCD
         MOV    R2,#15
         CALL   DTSEC
         CALL   RTESED            ;CALL CLEAR DATA IN DISBUF

RTESE0:  MOV    R7,#8
         MOV    R0,#DISBUF
         MOV    R6,#1H
         MOV    R5,#64
RTESE00: MOV    A,R6
         MOV    @R0,A
         INC    R0
         DJNZ   R7,RTESE00
         RL     A
         MOV    R6,A
         MOV    R7,#8
RTESE01: CALL   RTESEX
         MOV    R2,#0
         MOV    R3,#20
         CALL   DMSEC
         CALL   RTESEX
         DJNZ   R7,RTESE01
         MOV    R7,#8
         MOV    R0,#DISBUF
         DJNZ   R5,RTESE00
         CALL   RTESED

         MOV    R7,#7
         MOV    DPTR,#RTEBUF1
         MOV    R5,#8
         MOV    R0,#DISBUF
         MOV    R6,#1
RTESE1:  MOV    A,R6
         MOV    @R0,A             ;SHILF ACC
         RL     A
         MOV    R6,A
         LCALL  RTESEX
         MOV    R2,#0
         MOV    R3,#20
         CALL   DMSEC
         DJNZ   R7,RTESE1
         CLR    A
         MOVC   A,@A+DPTR
         MOV    @R0,A
         INC    DPTR
         INC    R0
         MOV    R7,#7
         DJNZ   R5,RTESE1-2
         LCALL  RTESEX
         MOV    R2,#0
         MOV    R3,#20
         CALL   DMSEC
         MOV    R5,#60
RTESE10: LCALL  RTESEX
         MOV    R2,#0
         MOV    R3,#20
         CALL   DMSEC
         LCALL  RTESEX
         DJNZ   R5,RTESE10

         MOV    R7,#8
         MOV    R5,#8
         MOV    DPTR,#RTEBUF2
         MOV    R0,#DISBUF
         MOV    R6,#1
RTESE2:  MOV    A,R6
         MOV    @R0,A             ;SHILF ACC
         RL     A
         MOV    R6,A
         LCALL  RTESEX
         MOV    R2,#0
         MOV    R3,#20
         CALL   DMSEC
         DJNZ   R7,RTESE2
         CLR    A
         MOVC   A,@A+DPTR
         MOV    @R0,A
         INC    DPTR
         INC    R0
         MOV    R7,#8
         DJNZ   R5,RTESE2-2
         MOV    R5,#70
RTESE20: LCALL  RTESEX
         MOV    R2,#0
         MOV    R3,#20
         CALL   DMSEC
         LCALL  RTESEX
         DJNZ   R5,RTESE20
         SJMP   RTESE3
         LJMP   MAIN1
         CJNE   A,#8H,$-3
         LJMP   TESE
RTESE3:  MOV    P1,#0FFH
         MOV    A,P1
         ANL    A,#0FH
         CJNE   A,#00H,RTESE3-6
         LJMP   RTESE0

; ******* CLEAR DISBUF *******
; IN  =  DISBUF
; REG =  R0,R1

RTESED:  MOV    R1,#8
         MOV    R0,#DISBUF
         MOV    @R0,#00H
         INC    R0
         DJNZ   R1,$-3
         RET

; ******* SCAN SEGMENT *******
; IN   = DISBUF
; REG  = A,B,R1,R2,R3,R4

RTESEX:  MOV    R1,#DISBUF        ;SCAN DISPLAY
         MOV    B,#00H
RTESEY:  MOV    A,B
         MOV    P0,@R1
         MOV    P2,A
         ADD    A,#10H
         MOV    B,A
         INC    R1
         MOV    R2,#2
RTESEZ:  MOV    R3,#64H
         DJNZ   R3,$
         DJNZ   R2,RTESEZ
         CJNE   A,#80H,RTESEY
         MOV    P2,#80H
         RET

RTEBUF1: DB     0FFH,0FFH,0FFH,0FFH,0FFH,0FFH,0FFH,0FFH
RTEBUF2: DB     0B9H,0B9H,0EDH,0B0H,0B8H,0F7H,8FH,8FH  ;SILA
; ********** END TEST RS485 ************

; $$$$$$$$$$ START LIBRARY $$$$$$$$$$$$$
; ******* SHOW MONITOR *******
SHOW:    CALL   SLF
         MOV    DPTR,#SHO1W
         CALL   RSLCD
         MOV    DPTR,#SHO2W
         CALL   RSLCD
         MOV    DPTR,#SHO3W
         CALL   RSLCD
         MOV    DPTR,#SHO4W
         CALL   RSLCD
         MOV    R2,#15
         CALL   DTSEC
         RET

; ********** SHOW ************
; XXXYZ =  XXX = SHOW
;          Y   = LINE (1-4)
;          Z   = STATE SHOW (W=SHOW MONITOR ,F=FIX ,T_=TEST ,R=RUN
;                            K=WORKING ,E=ERROR ,B=BACKUP)

SHO1W:   DB     ' SILA RESEARCH  '     ;SHOW MONITOR
SHO2W:   DB     'Sefttest V-C51M '     ;SHOW MONITOR
SHO3W:   DB     '  SILA -> 1997  '     ;SHOW MONITOR
SHO4W:   DB     'By Mr.Apichit B.'     ;SHOW MONITOR
SHO1F:   DB     ' BOARD  V-C51M  '     ;FIX SHOW TEST
SHO3F:   DB     'Run  Sw> 4=->Run'     ;WAIT TEST DIP 4
SHO4F:   DB     'Seft-Test  Board'     ;FIX SHOW
SHO34R:  DB     'Wait !!! Testing'     ;TEST BOARD

SHO2T1:  DB     'TEST> LED,SOUND '     ;TEST LED,SOUND
SHO2T2:  DB     'TEST> PORT USER '     ;TEST PORT USER
SHO2T3:  DB     'TEST> KEY 3 x 4 '     ;TEST KEY 3 x 4
SHO2T4:  DB     'TEST> RELAY     '     ;TEST RELAY
SHO2T5:  DB     'TEST>DS1202,2223'     ;TEST DS1202(RTC) TEST DS2223
SHO2T6:  DB     'TEST>RS232,RS485'     ;TEST RS232(DS275) AND RS485(75176)
SHO2T7:  DB     'TEST> BACKUP,WD '     ;TEST BACKUP TEST WD
SHO2T8:  DB     'TEST>  SEGMENT  '     ;TEST SEGMENT

SHO3BE:  DB     'BAT BACKUP FAIL!'     ;BATTERY FAIL
SHO3B7:  DB     'BAT BACKUP<GOOD>'     ;BATTERY GOOD
SHO34K:  DB     '*** Working! ***'     ;END TEST SHOW WORK
SHO34E:  DB     '### ERROR!!! ###'     ;END TEST ERROR
SHO3TE:  DB     'DS1202 > Fail??!'     ;END TEST DS1202 FAIL
SHO4TE:  DB     'DS2223 > Fail??!'     ;END TEST DS2223 FAIL
SHO3TK:  DB     'DS1202 > Good!?!'     ;END TEST DS1202 GOOD
SHO4TT:  DB     'DS2223>>Working!'     ;WAIT TEST DS2223
SHO4TK:  DB     'DS2223 > Good!?!'     ;END TEST DS2223 GOOD
SHO3T1:  DB     'I do not know !!'     ;DON'T KNOW TEST
SHO4T1:  DB     'Do you know test'     ;YOU KNOW TEST
SHO3T3:  DB     ' Plase Push Key '     ;TEST KEY
SHO4T3:  DB     '                '     ;SHOW PUSH KEY
SHO4T7:  DB     'OFFPWR>DIP>ONPWR'     ;OFFPOWER->DIP=4->ONPOWER

; *** RSLCD MTOOL7 1 LINE ****
; SERIAL SHOW LCD TO MTOOL7 OR PC
; IN  = DPTR
; REG = A,R0,DPTR

RSLCD:   MOV    R0,#16
RSLCD1:  CLR    A
         MOVC   A,@A+DPTR
         CALL   SBYTE
         INC    DPTR
         DJNZ   R0,RSLCD1
         RET

RSLCD2:  MOV    R0,#16
RSLCD3:  MOV    A,@R1
         CALL   SBYTE
         INC    R1
         DJNZ   R0,RSLCD3
         RET

; *** RTCLCD MTOOL7 1 LINE ***
; SERIAL SHOW TIME RTC TO MTOOL7
; IN  = R1
; REG = A,R0,R1,R2,R3

RTCLCD:  MOV    R0,#7
         MOV    A,#'<'
         CALL   SBYTE
RTCLCD1: CLR    A
         MOV    A,@R1
         MOV    R4,A
         ANL    A,#0F0H
         SWAP   A
         ADD    A,#30H
         CALL   SBYTE
         MOV    A,R4
         ANL    A,#0FH
         ADD    A,#30H
         CALL   SBYTE
         INC    R1
         DJNZ   R0,RTCLCD1
         MOV    A,#'>'
         CALL   SBYTE
         RET

; ******** SOUND SUB *********
; SOUND GENERATOR
; IN  = R2 FREQUENCY
;     = R3 LENGTH
; REG = A,R2,R3,R4,R5

SOUND:   MOV    R5,#0             ;END FLAG
         MOV    R4,#80H           ;DELAY CONSTANT
SOUND1:  CALL   SOUNDS
         CJNE   R5,#1,SOUND1
         RET

SOUNDS:  SETB   SOUNDB            ;OUT 1
         CALL   SOUNDX
         CLR    SOUNDB            ;OUT 0
         CALL   SOUNDX
         RET

SOUNDX:  MOV    A,R2              ;FREQUENCY DELAY
SOUNDX1: CALL   SOUNDY
         DEC    A
         JNZ    SOUNDX1
         RET

SOUNDY:  DJNZ   R4,SOUNDY1        ;LENGTH COUNT DOWN
         MOV    R4,#80H
         DJNZ   R3,SOUNDY1
         MOV    R5,#1
SOUNDY1: RET

; ******** xBEEP SUB *********
; ANY BEEP SUBROUTINE
; REG = A,R2,R3,R4,R5

KBEEP:   MOV    R2,#50H           ;KEY BEEP
         MOV    R3,#10H
         CALL   SOUND
         RET

FBEEP:   MOV    R2,#30H           ;FUNCTION KEY BEEP
         MOV    R3,#10H
         CALL   SOUND
         RET

HBEEP:   MOV    R2,#10H           ;SUCCESS BEEP
         MOV    R3,#40H
         CALL   SOUND
         RET

LBEEP:   MOV    R2,#90H           ;WARNING BEEP
         MOV    R3,#90H
         CALL   SOUND
         RET

EBEEP:   MOV    R2,#90H           ;ERROR BEEP
         MOV    R3,#00H
         CALL   SOUND
         MOV    R2,#0C0H
         MOV    R3,#00H
         CALL   SOUND
         RET

RBEEP:   MOV    R2,#4             ;READY BEEP
RBEEP1:  MOV    A,R2
         PUSH   ACC               ;-[
         MOV    R2,#70H
         MOV    R3,#15H
         CALL   SOUND
         MOV    R2,#00H
         MOV    R3,#18H
         CALL   DMSEC
         POP    ACC               ;-]
         MOV    R2,A
         DJNZ   R2,RBEEP1
         RET

PBEEP:   MOV    R6,#30H           ;POWER-UP BEEP
         MOV    R7,#38H
PBEEP2:  MOV    A,R7
         MOV    R2,A
         MOV    R3,#02H
         CALL   SOUND
         MOV    R2,#00H
         MOV    R3,#08H
         CALL   DMSEC
         DEC    R7
         DJNZ   R6,PBEEP2
         RET

; ******** ERAMWR SUB ********
; DS2223 RAM - WRITE SUBROUTINE
; IN  = RAMBUF (32 BYTE)
; REG = A,R0,R1,R2,R3

ERAMWR:  CALL   WONE              ;COMMAND WORD (FIRST BIT = 1)
         CALL   WZERO             ;PHYSICAL ADDRESS BIT 0 = 0
         CALL   WZERO             ;PHYSICAL ADDRESS BIT 1 = 0
         CALL   WONE              ;WRITE
         CALL   WONE
         CALL   WONE
         CALL   WONE
         CALL   WONE

         MOV    R0,#RAMBUF
         MOV    R2,#32            ;32 BYTE
ERAMWR1: MOV    R3,#8             ;8 BIT
         MOV    A,@R0
ERAMWR2: RRC    A
         JC     ERAMWR3
         CALL   WZERO
         SJMP   ERAMWR4
ERAMWR3: CALL   WONE
ERAMWR4: DJNZ   R3,ERAMWR2
         INC    R0
         DJNZ   R2,ERAMWR1
         RET

WONE:    NOP                      ;Tsyc = 2 uS
         CLR    ERAMDA
         MOV    R1,#4             ;Tlow1 = 10 uS
         DJNZ   R1,$
         SETB   ERAMDA
         MOV    R1,#30            ;MAKE Tslot = 80 uS
         DJNZ   R1,$
         NOP
         RET

WZERO:   NOP                      ;Tsyc = 2 uS
         CLR    ERAMDA
         MOV    R1,#36            ;Tlow0 = Tslot = 80 uS
         DJNZ   R1,$
         SETB   ERAMDA
         RET

; ******* ERAMRD SUB *********
; DS2223 RAM - READ SUBROUTINE
; OUT = RAMBUF (32 BYTE)
; REG = A,R0,R1,R2,R3

ERAMRD:  CALL   WONE              ;COMMAND WORD (FIRST BIT = 1)
         CALL   WZERO             ;PHYSICAL ADDRESS BIT 0 = 0
         CALL   WZERO             ;PHYSICAL ADDRESS BIT 1 = 0
         CALL   WZERO             ;READ
         CALL   WZERO
         CALL   WZERO
         CALL   WZERO
         CALL   WZERO

         MOV    R0,#RAMBUF
         MOV    R2,#32            ;32 BYTE
ERAMRD1: MOV    R3,#8             ;8 BIT
ERAMRD2: CALL   RBIT
         RRC    A
         DJNZ   R3,ERAMRD2
         MOV    @R0,A
         INC    R0
         DJNZ   R2,ERAMRD1
         RET

RBIT:    NOP                      ;Tsyc = 2 uS
         CLR    ERAMDA
         MOV    R1,#3             ;Tsu = 8 uS
         DJNZ   R1,$
         SETB   ERAMDA
         MOV    R1,#5             ;Tdvr = 14 uS
         DJNZ   R1,$
         NOP
         MOV    C,ERAMDA          ;GET DATA FROM EconoRAM TO CY
         MOV    R1,#28            ;TAKE Tslot = 80 uS
         DJNZ   R1,$
         NOP
         SETB   ERAMDA
         RET

; ******** ERAMST SUB ********
; DS2223 RAM - START UP
; REG = R1,R2

ERAMST:  MOV    R2,#200           ;RESET DS2223 (264 WRITE ZERO)
ERAMST1: CALL   WZERO
         DJNZ   R2,ERAMST1
         MOV    R2,#64
ERAMST2: CALL   WZERO
         DJNZ   R2,ERAMST2
         RET

; ********* SLF SUB **********
; PRINT CR = CARRIAGE RETURN /LF = LINE FEED
; REG = A

SLF:     MOV    A,#0DH
         CALL   SBYTE
         MOV    A,#0AH
         CALL   SBYTE
         RET

; ********* RBYTE SUB ********
; READ ONE BYTE
; OUT = A
; REG = A

RBYTE:   JNB    RI,$              ;WAIT FOR RECEIVE OK
         CLR    RI
         MOV    A,SBUF
         RET

; ******** SBYTE SUB *********
; SEND BYTE
; IN  = A
; REG = NO

SBYTE:   JNB    TI,$              ;WAIT FOR SEND OK
         CLR    TI
         MOV    SBUF,A
         RET

; ******** SBYTEH SUB ********
; SEND 2 BYTE HEX FROM A
; IN  = A
; REG = A,R2,R3

SBYTEH:  CALL   HTOA
         MOV    A,R2
         CALL   SBYTE
         MOV    A,R3
         CALL   SBYTE
         RET

; ******** HTOA SUB **********
; CONVERT HEX TO ASCII
; IN  = A
; OUT = R2,R3
; REG = A,R2,R3

HTOA:    PUSH   ACC
         SWAP   A
         CALL   HTOAS
         MOV    R2,A
         POP    ACC
         CALL   HTOAS
         MOV    R3,A
         RET

HTOAS:   ANL    A,#0F0H
         CJNE   A,#0AH,$+3
         JNC    HTOAS1
         ORL    A,#30H
         RET
HTOAS1:  SUBB   A,#9
         ORL    A,#40H
         RET

; ******** RTCRD SUB *********
; READ SINGLE BYTE FROM DS1202
; IN  = R2 COMMAND
; OUT = R3 DATA
; REG = A,R2,R3,R4,R5

RTCRD:   CLR    RTCCLK            ;CLK=0
         CALL   RTCDL
         SETB   RTCRST            ;RST=1
         CALL   RTCDL
         MOV    A,R2              ;COMMAND
         CALL   RTCWRC

         MOV    R4,#8             ;READ DATA
         CLR    A
RTCRD1:  CLR    RTCCLK
         CALL   RTCDL
         MOV    C,RTCDAT
         RRC    A
         SETB   RTCCLK
         CALL   RTCDL
         DJNZ   R4,RTCRD1
         MOV    R3,A

         CLR    RTCRST            ;RST=0
         CALL   RTCDL
         RET

; ******** RTCWR SUB *********
; WRITE SINGLE BYTE TO DS1202
; IN  = R2 COMMAND
;       R3 DATA
; REG = A,R2,R3,R4,R5

RTCWR:   CLR    RTCCLK            ;CLK=0
         CALL   RTCDL
         SETB   RTCRST            ;RST=1
         CALL   RTCDL
         MOV    A,R2              ;COMMAND
         CALL   RTCWRC
         MOV    A,R3              ;DATA
         CALL   RTCWRC
         CLR    RTCRST            ;RST=0
         CALL   RTCDL
         RET

RTCWRC:  MOV    R4,#8             ;WRITE COMMAND/DATA
RTCWRC1: RRC    A
         MOV    RTCDAT,C
         SETB   RTCCLK            ;RISING EDGE CLOCK
         CALL   RTCDL
         CLR    RTCCLK
         CALL   RTCDL
         DJNZ   R4,RTCWRC1
         RET

RTCDL:   MOV    R5,#4             ;DELAY
         DJNZ   R5,$
         RET

; ******** TIMERD SUB ********
; READ RTC -> TIMBUF
; OUT = TIMBUF
; REG = A,R2,R3,R4,R5

TIMERD:  MOV    R2,#87H           ;DATE
         CALL   RTCRD
         MOV    TIMBUF,R3
         MOV    R2,#89H           ;MONTH
         CALL   RTCRD
         MOV    TIMBUF+1,R3
         MOV    R2,#8DH           ;YEAR
         CALL   RTCRD
         MOV    TIMBUF+2,R3
         MOV    R2,#8BH           ;WEEK-DAY
         CALL   RTCRD
         MOV    TIMBUF+6,R3
         MOV    R2,#85H           ;HOUR
         CALL   RTCRD
         MOV    TIMBUF+3,R3
         MOV    R2,#83H           ;MINUTE
         CALL   RTCRD
         MOV    TIMBUF+4,R3
         MOV    R2,#81H           ;SECOND
         CALL   RTCRD
         MOV    TIMBUF+5,R3
         RET

; ******** TIMEWR SUB ********
; WRITE TIMBUF -> RTC
; IN  = TIMBUF
; REG = A,R2,R3,R4,R5

TIMEWR:  MOV    R2,#8EH           ;WRITE PROTECT = 0
         MOV    R3,#0
         CALL   RTCWR
         MOV    R2,#86H           ;DATE
         MOV    R3,TIMBUF
         CALL   RTCWR
         MOV    R2,#88H           ;MONTH
         MOV    R3,TIMBUF+1
         CALL   RTCWR
         MOV    R2,#8CH           ;YEAR
         MOV    R3,TIMBUF+2
         CALL   RTCWR
         MOV    R2,#8AH           ;WEEK-DAY
         MOV    R3,TIMBUF+6
         CALL   RTCWR
         MOV    R2,#84H           ;HOUR
         MOV    R3,TIMBUF+3
         CALL   RTCWR
         MOV    R2,#82H           ;MINUTE
         MOV    R3,TIMBUF+4
         CALL   RTCWR
         MOV    R2,#80H           ;SECOND
         MOV    R3,TIMBUF+5
         CALL   RTCWR
         MOV    R2,#8EH           ;WRITE PROTECT = 1
         MOV    R3,#80H
         CALL   RTCWR
         RET

; ********* WDS SUB **********
; WATCH DOG TRIG
; REG = NO

WDS:     CPL    WTDBIT
         RET

; ********* DTSEC SUB ********
; DELAY 1/10 SECOND
; IN  = R2
; REG = R2,R3,R4

DTSEC:   MOV    R3,#179
DTSEC1:  MOV    R4,#0
         DJNZ   R4,$
         DJNZ   R3,DTSEC1
         DJNZ   R2,DTSEC
         RET

; ********* DSEC SUB *********
; DELAY SECOND
; IN  = R1
; REG = R1,R2,R3,R4

DSEC:    MOV    R2,#10
         CALL   DTSEC
         DJNZ   R1,DSEC
         RET

; ******** DMSEC SUB *********
; DELAY 1/1000 SECOND
; IN  = R2,R3
; REG = A,R2,R3,R4

DMSEC:   MOV    R4,#230         ;1 MSEC LOOP
DMSEC1:  NOP
         NOP
         DJNZ   R4,DMSEC1
         DJNZ   R3,DMSEC
         MOV    A,R2
         CJNE   A,#0,DMSEC2
         RET
DMSEC2:  DEC    R2
         SJMP   DMSEC

; ******* SON,SOFF SUB *******
SON:     SETB   COMDRV
         RET

SOFF:    JNB    TI,$
         NOP
         NOP
         CLR    COMDRV
         RET

; $$$$$$$$$$$$ END LIBRARY $$$$$$$$$$$$$

         END
