; FILENAME      ANTC51M.ASM
; DESCRIPTION   ANT-C51M QC PROGRAM
; HARDWARE      ANT-C51M BOARD
; ASSEMBLER     SXA51
; START-DATE    18/09/39
; SOFTWARE ENG. KRIANGSAK B.
; COMPANY       SILA RESEARCH CO.,LTD.

; ********** VARIABLE SET **********

CONA     EQU    0F800H       ;OUT 0-7=SEGMENT
CONB     EQU    0F801H       ;OUT 0-2=DIGIT 3=SPK 4-7=LED
CONC     EQU    0F802H       ;IN  0-3=KEYIN 4-7=DIPSW
CONP     EQU    0F803H       ;CONTROL PORT

LCDWRC   EQU    0FA00H       ;LCD WRITE CONTROL
LCDRDC   EQU    0FA01H       ;LCD READ CONTROL (BUSY CHECK)
LCDWRD   EQU    0FA02H       ;LCD WRITE DATA
LCDRDD   EQU    0FA03H       ;LCD READ DATA

USRA     EQU    0FC00H       ;USER PORT
USRB     EQU    0FC01H
USRC     EQU    0FC02H
USRP     EQU    0FC03H

ERAMDA   EQU    P1.0         ;DS2223 DATA
DTMDAT   EQU    P1.1         ;DS1620 TEMP.
DTMCLK   EQU    P1.2
DTMRST   EQU    P1.3
RTCDAT   EQU    P1.4         ;DS1202 RTC
RTCCLK   EQU    P1.5
RTCRST   EQU    P1.6
WTDBIT   EQU    P1.7         ;WATCH DOG TRIG
OPTINP   EQU    INT0         ;OPTO INPUT
RELOUT   EQU    INT1         ;RELAY OUTPUT
COMDRV   EQU    T0           ;RS485 DRIVE
BATOKB   EQU    T1           ;BATTERY (CHECK LOW BATT)

BRAT12   EQU    0E8H         ;1200 BAUD RATE VAR.
BRAT24   EQU    0F4H         ;2400
BRAT48   EQU    0FAH         ;4800
BRAT96   EQU    0FDH         ;9600
SMOD     EQU    B.7          ;FOR BAUD FUNCTION

; ********** INTERNAL RAM **********

         ORG    0000H
                             ;**** USER AREA ****
         DS     8            ;USER AREA (REGISTER BANK-0)
SYSSTK:  DS     24           ;SYSTEM STACK

SYSFAG:  DS     1            ;**** BIT ADDRESSABLE ****
SCANPF   EQU    00H          ;SCAN - PRESS FLAG
SCANOL   EQU    01H          ;SCAN - ONE LOOP FLAG

LCDBUF:  DS     40           ;LCD BUFFER
RAMBUF:  DS     32           ;DS2223 RAM BUFFER
DISBUF:  DS     8            ;DISPLAY BUFFER
ADDIXX:  DS     2            ;ADDRESS IX BUFFER
ADDIYY:  DS     2            ;ADDRESS IY BUFFER
TIMBUF:  DS     7            ;DD MM YY HH MM SS WW
ENDINT:

; ********** RESET ************
; SYSTEM RESET

         ORG    0000H

RES:     MOV    R2,#80H           ;POWER UP DELAY
RES1:    MOV    R3,#0
         DJNZ   R3,$
         DJNZ   R2,RES1
         MOV    SP,#SYSSTK

         MOV    A,#89H            ;SET CONTROL (CONSOLE PORT)
         MOV    DPTR,#CONP
         MOVX   @DPTR,A
         CLR    A                 ;CLEAR SEGMENT
         MOV    DPTR,#CONA
         MOVX   @DPTR,A
         MOV    A,#80H            ;SET CONTROL (USER PORT)
         MOV    DPTR,#USRP
         MOVX   @DPTR,A

         CLR    RTCCLK
         CLR    RTCRST
         MOV    R2,#0CH           ;WRITE CONFIG DS1620
         MOV    R3,#00000011B
         CALL   DTWR8
         CALL   ERAMST            ;START UP DS2223

         MOV    TMOD,#20H         ;TIMER1 MODE2
         MOV    SCON,#52H         ;SERIAL 8 BIT UART MODE
         MOV    TH1,#BRAT96       ;9600 BPS
         SETB   TR1               ;TIMER1 ON
         CALL   SOFF              ;RS485 OFF

         MOV    R0,#08H           ;CLEAR INT-RAM (ALL)
         MOV    R2,#ENDINT-08H
RES4:    MOV    @R0,#0
         INC    R0
         DJNZ   R2,RES4

         CALL   LCDSET
         MOV    DPTR,#RESTAB
         CALL   LCDLDX
         CALL   LCDOUT
         CALL   HBEEP
         MOV    R2,#15
         CALL   DTSEC
         SJMP   MAIN

RESTAB:  DB     '   ANT-C51M Board   '
         DB     ' Self-test  Program '

; ********** MAIN **********

MAIN:    MOV    DPTR,#MAINTX      ;FIRST LINE
         CALL   LCDLD1

MAIN2:   MOV    DPTR,#CONC        ;CHECK DIP-SW 1,2,3
         MOVX   A,@DPTR
         CPL    A
         ANL    A,#01110000B
MAIN31:  CJNE   A,#00000000B,MAIN32
         MOV    DPTR,#MAINT1
         SJMP   MAIN4
MAIN32:  CJNE   A,#00010000B,MAIN33
         MOV    DPTR,#MAINT2
         SJMP   MAIN4
MAIN33:  CJNE   A,#00100000B,MAIN34
         MOV    DPTR,#MAINT3
         SJMP   MAIN4
MAIN34:  CJNE   A,#00110000B,MAIN35
         MOV    DPTR,#MAINT4
         SJMP   MAIN4
MAIN35:  CJNE   A,#01000000B,MAIN36
         MOV    DPTR,#MAINT5
         SJMP   MAIN4
MAIN36:  CJNE   A,#01010000B,MAIN37
         MOV    DPTR,#MAINT6
         SJMP   MAIN4
MAIN37:  CJNE   A,#01100000B,MAIN38
         MOV    DPTR,#MAINT7
         SJMP   MAIN4
MAIN38:  MOV    DPTR,#MAINT8

MAIN4:   CALL   LCDLD2            ;LOAD TO LINE-2
         CALL   LCDOUT
         MOV    DPTR,#CONC        ;CHECK DIP-SW 4
         MOVX   A,@DPTR
         CPL    A
         JB     ACC.7,MAING
         SJMP   MAIN2

MAING:   PUSH   ACC               ;WORKING
         MOV    DPTR,#MAINTY
         CALL   LCDLD1
         CALL   LCDOUT
         MOV    R2,#5
         CALL   DTSEC
         POP    ACC

         ANL    A,#01110000B
         CJNE   A,#00000000B,$+6
         LJMP   ST1
         CJNE   A,#00010000B,$+6
         LJMP   ST2
         CJNE   A,#00100000B,$+6
         LJMP   ST3
         CJNE   A,#00110000B,$+6
         LJMP   ST4
         CJNE   A,#01000000B,$+6
         LJMP   ST5
         CJNE   A,#01010000B,$+6
         LJMP   ST6
         CJNE   A,#01100000B,$+6
         LJMP   ST7
         LJMP   ST8

MAINT1:  DB     'TEST> LCD,LED,SOUND '
MAINT2:  DB     'TEST> 8255,DISPLAY  '
MAINT3:  DB     'TEST> KEY 3x4,4x8   '
MAINT4:  DB     'TEST> OPTO,RELAY <->'
MAINT5:  DB     'TEST> DS1202,DS1620 '
MAINT6:  DB     'TEST> RAM,2223 ---- '
MAINT7:  DB     'TEST> RS232,485 <-->'
MAINT8:  DB     'TEST> BACKUP,LB,WD  '
MAINTX:  DB     'DIPSW 1-3=Mode 4=Run'
MAINTY:  DB     '****  Working  **** '

; ********** ST1 **********
; TEST LCD,LED,SOUND

ST1:     MOV    R7,#8             ;COUNTER
         MOV    R6,#'1'           ;START CHAR.
         MOV    B,#00010001B      ;LED BIT

ST12:    CALL   ST1SUB            ;LOAD & OUT TO LCD
         CALL   LCDOUT
         MOV    A,B               ;LED OUT
         ANL    A,#11110000B
         MOV    DPTR,#CONB
         MOVX   @DPTR,A
         MOV    A,B
         RL     A
         MOV    B,A

         CALL   FBEEP             ;BEEP & DELAY
         MOV    R2,#5
         CALL   DTSEC
         INC    R6
         DJNZ   R7,ST12

         MOV    R6,#0FFH          ;LCD DARK
         CALL   ST1SUB
         CALL   LCDOUT
         MOV    A,#11110000B      ;LED BRIGHT
         MOV    DPTR,#CONB
         MOVX   @DPTR,A
         MOV    R2,#20
         CALL   DTSEC

         MOV    A,#00000000B      ;LED DARK
         MOV    DPTR,#CONB
         MOVX   @DPTR,A
         LJMP   MAIN

ST1SUB:  MOV    R2,#40            ;**** LOAD SUB ****
         MOV    R0,#LCDBUF
         MOV    A,R6
ST1SUB1: MOV    @R0,A             ;LOAD CHAR. TO BUFFER
         INC    R0
         DJNZ   R2,ST1SUB1
         RET

; ********** ST2 **********
; TEST 8255,DISPLAY

ST2:     MOV    DPTR,#USRP
         MOV    A,#80H
         MOVX   @DPTR,A
         CALL   ST2SUB
         LJMP   MAIN

ST2SUB:  MOV    ADDIYY,#2         ;**** TEST 8255 SUB ****
ST2SUB1: MOV    ADDIYY+1,#18
         MOV    DPTR,#TB8255
         MOV    ADDIXX,DPH
         MOV    ADDIXX+1,DPL

ST2SUB2: MOV    DPH,ADDIXX
         MOV    DPL,ADDIXX+1
         CLR    A
         MOVC   A,@A+DPTR
         INC    DPTR
         MOV    ADDIXX,DPH
         MOV    ADDIXX+1,DPL

         MOV    R4,#3             ;OUT TO 8255
         MOV    DPTR,#USRA
ST2SUB4: MOVX   @DPTR,A
         INC    DPTR
         DJNZ   R4,ST2SUB4

         MOV    R2,#8             ;LOAD TO DISBUF
         MOV    R0,#DISBUF
ST2SUB5: MOV    @R0,A
         INC    R0
         DJNZ   R2,ST2SUB5

         MOV    R2,#20H
         CALL   SCAND
         DJNZ   ADDIYY+1,ST2SUB2
         DJNZ   ADDIYY,ST2SUB1
         RET

TB8255:  DB     00000001B
         DB     00000010B
         DB     00000100B
         DB     00001000B
         DB     00010000B
         DB     00100000B
         DB     01000000B
         DB     10000000B
         DB     00000000B
         DB     11111111B
         DB     00000000B
         DB     11111111B
         DB     00000000B
         DB     10101010B
         DB     01010101B
         DB     10101010B
         DB     01010101B
         DB     00000000B

; ********** ST3 **********
; TEST KEY 3x4,4x8

ST3:     CALL   CLEAR
ST31:    SETB   SCANOL
         CALL   SCAN
         CLR    SCANOL
         CJNE   A,#0FFH,ST32
         SJMP   ST35
ST32:    CALL   HTOA
         MOV    LCDBUF+38,R2
         MOV    LCDBUF+39,R3
         CALL   LCDOUT

ST35:    MOV    DPTR,#CONC        ;CHECK DIP-SW
         MOVX   A,@DPTR
         CPL    A
         JB     ACC.7,ST31
         LJMP   MAIN

; ********** ST4 **********
; TEST OPTO,RELAY

ST4:     JB     OPTINP,ST42
         MOV    LCDBUF+38,#'*'
         CLR    RELOUT
         SJMP   ST44

ST42:    MOV    LCDBUF+38,#' '
         SETB   RELOUT

ST44:    CALL   LCDOUT
         MOV    DPTR,#CONC        ;CHECK DIP-SW
         MOVX   A,@DPTR
         CPL    A
         JB     ACC.7,ST4
         LJMP   MAIN

; ********** ST5 **********
; TEST DS1202,DS1620

ST5:     MOV    TIMBUF,#31H       ;DDMMYY
         MOV    TIMBUF+1,#12H
         MOV    TIMBUF+2,#96H
         MOV    TIMBUF+3,#23H     ;HHMMSS
         MOV    TIMBUF+4,#59H
         MOV    TIMBUF+5,#55H
         MOV    TIMBUF+6,#01H     ;WEEK DAYS
         CALL   TIMEWR

ST54:    MOV    R2,#0EEH          ;START CONVERT FOR DS1620
         CALL   DTWR

         CALL   TIMERD            ;READ TIME
         MOV    A,TIMBUF          ;DISPLAY DDMMYY
         CALL   HTOA
         MOV    LCDBUF+20,R2
         MOV    LCDBUF+21,R3
         MOV    LCDBUF+22,#'/'
         MOV    A,TIMBUF+1
         CALL   HTOA
         MOV    LCDBUF+23,R2
         MOV    LCDBUF+24,R3
         MOV    LCDBUF+25,#'/'
         MOV    A,TIMBUF+2
         CALL   HTOA
         MOV    LCDBUF+26,R2
         MOV    LCDBUF+27,R3
         MOV    LCDBUF+28,#' '

         MOV    A,TIMBUF+3        ;DISPLAY HHMMSS
         CALL   HTOA
         MOV    LCDBUF+29,R2
         MOV    LCDBUF+30,R3
         MOV    LCDBUF+31,#':'
         MOV    A,TIMBUF+4
         CALL   HTOA
         MOV    LCDBUF+32,R2
         MOV    LCDBUF+33,R3
         MOV    LCDBUF+34,#':'
         MOV    A,TIMBUF+5
         CALL   HTOA
         MOV    LCDBUF+35,R2
         MOV    LCDBUF+36,R3
         MOV    LCDBUF+37,#' '

         MOV    R2,#0AAH          ;READ TEMP
         CALL   DTRD16
         CALL   DTCON
         MOV    A,R3
         CALL   HTOA
         MOV    LCDBUF+38,R2
         MOV    LCDBUF+39,R3

         CALL   LCDOUT
         MOV    DPTR,#CONC        ;CHECK DIP-SW
         MOVX   A,@DPTR
         CPL    A
         JB     ACC.7,ST54
         LJMP   MAIN

; ********** ST6 **********
; TEST RAM,DS2223

ST6:     MOV    ADDIXX,#80H
         MOV    ADDIXX+1,#00H
         MOV    ADDIYY,#0F7H
         MOV    ADDIYY+1,#0FFH
         CALL   RAMT

         MOV    R2,#32            ;WRITE DS2223
         MOV    R0,#RAMBUF
         MOV    A,#'A'
ST62:    MOV    @R0,A
         INC    R0
         INC    A
         DJNZ   R2,ST62
         CALL   ERAMWR
         MOV    LCDBUF+35,#'W'
         MOV    LCDBUF+36,#'R'
         MOV    LCDBUF+37,#'I'
         MOV    LCDBUF+38,#'T'
         CALL   LCDOUT
         MOV    R2,#5
         CALL   DTSEC

         CALL   ERAMRD            ;READ & CHECK DS2223
         MOV    LCDBUF+35,#'C'
         MOV    LCDBUF+36,#'H'
         MOV    LCDBUF+37,#'C'
         MOV    LCDBUF+38,#'K'
         CALL   LCDOUT
         MOV    R2,#5
         CALL   DTSEC
         MOV    R2,#32
         MOV    R0,#RAMBUF
         MOV    B,#'A'
ST64:    MOV    A,@R0
         CJNE   A,B,ST68
         INC    R0
         INC    B
         DJNZ   R2,ST64

         MOV    LCDBUF+35,#'P'    ;PASS
         MOV    LCDBUF+36,#'A'
         MOV    LCDBUF+37,#'S'
         MOV    LCDBUF+38,#'S'
         CALL   LCDOUT
         MOV    R2,#5
         CALL   DTSEC
         SJMP   ST6X

ST68:    MOV    LCDBUF+35,#'E'    ;ERR!
         MOV    LCDBUF+36,#'R'
         MOV    LCDBUF+37,#'R'
         MOV    LCDBUF+38,#'!'
         CALL   LCDOUT
         MOV    R2,#5
         CALL   DTSEC
         CALL   LBEEP

ST6X:    MOV    DPTR,#CONC        ;CHECK DIP-SW
         MOVX   A,@DPTR
         CPL    A
         JB     ACC.7,ST6X1
         LJMP   MAIN
ST6X1:   LJMP   ST6

; ********** ST7 *********
; TEST RS232,RS485

ST7:     MOV    B,#'0'
         CALL   SON

ST71:    MOV    A,B               ;LOOP (TXD)
         CALL   SBYTE
         MOV    R2,#5
         CALL   DTSEC
         INC    B
         MOV    A,B
         CJNE   A,#'9'+1,ST72
         MOV    B,#'0'

ST72:    JB     RI,ST76           ;CHECK RXD
ST73:    MOV    DPTR,#CONC        ;CHECK DIP-SW
         MOVX   A,@DPTR
         CPL    A
         JB     ACC.7,ST71
         LJMP   MAIN

ST76:    CALL   RBYTE             ;RXD
         CALL   HTOA
         MOV    LCDBUF+37,R2
         MOV    LCDBUF+38,R3
         CALL   LCDOUT
         SJMP   ST73

; ********** ST8 **********

ST8:     CALL   HBEEP             ;START
         MOV    DPTR,#ST8TB1
         CALL   LCDLD1
         MOV    DPTR,#ST8TB2
         CALL   LCDLD2

         MOV    DPTR,#8000H       ;RAM CHECK
         MOVX   A,@DPTR
         CJNE   A,#55H,ST82
         INC    DPTR
         MOVX   A,@DPTR
         CJNE   A,#0AAH,ST82
         MOV    LCDBUF+4,#'O'     ;RAM OK
         MOV    LCDBUF+5,#'K'
         SJMP   ST84

ST82:    MOV    DPTR,#8000H       ;RAM WRITE
         MOV    A,#55H
         MOVX   @DPTR,A
         INC    DPTR
         MOV    A,#0AAH
         MOVX   @DPTR,A
         MOV    LCDBUF+4,#'W'
         MOV    LCDBUF+5,#'R'

ST84:    CALL   ERAMRD            ;DS2223 CHECK
         MOV    A,RAMBUF
         CJNE   A,#55H,ST85
         MOV    A,RAMBUF+1
         CJNE   A,#0AAH,ST85
         MOV    LCDBUF+12,#'O'    ;DS2223 OK
         MOV    LCDBUF+13,#'K'
         SJMP   ST86

ST85:    MOV    RAMBUF,#55H       ;DS2223 WRITE
         MOV    RAMBUF+1,#0AAH
         CALL   ERAMWR
         MOV    LCDBUF+12,#'W'
         MOV    LCDBUF+13,#'R'

ST86:    MOV    R2,#10
         CALL   DTSEC

ST87:    MOV    LCDBUF+19,#'H'    ;CHECK BATT
         JB     BATOKB,ST88
         MOV    LCDBUF+19,#'L'
ST88:    CALL   LCDOUT

ST9:     CPL    WTDBIT            ;TRIG WATCH DOG
         MOV    DPTR,#CONC        ;CHECK DIP-SW
         MOVX   A,@DPTR
         CPL    A
         JB     ACC.7,ST87

         MOV    DPTR,#ST8TB3      ;TEST WD
         CALL   LCDLD2
         CALL   LCDOUT
         MOV    R2,#2
         CALL   DTSEC
         CPL    WTDBIT
         SJMP   $                 ;WAIT FOR RESET

ST8TB1:  DB     'RAM=xx 2223=xx BAT=x'
ST8TB2:  DB     'Enable WD & DIP4 off'
ST8TB3:  DB     'Wait! Watchdog Reset'

; ********** DTSEC SUB **********
; DELAY 1/10 SECOND
; IN  = R2
; REG = R2,R3,R4

DTSEC:   MOV    R3,#179
DTSEC1:  MOV    R4,#0
         DJNZ   R4,$
         NOP
         NOP
         DJNZ   R3,DTSEC1
         DJNZ   R2,DTSEC
         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

; ********** LCDWI SUB **********
; LCD WRITE INSTRUCTION (RS=0)
; IN  = A
; REG = A

LCDWI:   PUSH   DPH
         PUSH   DPL
         MOV    DPTR,#LCDWRC
         MOVX   @DPTR,A
         MOV    DPTR,#LCDRDC
LCDWI1:  MOVX   A,@DPTR           ;WAIT FOR BF=0
         JB     ACC.7,LCDWI1
         POP    DPL
         POP    DPH
         RET

; ********** LCDWD SUB **********
; LCD WRITE DATA (RS=1)
; IN  = A
; REG = A

LCDWD:   PUSH   DPH
         PUSH   DPL
         MOV    DPTR,#LCDWRD
         MOVX   @DPTR,A
         MOV    DPTR,#LCDRDC
LCDWD1:  MOVX   A,@DPTR           ;WAIT FOR BF=0
         JB     ACC.7,LCDWD1
         POP    DPL
         POP    DPH
         RET

; ********** LCDSET SUB **********
; LCD SETUP COMMAND

LCDSET:  MOV    A,#00111000B      ;FUNCTION SET
         CALL   LCDWI
         MOV    A,#00001110B      ;DISPLAY ON/OFF
         CALL   LCDWI
         MOV    A,#01H            ;CLEAR
         CALL   LCDWI
         RET

; ********** LCDLDX SUB **********
; LOAD PMEM TO LCDBUF (ALL,LINE)
; IN  = DPTR
; REG = A,R0,R2,DPTR

LCDLDX:  MOV    R2,#40            ;LOAD ALL
         MOV    R0,#LCDBUF
LCDLDX1: CLR    A
         MOVC   A,@A+DPTR
         MOV    @R0,A
         INC    DPTR
         INC    R0
         DJNZ   R2,LCDLDX1
         RET

LCDLD1:  MOV    R2,#20            ;LOAD LINE-1
         MOV    R0,#LCDBUF
         SJMP   LCDLDX1

LCDLD2:  MOV    R2,#20            ;LOAD LINE-2
         MOV    R0,#LCDBUF+20
         SJMP   LCDLDX1

; ********** LCDOUT SUB **********
; LOAD LCDBUF TO LCD-MODULE (DMC202)
; IN  = LCDBUF
; REG = A,R0,R2

LCDOUT:  MOV    R0,#LCDBUF        ;LCD BUFFER
         MOV    A,#80H            ;SET ADDRESS LINE 1
         CALL   LCDOUTS
         MOV    A,#0C0H           ;SET ADDRESS LINE 2
         CALL   LCDOUTS
         RET

LCDOUTS: CALL   LCDWI             ;LOAD ONE LINE
         MOV    R2,#20            ;20 CHAR.
LCDOUTS1:MOV    A,@R0
         CALL   LCDWD             ;WRITE DATA
         INC    R0
         DJNZ   R2,LCDOUTS1
         RET

; ********** SOUND SUB **********
; SOUND GENERATOR
; IN  = R2 FREQUENCY
;     = R3 LENGTH
; REG = A,R2,R3,R4,R5,DPTR

SOUND:   MOV    R5,#0             ;END FLAG
         MOV    R4,#80H           ;DELAY CONSTANT
SOUND1:  CALL   SOUNDS
         CJNE   R5,#1,SOUND1
         RET

SOUNDS:  MOV    DPTR,#CONB        ;OUT 1
         MOVX   A,@DPTR
         SETB   ACC.3
         MOVX   @DPTR,A
         CALL   SOUNDX
         MOV    DPTR,#CONB        ;OUT 0
         MOVX   A,@DPTR
         CLR    ACC.3
         MOVX   @DPTR,A
         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,DPTR

KBEEP:   MOV    R2,#50H           ;KEY BEEP
         MOV    R3,#15H
         CALL   SOUND
         RET

FBEEP:   MOV    R2,#30H           ;FUNCTION KEY BEEP
         MOV    R3,#15H
         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

; ********** CLEAR SUB **********
; CLEAR DISBUF

CLEAR:   MOV    R2,#8
         MOV    R0,#DISBUF
CLEAR1:  MOV    @R0,#0
         INC    R0
         DJNZ   R2,CLEAR1
         RET

; ********** SCAN SUB **********
; SCAN DISPLAY AND WAIT FOR KEY AND KEY BEEP
; REFRESH , FLASH , BYPASS , ONELOOP - CHECK
; IN  = DISBUF
;     = SCANPF (KEY PRESS)
;     = SCANOL (ONE LOOP)
; OUT = A
; REG = A,R0,R1,R2,R3,R4,R5,R6,R7,DPTR

SCAN:    MOV    R4,#8             ;LOOP
         MOV    R5,#8             ;RELEASE KEY COUNT
         MOV    R6,#0             ;KEY-TABLE COUNT
         MOV    R7,#DISBUF

SCAN1:   CALL   SCANS             ;OUT SEGMENT
         MOV    DPTR,#CONC
         MOVX   A,@DPTR
         ANL    A,#0FH
         CJNE   A,#0FH,SCAN3
         DJNZ   R5,SCAN2
         CLR    SCANPF
SCAN2:   DJNZ   R4,SCAN1
         JNB    SCANOL,SCAN
         MOV    A,#0FFH
         RET

SCAN3:   SWAP   A                 ;PRESS
         ORL    A,R6
         JB     SCANPF,SCAN2      ;NOT NEW PRESS

SCAN5:   SETB   SCANPF            ;*** NEW PRESS ***
         MOV    R2,A              ;LOOKUP TABLE
         MOV    R3,#1FH
         MOV    DPTR,#KEYTAB
SCAN51:  CLR    A
         MOVC   A,@A+DPTR
         CLR    C
         SUBB   A,R2
         JZ     SCAN7
         INC    DPTR
         DJNZ   R3,SCAN51

SCAN7:   MOV    A,R3
         PUSH   ACC               ;BEEP GEN.
         CALL   KBEEP
         POP    ACC
         RET

; ********** SCANS SUB **********
; OUT ONE DIGIT & DELAY
; IN  = R6 DIGIT
;     = R7 INDEX TO DISBUF
; OUT = UPDATE R6 R7 + 1
; REG = A,R0,R2,R3,R6,R7,DPTR

SCANS:   MOV    DPTR,#CONB        ;SCANS SUB
         MOVX   A,@DPTR
         ANL    A,#11111000B
         ORL    A,R6
         MOVX   @DPTR,A           ;DIGIT
         MOV    A,R7
         MOV    R0,A
         MOV    A,@R0
         MOV    DPTR,#CONA
         MOVX   @DPTR,A           ;SEGMENT
         MOV    R2,#4             ;DELAY
SCANS2:  MOV    R3,#0
         DJNZ   R3,$
         DJNZ   R2,SCANS2
         CLR    A                 ;CLR SEGMENT
         MOVX   @DPTR,A
         INC    R6
         INC    R7
         RET

KEYTAB:  DB     0E8H,0E7H,0E6H,0E5H    ;1F,1E,1D,1C
         DB     0D8H,0D7H,0D6H,0D5H    ;1B,1A,19,18
         DB     0B8H,0B7H,0B6H,0B5H    ;17,16,15,14
         DB     078H,077H,076H,075H    ;13,12,11,10
         DB     0E4H,0E3H,0E2H,0E1H    ;F,E,D,C
         DB     0D4H,0D3H,0D2H,0D1H    ;B,A,9,8
         DB     0B4H,0B3H,0B2H,0B1H    ;7,6,5,4
         DB     074H,073H,072H,071H    ;3,2,1,0

; ********** SCAND SUB **********
; SCAN DISPLAY & DELAY
; IN  = DISBUF
;     = R2 DELAY VAR.
; REG = A,R0,R1,R2,R3,R4,R6,R7,DPTR

SCAND:   MOV    A,R2              ;DELAY
         MOV    R1,A
SCAND1:  MOV    R4,#8             ;LOOP
         MOV    R6,#0             ;DIGIT
         MOV    R7,#DISBUF
SCAND2:  CALL   SCANS
         DJNZ   R4,SCAND2
         DJNZ   R1,SCAND1
         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,#0FH
         CJNE   A,#0AH,$+3
         JNC    HTOAS1
         ORL    A,#30H
         RET
HTOAS1:  SUBB   A,#9
         ORL    A,#40H
         RET

; ********** ATOH SUB **********
; ASCII TO HEX CONVERT
; IN  = R2,R3   30H,41H
; OUT = A       0AH
; REG = A,R2

ATOH:    MOV    A,R2
         CALL   ATOHS
         SWAP   A
         MOV    R2,A
         MOV    A,R3
         CALL   ATOHS
         ORL    A,R2
         RET

ATOHS:   CJNE   A,#'A',$+3
         JC     ATOHS1
         ADD    A,#9
ATOHS1:  ANL    A,#0FH
         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

; ********** DTCON SUB **********
; CONVERT TO TEMPERATURE DATA
; IN  = R3,R4 (9 BIT DATA)
; OUT = R2,R3,A  (R2,R3 DECIMAL DATA) (A 0=0.0 1=0.5)
; REG = ALL

DTCON:   MOV    A,R3
         JNB    ACC.0,DTCON2
         RET                      ;??? (-)

DTCON2:  MOV    R7,#0             ;0.0 (+)
         CLR    C
         MOV    A,R4
         RRC    A
         JNC    DTCON3
         MOV    R7,#1             ;0.5
DTCON3:  MOV    DPL,A
         MOV    DPH,#0
         CALL   HTOD
         MOV    A,R7
         RET

; ********** DTWR,DTWR8,DTWR16 SUB **********
; WRITE COMMAND,DATA TO DS1620
; IN  = R2    COMMAND
;       R3,R4 DATA
; REG = A,R2,R3,R4,R5,R6

DTWR:    CLR    DTMCLK            ;CLK=0
         CALL   DTDEL
         SETB   DTMRST            ;RST=1
         CALL   DTDEL
         MOV    A,R2              ;COMMAND
         CALL   DTWRS
         CLR    DTMRST            ;RST=0
         CALL   DTDEL
         RET

DTWR8:   CLR    DTMCLK            ;CLK=0
         CALL   DTDEL
         SETB   DTMRST            ;RST=1
         CALL   DTDEL
         MOV    A,R2              ;COMMAND
         CALL   DTWRS
         MOV    A,R3              ;DATA
         CALL   DTWRS
         CLR    DTMRST            ;RST=0
         CALL   DTDEL
         CALL   DTDELE
         RET

DTWR16:  CLR    DTMCLK            ;CLK=0
         CALL   DTDEL
         SETB   DTMRST            ;RST=1
         CALL   DTDEL
         MOV    A,R2              ;COMMAND
         CALL   DTWRS
         MOV    A,R4              ;DATA
         CALL   DTWRS
         MOV    A,R3
         CALL   DTWRS
         CLR    DTMRST            ;RST=0
         CALL   DTDEL
         CALL   DTDELE
         RET

DTWRS:   MOV    R5,#8             ;WRITE COMMAND/DATA
DTWRS1:  RRC    A
         MOV    DTMDAT,C
         NOP
         SETB   DTMCLK            ;RISING EDGE CLOCK
         CALL   DTDEL
         CLR    DTMCLK
         CALL   DTDEL
         DJNZ   R5,DTWRS1
         RET

DTDEL:   MOV    R6,#4             ;DELAY
         DJNZ   R6,$
         RET

DTDELE:  MOV    R5,#40H           ;END DELAY (FOR EEPROM)
DTDELE1: MOV    R6,#0
         DJNZ   R6,$
         DJNZ   R5,DTDELE1
         RET

; ********** DTRD8,DTRD16 SUB **********
; WRITE COMMAND , READ DATA TO/FROM DS1620
; IN  = R2    COMMAND
; OUT = R3,R4 DATA (9 BIT)
; REG = A,R2,R3,R4,R5,R6

DTRD8:   CLR    DTMCLK            ;CLK=0
         CALL   DTDEL
         SETB   DTMRST            ;RST=1
         CALL   DTDEL
         MOV    A,R2              ;COMMAND
         CALL   DTWRS
         CALL   DTRDS
         MOV    R3,A
         CLR    DTMRST            ;RST=0
         CALL   DTDEL
         RET

DTRD16:  CLR    DTMCLK            ;CLK=0
         CALL   DTDEL
         SETB   DTMRST            ;RST=1
         CALL   DTDEL
         MOV    A,R2              ;COMMAND
         CALL   DTWRS
         CALL   DTRDS
         MOV    R4,A
         CALL   DTRDS
         ANL    A,#00000001B
         MOV    R3,A
         CLR    DTMRST            ;RST=0
         CALL   DTDEL
         RET

DTRDS:   MOV    R5,#8             ;READ DATA SUB
         CLR    A
DTRDS1:  CLR    DTMCLK
         CALL   DTDEL
         MOV    C,DTMDAT
         RRC    A
         SETB   DTMCLK
         CALL   DTDEL
         DJNZ   R5,DTRDS1
         RET

; ********** HTOD SUB **********
; HEX TO DECIMAL
; IN  = DPTR
; OUT = R1,R2,R3
; REG = A,R0,R1,R2,R3,R4,R5,DPTR

HTOD:    CLR    A                 ;CLEAR OUTPUT
         MOV    R1,A
         MOV    R2,A
         MOV    R3,A

         MOV    R4,#16            ;SHIFT 16 BIT
HTOD1:   MOV    A,DPL
         RLC    A
         MOV    DPL,A
         MOV    A,DPH
         RLC    A
         MOV    DPH,A

         MOV    R5,#3             ;ADD DECIMAL
         MOV    R0,#3             ;INDEX TO R3
HTOD2:   MOV    A,@R0
         ADDC   A,ACC
         DA     A
         MOV    @R0,A
         DEC    R0
         DJNZ   R5,HTOD2
         DJNZ   R4,HTOD1
         RET

; ********** DTOH SUB **********
; DECIMAL TO HEX
; IN  = R1,R2,R3
; OUT = DPTR
; REG = A,R0,R1,R2,R3,R4,R5,DPTR

DTOH:    MOV    R4,#16
DTOH1:   MOV    R5,#3             ;SHIFT & SUB
         MOV    R0,#1             ;INDEX TO R1
         CLR    C
DTOH2:   MOV    A,@R0
         RRC    A
         PUSH   PSW               ;-[
         JNB    ACC.7,DTOH3
         CLR    C
         SUBB   A,#30H
DTOH3:   JNB    ACC.3,DTOH4
         CLR    C
         SUBB   A,#03H
DTOH4:   MOV    @R0,A
         INC    R0
         POP    PSW               ;-]
         DJNZ   R5,DTOH2

         MOV    A,DPH
         RRC    A
         MOV    DPH,A
         MOV    A,DPL
         RRC    A
         MOV    DPL,A
         DJNZ   R4,DTOH1
         RET

; ********** RAMT SUB **********
; RAM TEST SUBROUTINE (100 BYTE BLOCK)
; IN  = ADDIXX ADDIYY
; REG = ALL

RAMT:    MOV    DPH,ADDIXX        ;LOAD TABLE TO RAM
         MOV    DPL,ADDIXX+1
RAMT1:   MOV    B,#'A'            ;START DATA
         MOV    R2,#100
RAMT12:  MOV    A,B
         MOVX   @DPTR,A
         MOV    A,DPH
         CJNE   A,ADDIYY,RAMT14
         MOV    A,DPL
         CJNE   A,ADDIYY+1,RAMT14
         SJMP   RAMT2
RAMT14:  INC    DPTR
         INC    B
         DJNZ   R2,RAMT12
         SJMP   RAMT1

RAMT2:   MOV    DPH,ADDIXX        ;START ADDRESS
         MOV    DPL,ADDIXX+1

RAMT3:   PUSH   DPH               ;DISPLAY ADDRESS
         PUSH   DPL
         MOV    A,DPH
         CALL   HTOA
         MOV    LCDBUF+35,R2
         MOV    LCDBUF+36,R3
         MOV    A,#'x'
         MOV    LCDBUF+37,A
         MOV    LCDBUF+38,A
         CALL   LCDOUT
         MOV    R2,#0
         MOV    R3,#10
         CALL   DMSEC
         POP    DPL
         POP    DPH

         MOV    B,#'A'            ;CHECK DATA
         MOV    R2,#100
RAMT32:  MOVX   A,@DPTR
         CJNE   A,B,RAMT7
         MOV    A,DPH
         CJNE   A,ADDIYY,RAMT34
         MOV    A,DPL
         CJNE   A,ADDIYY+1,RAMT34
         SJMP   RAMT4
RAMT34:  CLR    A
         MOVX   @DPTR,A
         INC    DPTR
         INC    B
         DJNZ   R2,RAMT32
         SJMP   RAMT3
RAMT4:   RET                      ;EXIT

RAMT7:   CALL   LBEEP             ;DISPLAY ERROR
         MOV    LCDBUF+37,#'E'
         MOV    LCDBUF+38,#'R'
         CALL   LCDOUT
RAMT71:  SJMP   $

; ********** 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

; ********** 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

SBYTEHC: XCH    A,R7              ;R7=CHECKSUM
         ADD    A,R7
         XCH    A,R7
         CALL   SBYTEH
         RET

; ********** SBYTEW SUB **********
; SEND 4 BYTE FROM SPTR
; IN  = DPTR
; REG = A,R2,R3

SBYTEW:  MOV    A,DPH
         CALL   SBYTEH
         MOV    A,DPL
         CALL   SBYTEH
         RET

; ********** SBLOCK SUB **********
; SEND BLOCK
; IN  = DPTR ROM-ADDRESS (END BY 0 OR 0DH)
; OUT = DPTR (NEXT)
; REG = A,DPTR

SBLOCK:  CLR    A
         MOVC   A,@A+DPTR
         CJNE   A,#0,SBLOCK1
         INC    DPTR              ;NEXT ADDRESS
         RET                      ;EXIT BY 0
SBLOCK1: CJNE   A,#0DH,SBLOCK2
         CALL   SLF               ;CR/LF
         INC    DPTR              ;NEXT ADDRESS
         RET                      ;EXIT BY ODH
SBLOCK2: CALL   SBYTE
         INC    DPTR
         JMP    SBLOCK

; ********** SBLANK SUB **********
; SEND BLANK
; IN  = R2 (NO OF BLANK)
; REG = A,R2

SBLANK:  MOV    A,#' '
         CALL   SBYTE
         DJNZ   R2,SBLANK
         RET

; ********** SLF SUB **********
; PRINT CR/LF
; 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

; ********** RBYTEH SUB **********
; READ HEX-ASCII FROM SERIAL (2 BYTE)
; OUT = A
; REG = A,R2,R3

RBYTEH:  CALL   RBYTE
         MOV    R2,A
         CALL   RBYTE
         MOV    R3,A
         CALL   ATOH
         RET

RBYTEHC: CALL   RBYTEH            ;R7=CHECKSUM
         XCH    A,R7
         ADD    A,R7
         XCH    A,R7
         RET

; ********** RBYTEW SUB **********
; READ BYTE WORD FROM SERIAL (4 BYTE)
; OUT = DPTR
; REG = A,R2,R3,DPTR

RBYTEW:  CALL   RBYTEH
         MOV    DPH,A
         CALL   RBYTEH
         MOV    DPL,A
         RET

; ********** BAUDS SUB **********
; SET BAUD RATE (HW-RS232)
; IN  = A (12H,24H,48H,96H,19H)
; REG = B

BAUDS:   MOV    B,PCON
         CJNE   A,#19H,BAUDS1
         MOV    TH1,#BRAT96       ;19200
         SETB   SMOD
         JMP    BAUDS7
BAUDS1:  CJNE   A,#96H,BAUDS2
         MOV    TH1,#BRAT96       ;9600
         CLR    SMOD
         JMP    BAUDS7
BAUDS2:  CJNE   A,#48H,BAUDS3
         MOV    TH1,#BRAT48       ;4800
         JMP    BAUDS7
BAUDS3:  CJNE   A,#24H,BAUDS4
         MOV    TH1,#BRAT24       ;2400
         JMP    BAUDS7
BAUDS4:  CJNE   A,#12H,BAUDS71
         MOV    TH1,#BRAT12       ;1200
BAUDS7:  MOV    PCON,B
BAUDS71: RET

; ********** SON,SOFF SUB **********

SON:     SETB   COMDRV
         RET

SOFF:    JNB    TI,$
         NOP
         NOP
         CLR    COMDRV
         RET

         END

