Reading from AN0 and AN1 on PIC32

Reading from AN0 and AN1 on PIC32

Postby eswallace » Thu Jul 28, 2016 4:35 pm

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;
}
eswallace
 
Posts: 3
Joined: Thu Jul 28, 2016 4:23 pm
PIC experience: Professional 1+ years with MCHP products

Re: Reading from AN0 and AN1 on PIC32

Postby jaromir » Sat Jul 30, 2016 2:30 pm

What exactly is problem?
Ideally, post self-contained short and complete program that exhibits your issue. Code snippets are OK, but problem may be somewhere else too.
some of my projects http://jaromir.xf.cz/
more of my projects https://hackaday.io/jaromir
User avatar
jaromir
Verified identity
 
Posts: 9
Joined: Thu Jul 28, 2016 7:16 am
PIC experience: Professional 5+ years with MCHP products


Return to ADC & Comparators

Who is online

Users browsing this forum: No registered users and 5 guests

cron