I am using dsPIC33EP64MC202-I/SP to read analog inputs with the code shown below. It is using a 2.5V voltage reference on pin5 (VREF+) as ADC reference voltage. When I connect 1.1V to pin3 (AN0) ADC1BUF1 (CH0) reads 0x1F5, which converts to 1.223V (501*(2.5/1024)). AN3 (CH0) reads 0x3FF, which seem correct. What is causing the error in reading AN0?
To test the voltage reference, I changed the code to use AVDD (connected to 3.3V) as voltage reference by setting AD1CON2bits.VCFG = 0. Then AN0, ADC1BUF1 reads 0x155 = 341*(3.3/1024) = 1.099V, which is the correct answer.
So I am wondering why the 2.5V reference is not working as expected?
void initAdc() {
ANSELAbits.ANSA0 = 1; // set AN0/RA0 as analog input
ANSELAbits.ANSA1 = 1; // set AN1/RA1 as analog input
ANSELBbits.ANSB0 = 1; // set AN2/RB0 as analog input
ANSELBbits.ANSB1 = 1; // set AN3/RB1 as analog input 2.5V
TRISAbits.TRISA0 = 1; // set AN0/RA0 as input
TRISAbits.TRISA1 = 1; // set AN1/RA1 as input
TRISBbits.TRISB0 = 1; // set AN2/RB0 as input
TRISBbits.TRISB1 = 1; // set AN3/RB1 as input
/* Initialize ADC module */
AD1CON1 = 0;
AD1CON1bits.SIMSAM = 1; // Simultaneous sampling
AD1CON1bits.SSRC = 7; // Auto convert
AD1CON1bits.ASAM = 1; // auto sample
AD1CON2 = 0;
AD1CON2bits.CHPS = 2; // all four channels
AD1CON2bits.ALTS = 0; // Always uses channel input selects for Sample MUXA. MUXB not used
AD1CON2bits.VCFG = 1; // External VREFH = VREF+, VREFL = Avss
AD1CON3 = 0;
AD1CON3bits.SAMC = 0x1f; // Auto-Sample Time for 31 TAD
AD1CON3bits.ADCS = 0x0f; // TAD = 16TP
AD1CON4 = 0;
/* Assign MUXA inputs */
AD1CHS0bits.CH0SA = 3; // Channel 0 Positive Input Select for Sample MUXA bits
// Channel 0 positive input is AN3
AD1CHS0bits.CH0NA = 0; // Channel 0 negative input is VREFL
AD1CHS123 = 0; // CH1 = AN0 (voltage FB), CH2 = AN1 (I_OUT), CH3 = AN2 (temp)
/* Enable interrupts */
IFS0bits.AD1IF = 0;
IEC0bits.AD1IE = 0;
/* Enable ADC module and provide ADC stabilization delay */
AD1CON1bits.ADON = 1;
__delay_us(20);
} //initAdc