[ บทความ : ตัวอย่างสำหรับ CP-SB31 V2 ตอนที่ 7 ] ทดสอบติดต่อกับ ADC ด้วย CP-SB31V2

ตัวอย่างซอร์สโค้ดสำหรับติดต่อกับ ADC ด้วยไอซี LTC1298 กับบอร์ด CP-SB31V2 ที่ติดตั้ง ET-DEBUG31 เป็นดังนี้



;/********************************/;
;/* Demo Program For ADC LTC1298 */;
;/* Hardware  :  ET-CPSB31 V2.0  */;
;/* Complier  :  Cross-32  V2.0  */;
;/********************************/;
;

;/***************************************/;
;/* Equate List Port I/O ET-CPSB31 V2.0 */;
;/***************************************/;
;
CS_ADC     EQU     P1.4                    ; Chips Select LTC1298
CS_RTC     EQU     P1.5                    ; Chips Select DS1202
DATA_ADC   EQU     P1.6                    ; Data I/O DS1202
CLK_ADC    EQU     P1.7                    ; Clock DS1202

            ORG     2200H                   ; Strat Program For "ET-DEBUG31"

MAIN:       SETB    DATA_ADC
            SETB    CLK_ADC
            SETB    CS_ADC
            CLR     CS_RTC                  ; Disable CS RTC1202
            ;
INIT_SER:   MOV     A,#0FDH                 ; Set baud rate 9600
            MOV     TH1,A
            MOV     TL1,A
            MOV     TMOD,#00100000B         ; Timer1 Mode 1
            CLR     ES                      ; Disable serial interupt
            CLR     ET1                     ; Disable timer1 interupt
            SETB    TR1                     ; Set timer1 control
            MOV     SCON,#01010000B         ; Serial mode 1
            ;
            LCALL   PRINT_SER
            DB     0CH,"Test Read ADC 12Bit LTC1298",0DH,0AH,00H
            ;
LOOP:       LCALL   PRINT_SER
            DB     0DH,"ADC Channal0 = ",00
            MOV     A,#00H                  ; Din Word For Ch0 Read
            LCALL   READ_ADC
            MOV     A,DPH
            LCALL   TX_2ASC
            MOV     A,DPL
            LCALL   TX_2ASC
            ;
            LCALL   PRINT_SER
            DB     " ----  ADC Channal1 = ",00
            MOV     A,#01H                  ; Din Word For Ch1 Read
            LCALL   READ_ADC
            MOV     A,DPH
            LCALL   TX_2ASC
            MOV     A,DPL
            LCALL   TX_2ASC
            SJMP    LOOP

;*************************
;* Send 1-Byte to RS-232 *
;* Input   : ACC         *
;* Output  : Serial port *
;*************************
;
TX_BYTE:    PUSH   IE
            CLR    TI
            MOV    SBUF,A
            JNB    TI,$
            CLR    TI
            POP    IE
            RET

;****************************
;* Receive Data From RS-232 *
;* Input   :  Serial Port   *
;* Output  :  ACC           *
;****************************
;
RX_BYTE:    PUSH   IE
            JNB    RI,$                     ; Wait data
            CLR    RI
            MOV    A,SBUF
            POP    IE
            RET

;******************************
;*    Send 2 byte of ASCII    *
;*  Input : ACC (HEX)         *
;*  Example : A = A3H         *
;*          : send 41H,33H    *
;******************************
;
TX_2ASC:    LCALL   HEX_2ASC
            LCALL   TX_BYTE
            MOV     A,B
            LCALL   TX_BYTE
            RET

;******************************
;*   Convert HEX to ASCII     *
;* Input    :  A              *
;* Output   :  A (high-byte)  *
;*          :  B (low-byte)   *
;******************************
;
HEX_2ASC:   PUSH    ACC
            LCALL   HEX_TO_ASC
            MOV     B,A                     ; B = Lo byte
            POP     ACC
            SWAP    A
            LCALL   HEX_TO_ASC
            RET

;********************************
;*  Convert hex (0-F) to ASCII  *
;*  Input   : ACC (HEX code)    *
;*  Output  : ACC (ASCII code)  *
;********************************
;
HEX_TO_ASC: PUSH    PSW
            CLR     CY
            ANL     A,#00001111B
            CJNE    A,#09H,HTOA1
            SETB    CY
HTOA1:      JNB     CY,HTOA2                ;> 0AH
            ORL     A,#30H                  ;< 0AH
            SJMP    END_HTOA
HTOA2:      SUBB    A,#09H                  ;> 0AH
            ORL     A,#40H
END_HTOA:   POP     PSW
            RET

;************************************
;*  Print Data data to Serial Port  *
;*  to display buffer.              *
;*  Usage    : LCALL PRINT_SER      *
;*           : DB   'xxxx',00       *
;*  Register : ACC                  *
;*  Note     : last byte must be 00 *
;************************************
;
PRINT_SER:  POP     DPH
            POP     DPL
PRINT1:     CLR     A
            MOVC    A,@A+DPTR
            CJNE    A,#00H,PRINT2
            SJMP    PRINT3
PRINT2:     LCALL   TX_BYTE
            INC     DPTR
            SJMP    PRINT1
PRINT3:     PUSH    DPL
            PUSH    DPH
            RET

;/**********************/;
;/* Read Data From ADC */;
;/* LTC1298(12Bit A/D) */;
;/* Input  : ACC(ChXX) */;
;/* Output : DPTR      */;
;/**********************/;
; START,SGL,ODD,MSBF = ADC
;   1    1   0   X   = Ch0
;   1    1   1   X   = Ch1
; MSBF = 1 = MSB First
; MSBF = 0 = LSB First
; A = 1101 XXXX = Ch0(DF)
; A = 1111 XXXX = Ch1(FF)
;
READ_ADC:   PUSH    B
            SWAP    A               ; Swap Data
            RL      A               ; Shift User Channel to ODD
            SETB    ACC.7           ; Start Bit
            SETB    ACC.6           ; SGL = 1
            SETB    ACC.4           ; MSBF = 1 (MSB Read First)
            SETB    CLK_ADC         ; Makesure Start Clock
            SETB    DATA_ADC        ; Start Bit
            SETB    CS_ADC          ; Makesure CS=High
            CLR     CS_ADC          ; Start Operation
            ;
            MOV     B,#4            ; Counter (START/SGL/ODD/MSBF)
READ1:      RLC     A               ; Rotate Din to Carry
            CLR     CLK_ADC         ; Clock Low
            MOV     DATA_ADC,C      ; Get Data
            SETB    CLK_ADC         ; Clock High (ADC Receive Data)
            DJNZ    B,READ1         ; Loop Send Data Word 4 Bit
            ;
            SETB    DATA_ADC        ; Start for Input Read
            CLR     CLK_ADC         ; Clock Low
            MOV     B,#5            ; Counter MSB 4-Bit & Null
READ2:      MOV     C,DATA_ADC      ; Read Data
            RLC     A               ; Rotate Carry to Data
            SETB    CLK_ADC         ; Clock High
            CLR     CLK_ADC         ; Clock Low
            DJNZ    B,READ2         ; Next bit
            ANL     A,#0FH          ; Only 4bit Data  (D11..D8)
            MOV     DPH,A           ; Save Result MSB Data
            ;
            CLR     A
            MOV     B,#8            ; Counter LSB 8-bit Data
READ3:      MOV     C,DATA_ADC      ; Read Data
            RLC     A               ; Rotate Carry to Data
            SETB    CLK_ADC         ; Clock High
            CLR     CLK_ADC         ; Clock Low
            DJNZ    B,READ3         ; Next Bit
            MOV     DPL,A           ; Save Result LSB (D7..D0)
            SETB    CS_ADC          ; Stop Operation
            POP     B
            RET

            END

                

สามารถ download ไฟล์ตัวอย่างของบอร์ด พร้อม assembler ได้เลยครับ


เขียนโดย : ETT
Author : ETT team
e-mail : sales@etteam.com
วันที่ทำการปรับปรุง : ๒๒ ธ.ค. ๒๕๔๓