Which compiler version you are using?
- - - Updated - - -
Install xc32 1.22 version or previous version.This compiler has inbuilt peripheral libraries.Latest XC32 version doesnt have inbuilt peripheral library.
- - - Updated - - -
You will find examples how tp read ADC using peripheral libraries (plib.h)
#include <p32xxxx.h>
#include <plib.h>
//#include <xc.h>
// configuration bit settings, Fcy=72 MHz, Fpb=36 MHz
#pragma config POSCMOD=XT, FNOSC=PRIPLL
#pragma config FPLLIDIV=DIV_2, FPLLMUL=MUL_18, FPLLODIV=DIV_1
#pragma config FPBDIV=DIV_2, FWDTEN=OFF, CP=OFF, BWP=OFF
#define POT 8 // 10 k potentiometer
#define AINPUTS 0xff7f // Analog inputs POT, TSENS
void initADC( int amask)
{
AD1PCFG = amask; // select analog input pins
AD1CON1 = 0x00E0; // automatic conversion after sampling
AD1CSSL = 0; // no scanning required
AD1CON2 = 0; // use MUXA, use AVdd &AVss as Vref+/-
AD1CON3 = 0x1F3F; // Tsamp = 32 x Tad;
AD1CON1bits.ADON = 1; // turn on the ADC
} //initADC
int readADC( int ch)
{
AD1CHSbits.CH0SA = ch; // 1. select input channel
AD1CON1bits.SAMP = 1; // 2. start sampling
while (!AD1CON1bits.DONE); // 3. wait conversion complete
return ADC1BUF0; // 4. read conversion result
} // readADC
void DelayMs(unsigned int msec)
{
unsigned int tWait, tStart;
tWait=(80000000/2000)*msec;
tStart=ReadCoreTimer();
while((ReadCoreTimer()-tStart)<tWait); // wait for the time to pass
}
main ()
{
int i, a;
// initializations
initADC( AINPUTS); // initialize the ADC
// main loop
while( 1)
{
a = readADC(8); // select the POT input and convert
} // main loop
} // main
I have code for XC32 compiler but not for C32.
Here is my code to read adc values
In addition, you are perhaps not providing enough time to internal A/D capacitance to hold sampled value.AD1CON1bits.SAMP = 0;
int analogRead(char analogPIN)
{
AD1CON1 = 0x000000E0;
AD1CHS = 0;
AD1CSSL = 0;
AD1CON2 = 0x00000000;
AD1CON3 = 0x00001F00;
AD1CON1bits.ADON = 1;
AD1CHS = analogPIN << 16; // AD1CHS<16:19> controls which analog pin goes to the ADC
//AD1CON1bits.SAMP = 0;
AD1CON1bits.ASAM = 1;
delay_ms(500);
while (!IFS0bits.AD1IF);
IFS0bits.AD1IF = 0;
//AD1CON1bits.SAMP = 1;
AD1CON1bits.DONE = 0; // wait until conversion done
return ADC1BUF0; // result stored in ADC1BUF0
}
Akshay - that code is wrong is a couple of places and is conflating the initialisation and reading functions.
While setting the ASAMP bit does have the side effect of setting the SAMP bit, it also means that you will automatically start another sample after the previous one has completed. The way you have written your code, this would cause a lot of unnecessary sampling.
Also it is not a good idea to use a delay function to see when the sampling and conversion process is complete - that is what the DONE bit will tell you but is really only useful in manual sampling mode. Also you do not stop a conversion by clearing this bit - the FRM states that it will not stop any operation that is in progress.
Shrutipatel - if you can then I would suggest that you download the latest MPLABx and XC32 compiler - they are both free. Also download and read the Family Reference Manual section of the ADC - it contains a complete description of the converter, the function of each register as well as examples of how the device should be used.
Susan
But that code seems working fine.
Akshay - that code is wrong is a couple of places and is conflating the initialisation and reading functions.
While setting the ASAMP bit does have the side effect of setting the SAMP bit, it also means that you will automatically start another sample after the previous one has completed. The way you have written your code, this would cause a lot of unnecessary sampling.
Also it is not a good idea to use a delay function to see when the sampling and conversion process is complete - that is what the DONE bit will tell you but is really only useful in manual sampling mode. Also you do not stop a conversion by clearing this bit - the FRM states that it will not stop any operation that is in progress.
Shrutipatel - if you can then I would suggest that you download the latest MPLABx and XC32 compiler - they are both free. Also download and read the Family Reference Manual section of the ADC - it contains a complete description of the converter, the function of each register as well as examples of how the device should be used.
Susan
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?