; FILENAME      XMAX192.ASM
; DESCRIPTION   EXAMPLE PROGRAM FOR MAX192 8-CH,10-BIT ADC
; HARDWARE      JAZZ-31 V2.0 & MAX192 AT PORT1
; ASSEMBLER     SXA51
; WRITER        SAKCHAI W.
; COMPANY       SILA RESEARCH CO.,LTD.

; I/O PORT      MAX192             JAZZ-31
;               ===========     ==========
;               PIN-18 CS\  --- PIN-1 P1.0
;               PIN-19 SCLK --- PIN-2 P1.1
;               PIN-17 DIN  --- PIN-3 P1.2
;               PIN-15 DOUT --- PIN-4 P1.3

; FUNCTION      DISPLAY ON 7-SEGMENT
;               (DISPLAY VALUE X 4 = ANALOG INPUT VOLTAGE)

; ********** VARIABLE **********

CSBIT    EQU    P1.0         ;CHIP SELECT
SCLKBT   EQU    P1.1         ;SERIAL CLOCK
DINBIT   EQU    P1.2         ;DATA IN
DOUTBT   EQU    P1.3         ;DATA OUT

CLEAR    EQU    003CH        ;CLEAR DISPALY BUFFER
SCAND    EQU    0096H        ;SCAN DISPLAY
HTOS     EQU    00E4H        ;HEX TO SEGMENT
HTOD     EQU    00F0H        ;HEX TO DECIMAL

ADCBUF   EQU    20H          ;ADC BUFFER (2 BYTES)
TB1BUF   EQU    24H          ;TABEL1 BUFFER (1 BYTE)
DISBUF   EQU    32H          ;DISPLAY BUFFER (8 BYTES SEGMENT CODE)
HEXBUF   EQU    3AH          ;HEX BUFFER (3 BYTES)

; ********** MAIN **********
; 24-BIT EXTERNAL CLOCK MODE

         ORG    8100H

MAIN:    SETB   CSBIT             ;SET CS\
         CLR    SCLKBT            ;CLEAR SCLK

MAIN0:   MOV    TB1BUF,#10001111B ;SEL CH0
MAIN1:   CALL   TB1               ;TRANSMIT TB1
         CALL   D10B              ;10 BITS
         MOV    DPH,ADCBUF+0
         MOV    DPL,ADCBUF+1
         CALL   HTOD
         MOV    HEXBUF+0,#00H
         MOV    HEXBUF+1,R2
         MOV    HEXBUF+2,R3
         CALL   HTOS
         MOV    DISBUF+0,#58H     ;c
         MOV    DISBUF+1,#76H     ;H
         MOV    DISBUF+3,#08H     ;_
         MOV    R2,#65
         CALL   SCAND
         CALL   CLEAR
         MOV    R2,#5
         CALL   SCAND

         MOV    TB1BUF,#11001111B ;SEL CH1
MAIN2:   CALL   TB1               ;TRANSMIT TB1
         CALL   D10B              ;10 BITS
         MOV    DPH,ADCBUF+0
         MOV    DPL,ADCBUF+1
         CALL   HTOD
         MOV    HEXBUF+0,#10H
         MOV    HEXBUF+1,R2
         MOV    HEXBUF+2,R3
         CALL   HTOS
         MOV    DISBUF+0,#58H     ;c
         MOV    DISBUF+1,#76H     ;H
         MOV    DISBUF+3,#08H     ;_
         MOV    R2,#65
         CALL   SCAND
         CALL   CLEAR
         MOV    R2,#5
         CALL   SCAND

         MOV    TB1BUF,#10011111B ;SEL CH2
MAIN3:   CALL   TB1               ;TRANSMIT TB1
         CALL   D10B              ;10 BITS
         MOV    DPH,ADCBUF+0
         MOV    DPL,ADCBUF+1
         CALL   HTOD
         MOV    HEXBUF+0,#20H
         MOV    HEXBUF+1,R2
         MOV    HEXBUF+2,R3
         CALL   HTOS
         MOV    DISBUF+0,#58H     ;c
         MOV    DISBUF+1,#76H     ;H
         MOV    DISBUF+3,#08H     ;_
         MOV    R2,#65
         CALL   SCAND
         CALL   CLEAR
         MOV    R2,#5
         CALL   SCAND

         MOV    TB1BUF,#11011111B ;SEL CH3
MAIN4:   CALL   TB1               ;TRANSMIT TB1
         CALL   D10B              ;10 BITS
         MOV    DPH,ADCBUF+0
         MOV    DPL,ADCBUF+1
         CALL   HTOD
         MOV    HEXBUF+0,#30H
         MOV    HEXBUF+1,R2
         MOV    HEXBUF+2,R3
         CALL   HTOS
         MOV    DISBUF+0,#58H     ;c
         MOV    DISBUF+1,#76H     ;H
         MOV    DISBUF+3,#08H     ;_
         MOV    R2,#65
         CALL   SCAND
         CALL   CLEAR
         MOV    R2,#5
         CALL   SCAND

         MOV    TB1BUF,#10101111B ;SEL CH4
MAIN5:   CALL   TB1               ;TRANSMIT TB1
         CALL   D10B              ;10 BITS
         MOV    DPH,ADCBUF+0
         MOV    DPL,ADCBUF+1
         CALL   HTOD
         MOV    HEXBUF+0,#40H
         MOV    HEXBUF+1,R2
         MOV    HEXBUF+2,R3
         CALL   HTOS
         MOV    DISBUF+0,#58H     ;c
         MOV    DISBUF+1,#76H     ;H
         MOV    DISBUF+3,#08H     ;_
         MOV    R2,#65
         CALL   SCAND
         CALL   CLEAR
         MOV    R2,#5
         CALL   SCAND

         MOV    TB1BUF,#11101111B ;SEL CH5
MAIN6:   CALL   TB1               ;TRANSMIT TB1
         CALL   D10B              ;10 BITS
         MOV    DPH,ADCBUF+0
         MOV    DPL,ADCBUF+1
         CALL   HTOD
         MOV    HEXBUF+0,#50H
         MOV    HEXBUF+1,R2
         MOV    HEXBUF+2,R3
         CALL   HTOS
         MOV    DISBUF+0,#58H     ;c
         MOV    DISBUF+1,#76H     ;H
         MOV    DISBUF+3,#08H     ;_
         MOV    R2,#65
         CALL   SCAND
         CALL   CLEAR
         MOV    R2,#5
         CALL   SCAND

         MOV    TB1BUF,#10111111B ;SEL CH6
MAIN7:   CALL   TB1               ;TRANSMIT TB1
         CALL   D10B              ;10 BITS
         MOV    DPH,ADCBUF+0
         MOV    DPL,ADCBUF+1
         CALL   HTOD
         MOV    HEXBUF+0,#60H
         MOV    HEXBUF+1,R2
         MOV    HEXBUF+2,R3
         CALL   HTOS
         MOV    DISBUF+0,#58H     ;c
         MOV    DISBUF+1,#76H     ;H
         MOV    DISBUF+3,#08H     ;_
         MOV    R2,#65
         CALL   SCAND
         CALL   CLEAR
         MOV    R2,#5
         CALL   SCAND

         MOV    TB1BUF,#11111111B ;SEL CH7
MAIN8:   CALL   TB1               ;TRANSMIT TB1
         CALL   D10B              ;10 BITS
         MOV    DPH,ADCBUF+0
         MOV    DPL,ADCBUF+1
         CALL   HTOD
         MOV    HEXBUF+0,#70H
         MOV    HEXBUF+1,R2
         MOV    HEXBUF+2,R3
         CALL   HTOS
         MOV    DISBUF+0,#58H     ;c
         MOV    DISBUF+1,#76H     ;H
         MOV    DISBUF+3,#08H     ;_
         MOV    R2,#65
         CALL   SCAND
         CALL   CLEAR
         MOV    R2,#5
         CALL   SCAND
         LJMP   MAIN0

; ********** TB1 SUB **********
; TRANSMIT TB1 (1XXXXX11B)
; IN  = TB1BUF
; OUT = NONE
; REG = A,R2,R3

TB1:     CLR    CSBIT             ;CLEAR CS\
         MOV    R2,#8             ;8 SCLK
         MOV    A,TB1BUF
TB11:    RLC    A
         MOV    DINBIT,C          ;OUT TO DIN
         SETB   SCLKBT            ;SET SCLK (CLK = 0.1536 MHZ)
         MOV    R3,#1             ;COMPENSATE FOR DUTY CYCLE 45%-55%
         DJNZ   R3,$
         NOP
         CLR    SCLKBT            ;CLEAR SCLK
         DJNZ   R2,TB11
         RET

; ********** D10B SUB **********
; DATA 10 BITS (XXXXXXXXXXB)
; IN  = NONE
; OUT = ADCBUF+0 (000000XXB) HIGH BYTE
;       ADCBUF+1 (XXXXXXXXB) LOW BYTE
; REG = A,R4

D10B:    CALL   RB2X
         RL     A
         RL     A
         MOV    R4,A
         ANL    A,#00000011B
         MOV    ADCBUF+0,A
         CALL   RB3X
         RL     A
         RL     A
         ORL    A,#11111100B
         ANL    A,R4
         MOV    ADCBUF+1,A
         RET

; ********** RB2X SUB **********
; RECEIVE BYTE RB2
; IN  = NONE
; OUT = A
; REG = A,R2,R3

RB2X:    MOV    R2,#8             ;8 SCLK
         CLR    A
RB2X1:   SETB   SCLKBT            ;SET SCLK (CLK = 0.1536 MHZ)
         MOV    R3,#1             ;COMPENSATE FOR DUTY CYCLE 45%-55%
         DJNZ   R3,$
         NOP
         CLR    SCLKBT            ;CLEAR SCLK
         SETB   DOUTBT            ;SET DOUTBT=INPUT
         MOV    C,DOUTBT          ;DATA IN
         RLC    A
         DJNZ   R2,RB2X1
         RET

; ********** RB3X SUB **********
; RECEIVE BYTE RB3
; IN  =  NONE
; OUT =  A
; REG =  A,R2,R3

RB3X:    MOV    R2,#8             ;8 SCLK
         CLR    A
RB3X1:   SETB   SCLKBT            ;SET SCLK (CLK = 0.1536 MHZ)
         MOV    R3,#1             ;COMPENSATE FOR DUTY CYCLE 45%-55%
         DJNZ   R3,$
         NOP
         CLR    SCLKBT            ;CLEAR SCLK
         SETB   DOUTBT            ;SET DOUTBT=INPUT
         MOV    C,DOUTBT          ;DATA IN
         RLC    A
         DJNZ   R2,RB3X1
         SETB   CSBIT             ;SET CS\
         RET

         END
