;############################################################################
;# TITLE   "ADCInt General Purpose Library Checking Software"
;#
;#
;#      Program:        ADCInt.P18.Ex.txt
;#      Version:        1.0
;#      Revision Date:  May 13 2003
;#      Author:         B.K. Anantha Ramu
;#
;#
;# This Program demonstrates use of ADC general purpose library module for interrupt mode.
;############################################################################


       	list    p=18C452       
;       list    p=18f6620
       	#include   <P18C452.INC>
;       #include   <P18f6620.INC>
        #include <ADCInt.inc>           ;This file contains macros of ADC library module
;------------------------------------------------------------------------------                     

    	CONSTANT    TIMERSET=0xffa0 	;Constant to set Timer1 interrupt rate   

StartHere   CODE 0x0
        goto    Start

IntHighPriorserv CODE 0x8
        goto    HighPriorISR

IntLowPriorserv     CODE 0X18
        goto    LowPriorISR
;-------------------------------------------------------------------------------
MAIN        CODE
Start    
        nop                             ;User may have his code here

;------------------------------------------------------------------------------
;ADC is configured using macro mADCIntInit. This macro takes one or two arguments
;depending upon the processor used.  
;For the processors not having ADCON2 register, the ADC port configuration name 
;is the only argument required. Chose the appropriate port configuration name
;from Table 1 of ADCInt.ReadMe.pdf.
;For processors having ADCON2 register,this macro requires two arguments. The 
;first argument is the port configuration name & the second argument is the ADC
;reference voltage configuration name. Select the port configuration name from 
;Table 2  & the ADC reference voltage configuration name from Table 3 of file
;ADCInt.ReadMe.pdf.
; 
;           mADCIntInit ADCPORTCONFIG14,ADCREFERENCECONFIG2     
            mADCIntInit ADCPORTCONFIG9    ;ADC configured as per compile time options
                                          ; & interrupts enabled.
;mADCIntInit sets channel 0 as default ADC channel.
;If user wants to use a different channel, he can select
;the required channel by invoking macro mADCIntChannelSelect. The ADC channel being
;used is stored in location vADCIntChannelNumber_A. User can read this location  
;and use this for channel dependent processing.
;------------------------------------------------------------------------------
;        mSetADCIntHighPriority          ;Sets high priority for ADC interrupt              
;------------------------------------------------------------------------------
        nop                              ;User may have his code here.     

;------------------------------------------------------------------------------
        mADCIntChannelSelect D'6'       ;ADC channel 6 is selected.
                                        ;The selected channel number is stored
                                        ;in location vADCIntChannelNumber_A. User 
                                        ;may use this information for channel 
                                        ;dependent processing of the ADC
                                        ;result
;------------------------------------------------------------------------------   
        call    ADCIntAcquisitionTime   ;Before starting AD conversion a delay
                                        ;of acquisition time is provided. 
                                        ;User enters the required delay
                                        ;in MpAM.
;------------------------------------------------------------------------------
;Timer1 initialisation: Read/write in two 8 bit operations, prescaler 1:1, 
;internal clock, timer off
        movlw   B'00000000'
        movwf   T1CON
;Timer interrupt setting
        bcf     PIR1,TMR1IF 		;Clear interrupt flag at the beginning. 
        bsf     PIE1,TMR1IE 		;Enable Timer1 interrupt
        movlw   HIGH(TIMERSET)  	;
        movwf   TMR1H           	;Timer1 preset
        movlw   LOW(TIMERSET)   	;
        movwf   TMR1L           	; 

        bsf         T1CON,TMR1ON        ;Switches on timer1.


;-----------------------------------------------------------------------------
;        users code
;-------------------------------------------------------------------------------
ChkResult  
      

        call     ADCIntRead     ;This reads the buffer & puts the result into
                                ;locations vADCResultHigh & vADCResultLow, when
                                ;selected ADC resolution is 10 bits. If selected 
                                ;ADC resolution is 8 bits it reads the buffer &
                                ;put the result into location vADCResultLow.        
;-------------------------------------------------------------------------------
;        users code
;-------------------------------------------------------------------------------
        goto     ChkResult    

;-------------------------------------------------------------------------------
;Timer1 interrupt service routine
TimerISR:
        btfss   PIR1,TMR1IF
        return
        movlw   HIGH(TIMERSET)  ;
        movwf   TMR1H           ;Timer1 preset
        movlw   LOW(TIMERSET)   ;
        movwf   TMR1L           ;
        mADCIntStart            ;ADC conversion started with this macro
        bcf     PIR1,TMR1IF
        return 1
;------------------------------------------------------------------------------- 

HighPriorISR
;-------------------------------------------------------------------------------
;        User may have instructions here for context saving.
;        
;-------------------------------------------------------------------------------
        
        call TimerISR
        call ADCIntISR                  ;This checks whether ADC interrupt flag is
                                        ;set or not. If set it will clear this flag. 
                                        ;& sets ADCIntResultReady flag and
                                        ;returns. If ADC interrupt interrupt flag
                                        ;is not set it simply returns from the 
                                        ;routine.
;-------------------------------------------------------------------------------                              

NoADCInterrupt  
;       Users instructions to process other interrupts.
;-------------------------------------------------------------------------------
;       User may have instructions here for context restore.
;       
;------------------------------------------------------------------------------- 
        retfie
;-------------------------------------------------------------------------------

LowPriorISR    
;       users  ISR for Low priority interrupts.
        retfie
;-------------------------------------------------------------------------------

    END
