I am using the pic32mx795f512l model processor with MPLAB X 3.0 and an ICD 3 programmer. My need is to just read a voltage value that is being sent to pins AN0 and AN1, and am having trouble. I am using the following code to set up my ADC, and the analogRead function is my call to read from it. I call that function with either analogRead(0) or analogRead(1).
- Code: Select all
// ADC
//Setup for PIC32
AD1CHS = 0; // Input to AN0
AD1PCFGbits.PCFG0 = 0; // Disable digital input on AN0
AD1PCFGbits.PCFG1 = 0; // Disable digital input on AN1
// ADC
AD1CON1 = 0x84E4; // Turn on, auto sample start, auto-convert, 12 bit mode (on parts with a 12bit A/D)
AD1CON2 = 0x0404; // AVdd, AVss, int every 2 conversions, MUXA only, scan
AD1CON3 = 0x1003; // 16 Tad auto-sample, Tad = 3*Tcy
AD1CSSL = 1<<2; // Scan pot
- Code: Select all
WORD analogRead(BYTE analogPIN){
AD1CHS = analogPIN << 16;
// AD1CHS<16:19> controls which analog pin goes to the ADC
AD1CON1bits.ASAM = 0; // Clear this bit to 0 to disable auto-sampling, SAM must be set to 1 to begin sampling
AD1CON1bits.SAMP = 1; // Begin sampling
while( AD1CON1bits.SAMP ); // wait until acquisition is done
while( ! AD1CON1bits.DONE ); // wait until conversion done
return (WORD)ADC1BUF0; // result stored in ADC1BUF0
//return analogPIN;
}