Reading Analogs on a PIC32

Post
roujesky
Senior Member
2011/08/04 14:21:55
Trying to read the analogs on my PIC32 board.  Not really any luck.   Not really sure what I am doing wrong.  I have a POT hooked to PB10.  Dont see a change on AN10 :(   Can yall see me errors?  Here is the code which I lifted from the book and used....

void initADC()
{
    AD1PCFG = 0x0000;      // select all analog input pins
    AD1CON1 = 0x00E0;   // auto convert after end of sampling
    AD1CSSL = 0;        // no scanning required
    AD1CON2 = 0;        // use MUXA, AVss/AVdd used as Vref+/-
    AD1CON3 = 0x1F3F;   // max sample time = 31Tad
    AD1CON1SET = 0x8000;// turn on the ADC
   
} //initADC


int readADC( int ch)
{
    AD1CHSbits.CH0SA = ch;      // select analog input channel
    AD1CON1bits.SAMP = 1;       // start sampling
    while (!AD1CON1bits.DONE);  // wait to complete conversion
    return ADC1BUF0;            // read the conversion result
} // readADC

 void testjunkt()
{
    int i;
    for(i=0;i<16;i++)
        printf( "%d  ", readADC(i));
    printf("\n\r");
    return ;
}

thanks!

Lurch
Super Member
Re:Reading Analogs on a PIC32 2011/08/05 02:38:53
so when do you actually CALL initADC() ???
fallen
Starting Member
Re:Reading Analogs on a PIC32 2011/08/05 02:54:35
I don't c where u TRIS the pin to be an input.
 
this is how i did it.
 
void InitADC(unsigned char A2D)          // init the channel u want to use  
{
 static unsigned int WhichBit = 1;
 WhichBit = (WhichBit << A2D);
 
 TRISB |= WhichBit;
 AD1PCFG = ~WhichBit;
 
 AD1CON1bits.SSRC = 7;  // Auto start converting
 AD1CON1bits.FORM = 0;  // Give result as unsigned int
 AD1CON1bits.ASAM = 0;
 AD1CHS = 0;    
 AD1CHSbits.CH0SA = A2D;
 
 AD1CSSL = 0;    // Scan None
 
 AD1CON3bits.ADRC = 0;  // Conversion Clock Source
 AD1CON3bits.SAMC = 0x03; // Auto-Sample Time bits 
 AD1CON3bits.ADCS = 0x02; // Conversion Clock Select bits
 
 AD1CON2 = 0;    // Ref ....  
 
 AD1CON1bits.ON = 1; 
}
unsigned int GetADC(void)
{
 AD1CON1bits.SAMP = 1;
  
 while(!AD1CON1bits.DONE);
  
 return(ADC1BUF0); 

 
roujesky
Senior Member
Re:Reading Analogs on a PIC32 2011/08/05 07:41:47
Lurch:
I do call initADC(), a lot earlier.   Just didnt put the snippet here.   Sorry for the confusion. :(

More background:
Starting off, I just want to configure ALL the analogs as inputs.   I will read more analogs as the program develops.  I started out just trying to read PB10.

fallen:
I *thought* the documents stated that the analogs defaulted to all inputs....   BUT having just typed that, I am going to try that :)
I will try your code out and post results!!

thanks!!!!!
roujesky
Senior Member
Re:Reading Analogs on a PIC32 2011/08/05 13:44:15
fallen:
that works much better!  thanks!

  The only thing I see is that the analogs are off by 1.  what i mean is that when I call to get AN10, I seem to get AN11.   the hardware engineer *guarantees* that the sensor is connected to PB10.   Is there anything in the setup to could possibly cause this?
fallen
Starting Member
Re:Reading Analogs on a PIC32 2011/08/15 08:12:41
hi, been away on leave.
 
don't think i've ever use higher than 5, i will test 10 and get back to u.
 
cheers GARTH
roujesky
Senior Member
Re:Reading Analogs on a PIC32 2011/08/15 08:26:08
as usual, it looks like a self inflicted wound :(
I fixed the problem by initializing the specified analog and then reading it.  what I was doing (incorrectly?) was initializing ALL the analogs at startup and then reading individual analogs during the program execution.  (never initializing them again)   I guess my understanding of analogs was initially *WRONG* :)
fallen
Starting Member
Re:Reading Analogs on a PIC32 2011/08/16 04:44:48
i'm glad u got it wright!
 
this is always a standard part of my start up code
 
///////////////
 AD1PCFG = 0xFFFF;                                                      // Make all I/O
 TRISA = 0x0000,LATA = 0x0000,ODCA = 0x000C;
 TRISB = 0x0000,LATB = 0x0000,ODCB = 0x0000;        
 TRISC = 0x0000,LATC = 0x0000,ODCC = 0x0000;
 TRISD = 0x0000,LATD = 0x0000,ODCD = 0x0000;
 TRISE = 0x0000,LATE = 0x0000,ODCE = 0x0000;
 TRISF = 0x0000,LATF = 0x0000,ODCF = 0x0000;
 TRISG = 0x0000,LATG = 0x0000,ODCG = 0x0000;   
//////////
 
from here i set port pins as inputs or enable open drain
 
 
cheers
 
post edited by fallen - 2011/08/16 04:50:59