I have written code for ADC as below but i am not getting ADC output in ADC DATA (O\p Register). Please find code as below. Please suggest if any error or bug you get noticed.
#include <p32xxxx.h>
unsigned int Buffervalue;
unsigned int i, j;
int main(int argc, char** argv)
{
OSCCON=0x00002200; //Configuring Oscillator
ANSELGbits.ANSG15=0; //Setting pin as analog
//Setting Calibration values to registers
AD1CAL1 = 0xB3341210;
AD1CAL2 = 0x01FFA769;
AD1CAL3 = 0x0BBBBBB8;
AD1CAL4 = 0x000004AC;
AD1CAL5 = 0x02028002;
/*Configuring AD1CON1*/
AD1CON1= 0x00000000;
/*Configuring AD1CON2*/
AD1CON2= 0x00000000;
AD1CON2bits.ADCSEL = 01; // Selecting Clock as System Clock
AD1CON2bits.ADCDIV = 0000000; // Clock Divider
AD1CON2bits.SAMC = 0x09;
/*Configuring AD1CON3*/
AD1CON3= 0x00000000;
AD1CON3bits.ADINSEL= 0x17;
AD1CON3bits.VREFSEL=0x1;
/*Configuring AD1IMOD*/
AD1IMOD= 0x00000000;
/* Configure AD1GIRGENx */
AD1GIRQEN1 = 0; // No global interrupts are used.
AD1GIRQEN2 = 0;
/* Configure AD1CSSx */
AD1CSS1 = 0; // No channel scanning is used.
AD1CSS2 = 0;
/* Configure AD1CMPCONx */
AD1CMPCON1 = 0; // No digital comparators are used. Setting the AD1CMPCONx
AD1CMPCON2 = 0; // register to ?0? ensures that the comparator is disabled. Other
AD1CMPCON3 = 0; // registers are ?don?t care?.
AD1CMPCON4 = 0;
AD1CMPCON5 = 0;
AD1CMPCON6 = 0;
/* Configure AD1FLTRx */
AD1FLTR1 = 0; // No oversampling filters are used.
AD1FLTR2 = 0;
AD1FLTR3 = 0;
AD1FLTR4 = 0;
AD1FLTR5 = 0;
AD1FLTR6 = 0;
/* Set up the trigger sources */
AD1TRG1 = 0; // Initialize all sources to no trigger.
AD1TRG2 = 0;
AD1TRG3 = 0;
AD1TRG1bits.TRGSRC0 = 1; // Set AN0 to trigger from software.
AD1TRG1bits.TRGSRC1 = 1; // Set AN1 to trigger from software.
AD1TRG1bits.TRGSRC2 = 1; // Set AN2 to trigger from software.
/* Turn the ADC on, start calibration */
PB3DIVbits.ON=1;
AD1CON1bits.ADCEN = 1;
AD1CON3bits.CAL=1;
/* Wait for calibration to complete */
while (AD1CON2bits.ADCRDY == 0);
AD1CON3bits.RQCONVRT=1;
AD1TRG2bits.TRGSRC5=00001;
while (1)
{
/* Trigger a conversion */
AD1CON3bits.GSWTRG = 1;
/* Wait the conversions to complete */
while (AD1DSTAT1bits.ARDY23== 0);
if (AD1DSTAT1bits.ARDY23 == 1)
{
/* fetch the result */
Buffervalue = AD1DATA23;
}
}
return 0;
}
#include <p32xxxx.h>
unsigned int Buffervalue;
unsigned int i, j;
int main(int argc, char** argv)
{
OSCCON=0x00002200; //Configuring Oscillator
ANSELGbits.ANSG15=0; //Setting pin as analog
//Setting Calibration values to registers
AD1CAL1 = 0xB3341210;
AD1CAL2 = 0x01FFA769;
AD1CAL3 = 0x0BBBBBB8;
AD1CAL4 = 0x000004AC;
AD1CAL5 = 0x02028002;
/*Configuring AD1CON1*/
AD1CON1= 0x00000000;
/*Configuring AD1CON2*/
AD1CON2= 0x00000000;
AD1CON2bits.ADCSEL = 01; // Selecting Clock as System Clock
AD1CON2bits.ADCDIV = 0000000; // Clock Divider
AD1CON2bits.SAMC = 0x09;
/*Configuring AD1CON3*/
AD1CON3= 0x00000000;
AD1CON3bits.ADINSEL= 0x17;
AD1CON3bits.VREFSEL=0x1;
/*Configuring AD1IMOD*/
AD1IMOD= 0x00000000;
/* Configure AD1GIRGENx */
AD1GIRQEN1 = 0; // No global interrupts are used.
AD1GIRQEN2 = 0;
/* Configure AD1CSSx */
AD1CSS1 = 0; // No channel scanning is used.
AD1CSS2 = 0;
/* Configure AD1CMPCONx */
AD1CMPCON1 = 0; // No digital comparators are used. Setting the AD1CMPCONx
AD1CMPCON2 = 0; // register to ?0? ensures that the comparator is disabled. Other
AD1CMPCON3 = 0; // registers are ?don?t care?.
AD1CMPCON4 = 0;
AD1CMPCON5 = 0;
AD1CMPCON6 = 0;
/* Configure AD1FLTRx */
AD1FLTR1 = 0; // No oversampling filters are used.
AD1FLTR2 = 0;
AD1FLTR3 = 0;
AD1FLTR4 = 0;
AD1FLTR5 = 0;
AD1FLTR6 = 0;
/* Set up the trigger sources */
AD1TRG1 = 0; // Initialize all sources to no trigger.
AD1TRG2 = 0;
AD1TRG3 = 0;
AD1TRG1bits.TRGSRC0 = 1; // Set AN0 to trigger from software.
AD1TRG1bits.TRGSRC1 = 1; // Set AN1 to trigger from software.
AD1TRG1bits.TRGSRC2 = 1; // Set AN2 to trigger from software.
/* Turn the ADC on, start calibration */
PB3DIVbits.ON=1;
AD1CON1bits.ADCEN = 1;
AD1CON3bits.CAL=1;
/* Wait for calibration to complete */
while (AD1CON2bits.ADCRDY == 0);
AD1CON3bits.RQCONVRT=1;
AD1TRG2bits.TRGSRC5=00001;
while (1)
{
/* Trigger a conversion */
AD1CON3bits.GSWTRG = 1;
/* Wait the conversions to complete */
while (AD1DSTAT1bits.ARDY23== 0);
if (AD1DSTAT1bits.ARDY23 == 1)
{
/* fetch the result */
Buffervalue = AD1DATA23;
}
}
return 0;
}