;##################################################################################
;# TITLE   "OVERSAMPLING General Purpose Library Test Software"
;#
;#
;#      Program:        OVER.ASM
;#      Version:        1.0
;#      Revision Date:
;#      Author:         CG
;#
;#
;# Program demonstrates use of A/D Oversampling general purpose library module.
;##################################################################################

#define        	PIC16					;set the test to use PIC16

#ifdef          PIC16
        list p=16f877
        include <P16f877.INC>
#endif
      
        include "ADOver.inc"

;##################################################################################
;##############################  Definitions ######################################
;##################################################################################


#define	P16RAM0_INIT		0x20    	;RAM Bank0 initial address

#define	DELAY50USEC		.12

#define	DELAY1300MS		.7		;Value to get about 1.3seconds Long Delay
#define SETLINGDELAY            .1              ;D/A long .2sec delay


;##################################################################################
;###################### Test Related Variables ####################################
;##################################################################################


_ADOVERTESTDATA		UDATA

delayCounter	        RES		1	;delay counter used in SPI transmission and Reception
testdata		RES		1	;

DirUpper		res		1	;Data input Register - Upper (mirrors internal reg.)
DirHigh			res		1	;Data input Register - High (mirrors internal reg.)
DirLow			res		1	;Data input Register - Low (mirrors internal reg.)

t0			res		1	;Long delay timing
t1			res		1	;Long delay timing
t2			res		1	;Long delay timing

ascLow			res		1	;
ascHigh			res		1	;

;sampleCounter		res		1	;

;##################################################################################
;############################# Test Code Start ####################################
;##################################################################################


_ADOverTestStartHere         CODE    0x000

        goto        Main

_ADOverTestMainCode          CODE

;*********************** MAIN *************************************

Main
    	call        CpuInit			;CPU Registers Initialization
	call        ADOverInit                  ;oversampling initialization
	call	    SetlingDelay	        ;initial setling time
mainLoop
	call	    ADOverGetData		;New data obtained from A/D with Oversampling
	goto	    mainLoop			;Loop

;******************************************************************

CpuInit:
	movlw	    P16RAM0_INIT                ;points to the beginning of RAM
	movwf	    FSR
ADOverCleanLoop:
	clrf	    INDF                        ;clear the RAM
	incf	    FSR,f
	btfss       FSR,7
	goto	    ADOverCleanLoop
adOverInitAD
	banksel	    ADCON1                      ;Init A/D Registers
	bcf	    ADCON1,PCFG0		;Use AN0(RA0) Analog Input
	bsf	    ADCON1,PCFG1
	bsf	    ADCON1,PCFG2
	bsf	    ADCON1,PCFG3

	banksel	    ADCON0
        bcf	    ADCON0,CHS0		        ;choose channel 0
	bcf	    ADCON0,CHS1
	bcf	    ADCON0,CHS2
        return

SetlingDelay:
        movlw   SETLINGDELAY
        goto    PutDelay
        
LongDelay:
	movlw	    DELAY1300MS
PutDelay
        banksel     t0
	movwf	    t2
	clrf	    t1
	clrf	    t0
LlD
	decfsz	    t0,f
	goto	    LlD
	decfsz	    t1,f
	goto	    LlD
	decfsz	    t2,f
	goto	    LlD
	return


	END
