; FILENAME      XDS1620K.ASM
; DESCRIPTION   TEST DS1620 CHIP ON JAZZ31
; HARDWARE      JAZZ31 AND DS1620
; ASSEMBLER     SXA51
; START-DATE    22/11/37
; SOFTWARE ENG. KRIANGSAK B.
; COMPANY       SILA RESEARCH CO.,LTD.

; I/O PORT        DS1620         JAZZ-31
;               ==========     ==========
;               PIN-1 DQ   --- PIN-1 P1.0
;               PIN-2 CLK  --- PIN-2 P1.1
;               PIN-3 /RST --- PIN-3 P1.2

; FUNCTION      3-FUNCTION FOR DS1620
;               PRESS KEY 1 = READ TEMPERATURE
;               PRESS KEY 2 = READ T-HIGH,T-LOW FROM DS1620
;                             AND DISPLAY ON JAZZ-31
;               PRESS KEY 3 = INPUT T-HIGH,T-LOW FROM KEY
;                             AND SET TO DS1620 (STAND-ALONE MODE)

; ********** VARIABLE **********

         $INCLUDE "J31H2.ASM"

TMDATA   EQU    P1.0
TMCLCK   EQU    P1.1
TMRSTT   EQU    P1.2

; ********** MAIN **********

         ORG    8100H

MAIN:    CLR    TMRSTT
         MOV    R2,#5
         LCALL  DTSEC
         LCALL  CLEAR

MAINX:   MOV    DISBUF+2,#06H     ;123.trS
         MOV    DISBUF+3,#5BH
         MOV    DISBUF+4,#0CFH
         MOV    DISBUF+5,#78H
         MOV    DISBUF+6,#50H
         MOV    DISBUF+7,#6DH
         MOV    DISBUF,#0
         MOV    DISBUF+1,#0
         LCALL  SCAN
         CJNE   A,#1,$+6
         LJMP   MAIN2
         CJNE   A,#2,$+6
         LJMP   RHL
         CJNE   A,#3,$+6
         LJMP   SETUP
         SJMP   MAINX

; READ TEMP AND DISPLAY

MAIN2:   MOV    R2,#0CH           ;WRITE CONFIG
         MOV    R3,#00000011B
         LCALL  DTWR8

MAIN4:   MOV    R2,#0EEH          ;START CONVERT
         LCALL  DTWR
MAIN42:  MOV    R2,#0ACH          ;READ CONFIG (DONE BIT)
         LCALL  DTRD8
         MOV    A,R3
         JNB    ACC.7,MAIN42      ;DONE=0

         MOV    R2,#0AAH          ;READ TEMP
         LCALL  DTRD16
         LCALL  DTCON
         PUSH   ACC
         MOV    HEXBUF,R2
         MOV    HEXBUF+1,R3
         LCALL  HTOS
         POP    ACC
         MOV    DISBUF+6,#3FH     ;0.0
         JNB    ACC.0,MAIN5
         MOV    DISBUF+6,#6DH     ;0.5
MAIN5:   CLR    A
         MOV    DISBUF,A
         MOV    DISBUF+1,A
         MOV    DISBUF+2,A
         MOV    DISBUF+7,A
         ORL    DISBUF+5,#80H
         MOV    R2,#10H
         LCALL  SCAND
         SJMP   MAIN4

; SET FOR DS1620 STAND-ALONE MODE

SETUP:   MOV    A,#0              ;HIGH
         MOV    R2,#76H
         LCALL  INH
         MOV    R1,#0
         MOV    R2,#0
         MOV    R3,A
         LCALL  DTOH
         MOV    B,#2
         MUL    AB
         MOV    R3,B
         MOV    R4,A
         MOV    R2,#01H           ;WRITE TH
         LCALL  DTWR16

         MOV    A,#0              ;LOW
         MOV    R2,#38H
         LCALL  INH
         MOV    R1,#0
         MOV    R2,#0
         MOV    R3,A
         LCALL  DTOH
         MOV    B,#2
         MUL    AB
         MOV    R3,B
         MOV    R4,A
         MOV    R2,#02H           ;WRITE TL
         LCALL  DTWR16

         MOV    R2,#0CH           ;WRITE CONFIG
         MOV    R3,#00000000B
         LCALL  DTWR8
         LCALL  UBEEP
         LJMP   MAINX

; READ & DISPLAY TH,TL ON DS1620

RHL:     MOV    R2,#0A1H          ;READ TH
         LCALL  DTRD16
         LCALL  DTCON
         PUSH   ACC
         MOV    HEXBUF,R2
         MOV    HEXBUF+1,R3
         LCALL  HTOS
         POP    ACC
         MOV    DISBUF+2,#0F6H      ;H.
         LCALL  RHLS
         MOV    DISBUF,#0
         MOV    DISBUF+1,#0
         LCALL  SCAN

         MOV    R2,#0A2H          ;READ TL
         LCALL  DTRD16
         LCALL  DTCON
         PUSH   ACC
         MOV    HEXBUF,R2
         MOV    HEXBUF+1,R3
         LCALL  HTOS
         POP    ACC
         MOV    DISBUF+2,#0B8H      ;L.
         LCALL  RHLS
         MOV    DISBUF,#0
         MOV    DISBUF+1,#0
         LCALL  SCAN
         LJMP   MAINX
  
RHLS:    MOV    DISBUF+6,#3FH     ;0.0
         JNB    ACC.0,RHLS1
         MOV    DISBUF+6,#6DH     ;0.5
RHLS1:   MOV    DISBUF+7,#0
         ORL    DISBUF+5,#80H
         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
         LCALL  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    TMCLCK            ;CLK=0
         LCALL  DTDEL
         SETB   TMRSTT            ;RST=1
         LCALL  DTDEL
         MOV    A,R2              ;COMMAND
         LCALL  DTWRS
         CLR    TMRSTT            ;RST=0
         LCALL  DTDEL
         RET

DTWR8:   CLR    TMCLCK            ;CLK=0
         LCALL  DTDEL
         SETB   TMRSTT            ;RST=1
         LCALL  DTDEL
         MOV    A,R2              ;COMMAND
         LCALL  DTWRS
         MOV    A,R3              ;DATA
         LCALL  DTWRS
         CLR    TMRSTT            ;RST=0
         LCALL  DTDEL
         LCALL  DTDELE
         RET

DTWR16:  CLR    TMCLCK            ;CLK=0
         LCALL  DTDEL
         SETB   TMRSTT            ;RST=1
         LCALL  DTDEL
         MOV    A,R2              ;COMMAND
         LCALL  DTWRS
         MOV    A,R4              ;DATA
         LCALL  DTWRS
         MOV    A,R3
         LCALL  DTWRS
         CLR    TMRSTT            ;RST=0
         LCALL  DTDEL
         LCALL  DTDELE
         RET

DTWRS:   MOV    R5,#8             ;WRITE COMMAND/DATA
DTWRS1:  RRC    A
         MOV    TMDATA,C
         NOP
         SETB   TMCLCK            ;RISING EDGE CLOCK
         LCALL  DTDEL
         CLR    TMCLCK
         LCALL  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    TMCLCK            ;CLK=0
         LCALL  DTDEL
         SETB   TMRSTT            ;RST=1
         LCALL  DTDEL
         MOV    A,R2              ;COMMAND
         LCALL  DTWRS
         LCALL  DTRDS
         MOV    R3,A
         CLR    TMRSTT            ;RST=0
         LCALL  DTDEL
         RET

DTRD16:  CLR    TMCLCK            ;CLK=0
         LCALL  DTDEL
         SETB   TMRSTT            ;RST=1
         LCALL  DTDEL
         MOV    A,R2              ;COMMAND
         LCALL  DTWRS
         LCALL  DTRDS
         MOV    R4,A
         LCALL  DTRDS
         ANL    A,#00000001B
         MOV    R3,A
         CLR    TMRSTT            ;RST=0
         LCALL  DTDEL
         RET

DTRDS:   MOV    R5,#8             ;READ DATA SUB
         CLR    A
DTRDS1:  CLR    TMCLCK
         LCALL  DTDEL
         MOV    C,TMDATA
         RRC    A
         SETB   TMCLCK
         LCALL  DTDEL
         DJNZ   R5,DTRDS1
         RET

         END

