;********************************************************************
;
; Author        : ADI - Apps            www.analog.com/MicroConverter
;
; Date          : October 2003
;
; File          : ADCtimer.asm
;
; Hardware      : ADuC842/ADuC843
;
; Description   : Performs ADC conversions at 116KSPS in Timer2 mode.
;                 Outputs ADC results to RAM.  Continuously
;                 flashes LED (independently of ADC routine) at
;                 approximately 3Hz.
;                 All rate calculations assume an 2.097152MHz Mclk.
;		
;********************************************************************

$MOD842                         ; Use 8052&ADuC842 predefined symbols

LED     EQU     P3.4            ; P3.4 drives red LED on eval board
CHAN    EQU     0               ; convert this ADC input channel..
                                ; ..chan values can be 0 thru 6

DSEG 
ORG 0030H
LENGTH EQU 40
BUFFER: DS LENGTH                ; set up buffer in RAM

;____________________________________________________________________
                                                  ; BEGINNING OF CODE
CSEG

ORG 0000h

        JMP     MAIN            ; jump to main program
;____________________________________________________________________
                                             ; INTERRUPT VECTOR SPACE
ORG 0033H ; (ADC ISR)
	CJNE    R0,#58H,CONT
        JMP     EXIT		; place breakpoint here to view ram in debugger after conversions
CONT:	MOV     @R0,ADCDATAH
	INC     R0
	MOV     @R0,ADCDATAL		
	INC     R0
EXIT:	RETI	
	

;====================================================================
                                                       ; MAIN PROGRAM
ORG 004Bh

MAIN:	MOV     R0,#BUFFER
; PRECONFIGURE...
        MOV     ADCCON1,#09Eh   ; power up ADC & enable Timer2 mode
        MOV     ADCCON2,#CHAN   ; select channel to convert
        MOV     RCAP2L,#0F6h    ; sample period = 2 * T2 reload prd
        MOV     RCAP2H,#0FFh    ;   = 2*(10000h-FFF6h)*0.476us
        MOV     TL2,#0F6h       ;   = 2*9*0.476us
        MOV     TH2,#0FFh       ;   = 8.5us

; LAUNCH Timer2 DRIVEN CONVERSIONS...
        SETB	EA              ; enable interrupts
        SETB    EADC            ; enable ADC interrupt
        SETB    TR2             ; run Timer2

; CONTINUE WITH OTHER CODE...
AGAIN:  CPL     LED             ; blink (complement) the LED
        MOV     A,#010		; Delay length
	CALL    DELAY           ; delay 100ms
        JMP     AGAIN           ; repeat

; the micro is free to continue with other tasks (flashing the LED in
; this case) while the ADC operation is being controlled by Timer2
; and the ADC interrupt service routine.

;____________________________________________________________________
                                                         ; SUBROUTINE
DELAY:					; Delays by 10ms * A
					; 10mSec based on 2.09MHZ 
					; Core Clock 
					; i.e. default ADuC842 Clock

		MOV	R1,A		; Acc holds delay variable (1 clock)
 DLY0:		MOV	R2,#01Bh	; Set up delay loop0 (2 clocks)
 DLY1:		MOV	R3,#0FFh	; Set up delay loop1 (2 clocks)
		DJNZ	R3,$		; Dec R3 & Jump here until R3 is 0 (3 clocks)
		DJNZ	R2,DLY1         ; Dec R2 & Jump DLY1 until R2 is 0 (3 clocks)
		DJNZ	R1,DLY0		; Dec R1 & Jump DLY0 until R1 is 0 (3 clocks)
		RET			; Return from subroutine

;____________________________________________________________________

END
