;	CARD NAME : RTX-03B 14 ADDA A/D Convert
;	LICENCE   : REAL TIME INDUSTRIAL CO., LTD.
;	AUTHOR BY : MR. ROBIN SHEN
;	SOFTWARE  : Assembly Language 01/07/96
;
; *************************************************
; Define string character
;
DATASEG SEGMENT ; data segment
ADTIL_1 DB '  Channel =  $'
ADTIL_2 DB '  Date =  $'
DATASEG ENDS	; end of data segment
;
; *************************************************
PROGRAM SEGMENT ; code segment
;
; -------------------------------------------------
MAIN	PROC	FAR	; main part of program
	ASSUME	CS: PROGRAM, DS: DATASEG
;
START:			; starting execution address
; Set up stack for return
	PUSH DS 	; save DS on stack
	SUB  AX,AX	; set AX to 0
	PUSH AX 	; put AX to stack
KEYBOD: MOV  AH,0BH	; check keyboard input funct
	INT  21H	; call DOS
	CMP  AL,00H	; is keyin ?
	JZ   KEYBOD	; yes, keyin !
	CALL CRLF	; skip a line
AD14:	MOV  DX,0200H	; set DX port address
	MOV  CL,00H	; CL = channel number
NXCHL:	CALL LPBACK	; loop back 8 times
	PUSH DX 	; save DX
	MOV  DL,02H	; get high byte address
	IN   AL,DX	; get high 6 bit data
	AND  AL,3FH	; mask high 2 bit
	MOV  AH,AL	; move to high byte in AX
	DEC  DL 	; get low byte address
	IN   AL,DX	; get low 8 bit data
;
; Now A/D convert data in AX register
;
	PUSH AX 	; store AX
	PUSH CX 	; store CX
	MOV  AX,DATASEG ; set data to work area
	MOV  DS,AX	;
	MOV  DX,OFFSET ADTIL_1
	MOV  AH,09H	; display function
	INT  21H	; call DOS
	POP  CX 	; pop CX
	MOV  BX,CX	; channel number to BX
	CALL BIN2DEC	; call display BX routine
	PUSH CX 	; store CX
	MOV  AX,DATASEG ; set data to work area
	MOV  DS,AX	;
	MOV  DX,OFFSET ADTIL_2
	MOV  AH,09H
	INT  21H	; call DOS
	POP  CX 	; pop CX
	POP  AX 	; pop AX
	MOV  BX,AX	; A/D data to BX
	CALL BIN2DEC	; call display BX routine
	CALL CRLF	; carriage return & linefeed
	CALL DELAY	; delay CX = FFFF
	POP  DX 	; pop DX
	INC  CL 	; next channel
	CMP  CL,10H	; is 15 channel ?
	CALL DELAY	; delay CX = FFFF
	JNZ  NXCHL	; yes !
	CALL CRLF	; carriage return & linefeed
	CALL CRLF	; carriage return & linefeed
	JMP  AD14	; next routine
	RET		; return to DOS
MAIN	ENDP		; end of main porcedure
;
; ---------------------------------------------------
; Loop back 8 times subroutine
;
LPBACK	PROC	NEAR	; define procedure
	PUSH DX 	; save DX
	PUSH CX 	; save CX
	MOV  AL,00H	; clear AX
	MOV  DL,03H	; REM SET DX ADDRESS &H203
	OUT  DX,AL	; clear A/D register &h203
	DEC  DL 	; decrement DX
	DEC  DL 	; decrement DX
	DEC  DL 	; decrement DX
	MOV  AL,CL	; channel number into AL
;	MOV  DL,00H	; rem A/D CHANNEL NUMBER &H200
	OUT  DX,AL	; select A/D channel
	MOV  DL,04H	; REM select low byte 8 bit
	MOV  CX,08H	;  loop back 8 times
LOWBYTE:		;   for low byte
	IN   AL,DX	;
	LOOP LOWBYTE	;
	MOV  DL,05H	; REM select high byte 6 bit
	MOV  CX,08H	;  loop back 8 times
HIBYTE: 		;   for high byte
	IN   AL,DX	;
	LOOP HIBYTE	;
	POP  CX 	; pop CX
	POP  DX 	; pop DX
	RET		; return form LPBACK
	CALL DELAY	; delay CX = FFFF
LPBACK	ENDP		; end of porcedure
;
; ---------------------------------------------------
; Convert binary in BX to decimal in screen
;
BIN2DEC PROC	NEAR	; define porcedure
	PUSH CX 	; save CX
	MOV  CX,10000D	; divide by 10000
	CALL DEC_DIV
	MOV  CX,1000D	; divide by 1000
	CALL DEC_DIV
	MOV  CX,100D	; divide by 100
	CALL DEC_DIV
	MOV  CX,10D	; divide by 10
	CALL DEC_DIV
	MOV  CX,1D	; divide by 1
	CALL DEC_DIV
	POP  CX 	; pop CX
	RET		; return form BIN2DEC
;
; --------------------------------------------------
; Sub-subroutine to divide number in BX by
;   number in CX, print in screen
;
DEC_DIV PROC	NEAR	; define procedure
	MOV  AX,BX	; number high half
	MOV  DX,00H	; zero out low half
	DIV  CX 	; divide by CX
	MOV  BX,DX	; remainder into BX
	MOV  DL,AL	; quotient into DL
	ADD  DL,30H	; convert to ASCII
	MOV  AH,02H	; display function
	INT  21H	; call DOS
	RET		; return from DEC_DIV
DEC_DIV ENDP		; end of porcedure
;
; --------------------------------------------------
;
BIN2DEC ENDP		; end of porcedure BIN2DEC
;
; --------------------------------------------------
; Carriage return & Linefeed
;
CRLF	PROC	NEAR
	PUSH CX 	; save CX
	MOV  DL,0DH	; carriage return
	MOV  AH,02H	; display function
	INT  21H	; call DOS
	MOV  DL,0AH	; linefeed
	MOV  AH,02H	; display function
	INT  21H	; call DOS
	POP  CX 	; pop CX
	RET		; return from CRLF
CRLF	ENDP		; end of procedure
;
; --------------------------------------------------
; Delay subroutine, delay time CX = FFFF
;
DELAY	PROC	NEAR	; define procedure
	PUSH CX 	; save CX
	MOV  CX,9FFFH	; define delay CX = FFFFH
NXDEL1: LOOP NXDEL1	; is zero ?
	POP  CX 	; pop CX
	RET		; return form delay
	PUSH CX 	; save CX
	MOV  CX,9FFFH	; define delay CX = FFFFH
NXDEL2: LOOP NXDEL2	; is zero ?
	POP  CX 	; pop CX
	RET		; return form delay
DELAY	ENDP		; end of procedure
;
; --------------------------------------------------
;
PROGRAM ENDS		; end of code segment
;
; **************************************************
;
	END	START	; end of main porgram
;
