void initAdc1(void)
{
AD1CON1bits.ADON = 0;
AD1CON1bits.AD12B = 0; // Select 10-bit mode 1=12bit
AD1CON2bits.CHPS = 3; // Select 4-channel mode
AD1CON1bits.SIMSAM = 1; // Enable Simultaneous Sampling
AD1CON1bits.ADDMABM = 1; // DMA buffer written in order of conversion
AD1CON2bits.ALTS = 1; // Enable Alternate Input Selection
AD1CON2bits.SMPI = 1; // Select 1 conversion between interrupt
AD1CON1bits.ASAM = 1; // Enable Automatic Sampling 0=SAMP must be set before sampling
AD1CON1bits.SSRC = 2; // Timer3 generates SOC trigger
AD1CON2bits.CSCNA = 0; // No input scan
// AD1CON3bits.ADRC=0; // ADC Clock is derived from Systems Clock
// AD1CON3bits.ADCS = 63; // ADC Conversion Clock Tad=Tcy*(ADCS+1)= (1/40M)*64 = 1.6us (625Khz)
// Initialize MUXA Input Selection
AD1CHS0bits.CH0SA = 8; // Select AN8 for CH0 +ve input
AD1CHS0bits.CH0NA = 0; // Select VREF- for CH0 -ve input
AD1CHS123bits.CH123SA = 0; // Select CH1 +ve = AN0, CH2 +ve = AN1, CH3 +ve = AN2
AD1CHS123bits.CH123NA = 0; // Select VREF- for CH1/CH2/CH3 -ve inputs
// Initialize MUXB Input Selection
AD1CHS0bits.CH0SB = 11; // Select AN11 for CH0 +ve input
AD1CHS0bits.CH0NB = 0; // Select VREF- for CH0 -ve input
AD1CHS123bits.CH123SB = 1; // Select CH1 +ve = AN3, CH2 +ve = AN4, CH3 +ve = AN5
IPC3bits.AD1IP = 7; // Set Priority to highest level 7
IFS0bits.AD1IF = 0; // Clear the A/D interrupt flag bit
IEC0bits.AD1IE = 1; // Enable A/D interrupt
AD1CON1bits.ADON = 1;
}