;FILENAME:   STLAB091.ASM
;DATE:       March 16, 1996
;TITLE:      Analog voltage to digital data conversion
;HARDWARE:   MLAB-121

; JAZZ-31 utilities subroutines
HTOD    EQU     00F0H    ;hex to decimal
HTOS    EQU     00E4H    ;hex to segment code
SCAND   EQU     0096H    ;scan display
; Pin Interface
CSBIT   EQU     P3.4     ;CHIP SELECT
SCLKBT  EQU     P3.5     ;SERIAL CLOCK
DINBIT  EQU     P1.2     ;DATA IN
DOUTBT  EQU     P1.3     ;DATA OUT
; Variables
ADCBUF  EQU     20H      ;ADC BUFFER (2 BYTES)
DISBUF  EQU     32H      ;DISPLAY BUFFER (8 BYTES SEGMENT CODE)
HEXBUF  EQU     3AH      ;HEX BUFFER (3 BYTES)

        ORG    8100H
START:
        SETB    CSBIT           ;SET CS\
        CLR     SCLKBT          ;CLEAR SCLK
LOOP0:
        LCALL   INIT_MAX192
        LCALL   ADC_READ_10BITS     ;read 10-bit digital conversion
;
; display on jazz-31 LED display
;
        MOV     DPH,ADCBUF+0
        MOV     DPL,ADCBUF+1
        CALL    HTOD            ;change hex value to decimal
        MOV     HEXBUF+0,#00H
        MOV     HEXBUF+1,R2
        MOV     HEXBUF+2,R3
        CALL    HTOS            ;and change to display segment code
        MOV     DISBUF+0,#58H   ;c
        MOV     DISBUF+1,#76H   ;H
        MOV     DISBUF+3,#08H   ;_
        MOV     R2,#65
        CALL    SCAND
        LJMP    LOOP0
;
; Initialize MAX192 ADC by transmit TB1 byte (serial form)
;
INIT_MAX192:
        CLR     CSBIT           ;prompt to transfer
        MOV     A,#10001111B    ;set TB1 byte (select Channel 0, unipolar,
                                ;single ended line & ext clock mode
        MOV     R2,#08H         ;8-data bits out
TB1_1:  RLC     A
        MOV     DINBIT,C        ;output to pin
        LCALL   MAKE_ADC_CLK    ;make sync clock
        DJNZ    R2,TB1_1
        RET
;
; Sync. clock for MAX192 ADC
;
MAKE_ADC_CLK:
        SETB    SCLKBT          ;high signal
        MOV     R3,#1           ;on period
        DJNZ    R3,$
        CLR     SCLKBT          ;low signal
        RET
;
; Receive byte RB2 and RB3 (contain 10 bits data) from ADC
; OUT: ADCBUF+0 (000000XXB) HIGH BYTE
;      ADCBUF+1 (XXXXXXXXB) LOW BYTE
;
ADC_READ_10BITS:
        LCALL  ADC_READ_8BIT
        RL     A
        RL     A
        MOV    R4,A
        ANL    A,#00000011B
        MOV    ADCBUF+0,A
;
        LCALL  ADC_READ_8BIT
        SETB   CSBIT             ;reset CS\ signal (end of comm.)
        RL     A
        RL     A
        ORL    A,#11111100B
        ANL    A,R4
        MOV    ADCBUF+1,A
        RET
;
; Get 8-bit serial data from MAX192 ADC
; OUT: A
;
ADC_READ_8BIT:
        MOV    R2,#08H          ;receive 8 bits
        CLR    A                ;clear variable
RD_8BIT1:
        LCALL   MAKE_ADC_CLK
        SETB   DOUTBT           ;SET DOUTBT=INPUT
        MOV    C,DOUTBT         ;read data
        RLC    A                ;shift left
        DJNZ   R2,RD_8BIT1
        RET

;
; (my own)
; Receive byte RB2 and RB3 (contain 10 bits data) from ADC
; RAM bits    15 14 13 12 11 10 09 08 07 06 05  04 03 02 01 00
; ADC Result  0  MSB                        LSB s1 s0 0  0  0
; OUT: ADCBUF+0 (000000XXB) HIGH BYTE
;      ADCBUF+1 (XXXXXXXXB) LOW BYTE
;
xADC_READ_10BITS:
        LCALL   ADC_READ_8BIT
        RL      A
        RL      A
        RL      A
        MOV     R4,A
        ANL     A,#00000011B
        MOV     ADCBUF+0,A
;
        LCALL   ADC_READ_8BIT
        SETB    CSBIT             ;reset CS\ signal (end of comm.)
        RL      A
        RL      A
        RL      A
        ANL     A,#00000111B
        MOV     R3,A
        MOV     A,R4
        ANL     A,#11111000B
        ORL     A,R3
        MOV     ADCBUF+1,A
        RET
;
        END

