/********************************************/
/*10-bit Analog to Digital Converter Code   */
/*PIC32MX795F512L-80I/PF                    */
/********************************************/

#include "xc.h"

void ADC_Init(void)
{    
    //Configure Analog Port Pins
    TRISBbits.TRISB2 = 1;
    TRISBbits.TRISB3 = 1;
    TRISBbits.TRISB9 = 1;
    TRISBbits.TRISB4 = 1;
    AD1PCFGbits.PCFG9 = 0;          //RB9/AN9 (pin 33)
    AD1PCFGbits.PCFG4 = 0;          //RB4/AN4 (pin 21)

    AD1CON1bits.FORM = 4;           //Integer 32-bit
    AD1CON1bits.SSRC = 7;           //Conversion Trigger Source Select
    AD1CSSL = 0;                    //No scan
    //ADC Conversion Clock Source bit
    AD1CON3bits.ADRC = 1;           //0=PBCLK, 1=FRC
    //Auto-Sample Time bits 
    AD1CON3bits.SAMC = 20;          //only used if AD1CON1bits.SSRC = 7 
    AD1CON3bits.ADCS = 3;           //ADC Conversion Clock Select bits, only used if AD1CON3bits.ADRC = 0
    AD1CON2bits.VCFG = 2;           //Voltage Reference Configuration bits
    AD1CON2bits.SMPI = 0;           //Samples per interrupt
    AD1CON1bits.CLRASAM = 1;        //Stop conversions when the first ADC interrupt is generated
    AD1CON1bits.ADON = 1;
    
    return;  
}