Code/C-Code/dspic.c
/*******************************************************
This function configures the ADC module. 
Called from the main() function.
Value of each field of ADC registers is set here.
ADC is configured to sample 4 channel simultaneously
*******************************************************/
This function configures the ADC module.
Called from the main() function.
Value of each field of ADC registers is set here.
ADC is configured to sample 4 channel simultaneously
This function configures the ADC module . Called from the main () function . Value of each field of ADC registers is set here . ADC is configured to sample 4 channel simultaneously
void adc_config()
{
	AD1CON1bits.ADSIDL  = 1;		
	AD1CON1bits.ADDMABM = 1;		
	AD1CON1bits.FORM    = 0;		
	AD1CON1bits.SSRC    = 7;		
	AD1CON1bits.ASAM    = 1;		
	AD1CON1bits.AD12B   = 0;		
	AD1CON1bits.SIMSAM  = 1;		
	AD1CON2bits.VCFG    = 0;		
	AD1CON2bits.CSCNA   = 0;		
	AD1CON2bits.CHPS    = 2;		
	AD1CON2bits.SMPI    = 15;		
	AD1CON2bits.BUFM    = 0;		
	AD1CON2bits.ALTS    = 0;		
	AD1CON3bits.ADRC    = 0;		
	AD1CON3bits.ADCS    = 3;		
	AD1CON3bits.SAMC    = 2;		
	AD1CON4bits.DMABL = 4;			
	AD1CHS123bits.CH123SA = 0;		
	AD1CHS123bits.CH123NA = 3;		
	AD1CHS0bits.CH0SA = 12;			
	AD1CSSL = 0;			
	AD1PCFGL=0xFFFF;				
	AD1PCFGLbits.PCFG0  = 0;		
	AD1PCFGLbits.PCFG1  = 0;		
	AD1PCFGLbits.PCFG2  = 0;		
	AD1PCFGLbits.PCFG9  = 0;		
	AD1PCFGLbits.PCFG10 = 0;		
	AD1PCFGLbits.PCFG11 = 0;		
	AD1PCFGLbits.PCFG12 = 0;		
    IFS0bits.AD1IF = 0;				
  	IEC0bits.AD1IE = 0;				
    AD1CON1bits.ADON = 1;			
}
void adc_config ( ) { AD1CON1bits . ADSIDL = 1 ; AD1CON1bits . ADDMABM = 1 ; AD1CON1bits . FORM = 0 ; AD1CON1bits . SSRC = 7 ; AD1CON1bits . ASAM = 1 ; AD1CON1bits . AD12B = 0 ; AD1CON1bits . SIMSAM = 1 ; AD1CON2bits . VCFG = 0 ; AD1CON2bits . CSCNA = 0 ; AD1CON2bits . CHPS = 2 ; AD1CON2bits . SMPI = 15 ; AD1CON2bits . BUFM = 0 ; AD1CON2bits . ALTS = 0 ; AD1CON3bits . ADRC = 0 ; AD1CON3bits . ADCS = 3 ; AD1CON3bits . SAMC = 2 ; AD1CON4bits . DMABL = 4 ; AD1CHS123bits . CH123SA = 0 ; AD1CHS123bits . CH123NA = 3 ; AD1CHS0bits . CH0SA = 12 ; AD1CSSL = 0 ; AD1PCFGL = 0xFFFF ; AD1PCFGLbits . PCFG0 = 0 ; AD1PCFGLbits . PCFG1 = 0 ; AD1PCFGLbits . PCFG2 = 0 ; AD1PCFGLbits . PCFG9 = 0 ; AD1PCFGLbits . PCFG10 = 0 ; AD1PCFGLbits . PCFG11 = 0 ; AD1PCFGLbits . PCFG12 = 0 ; IFS0bits . AD1IF = 0 ; IEC0bits . AD1IE = 0 ; AD1CON1bits . ADON = 1 ; }
This function configures the ADC module.
This function configures the ADC module .
// stop in Idle mode // Fill DMA buffers in order of conversion // Data Output Format: Integer // Sample Clock Source: Internal clock starts conversion // ADC Sample Control: Sampling begins immediately after conversion // 10-bit 4 channel ADC operation // Simultaneous sampling 4 channels // AVdd and AVss are used for reference voltage // Do not scan inputs // Converts CH0, CH1, CH2, CH3 // Increments DMA buffer address/interrupt after every 16 sample/conversion // Buffers starts filling at 0x0 // Alternate selection disabled // ADC Clock is derived from Systems Clock // ADC Conversion Clock Tad=Tcy*(ADCS+1)= 1/(6Mhz); Tad = 4Tcy // ADC Sampling time =  2*Tad // ADC Conversion Time for 10-bit Tc=12*Tab // Total sampling rate = 2Tad + 4*12Tad -- since converting 4 channels simultaneously // Sampling freq =  30 Khz // Allocating 16 word buffer for each analog input 	 // Necessary to give positive input and negative input for Ch1,2,3 only. // Ch1 positive input for AN0, Ch2 positive input in AN1, Ch3 +ve input is AN2 // Ch1 negative input is AN9,Ch2 negative input is AN10,Ch3 negative input is AN11  // Channel 0 postive input is AN12 //AD1CSSL: A/D Input Scan Selection Register // Skip all channel scan //AD1PCFGH/AD1PCFGL: Port Configuration Register // setting all pins as digital // AN0 as Analog Input for CH1 Pin 2 // AN1 as Analog Input for CH2 Pin 3 // AN2 as Analog Input for CH3 Pin 4 // AN9 as Analog -ve Input as gnd for CH1 Pin 26 // AN10 as Analog -ve Input as gnd for CH2 Pin 25 // AN11 as Analog -ve Input as gnd for CH3 Pin 24 // AN12 as Analog Input Pin 23 for CH0 // Clear the A/D interrupt flag bit // Disable A/D interrupt  // Turn on the A/D converter