ADC on PIC32MX795F512L

Thread Starter

jegues

Joined Sep 13, 2010
733
Hi guys,

I'm looking at some of the code examples and reference manuals for the ADC for the PIC32MX, and I have some questions.

Reference the code given below,

Rich (BB code):
#elif defined (__32MX220F032D__) || (__32MX250F128D__)
// Configuration Bit settings
// SYSCLK = 48 MHz (8MHz Crystal / FPLLIDIV * FPLLMUL / FPLLODIV)
// PBCLK = 48 MHz (SYSCLK / FPBDIV)
// Primary Osc w/PLL (XT+,HS+,EC+PLL)
// WDT OFF
// Other options are don't care
#pragma config FPLLMUL = MUL_24, FPLLIDIV = DIV_2, FPLLODIV = DIV_2, FWDTEN = OFF
#pragma config POSCMOD = HS, FNOSC = PRIPLL, FPBDIV = DIV_1
#define SYS_FREQ (48000000L)
#endif

// Define setup parameters for OpenADC10 function
// Turn module on | Ouput in integer format | Trigger mode auto | Enable autosample
#define config1     ADC_FORMAT_INTG | ADC_CLK_AUTO | ADC_AUTO_SAMPLING_ON
// ADC ref external | Disable offset test | Disable scan mode | Perform 2 samples | Use dual buffers | Use alternate mode
#define config2     ADC_VREF_AVDD_AVSS | ADC_OFFSET_CAL_DISABLE | ADC_SCAN_OFF | ADC_SAMPLES_PER_INT_2 | ADC_ALT_BUF_ON | ADC_ALT_INPUT_ON
// Use ADC internal clock | Set sample time
#define config3     ADC_CONV_CLK_INTERNAL_RC | ADC_SAMPLE_TIME_15
// Do not assign channels to scan
#define configscan  SKIP_SCAN_ALL

#if defined (__32MX460F512L__) || (__32MX795F512L__) || (__32MX430F064L__) || (__32MX220F032D__) || (__32MX250F128D__)
// AN4 = Temperature Sensor, AN2 = POT
#define configport  ENABLE_AN4_ANA | ENABLE_AN2_ANA
#elif defined (__32MX360F512L__)
// AN4 = Temperature Sensor, AN5 = POT
#define configport  ENABLE_AN4_ANA | ENABLE_AN5_ANA
#endif

unsigned int tempSensor;    // Connected to AN4
unsigned int pot;   // Connected to AN2 or AN5 (depending on device)
unsigned int offset;    // Buffer offset to point to the base of the idle buffer

int main(void)
{
    // Configure the device for maximum performance but do not change the PBDIV
    // Given the options, this function will change the flash wait states and
    // enable prefetch cache but will not change the PBDIV. The PBDIV value
    // is already set via the pragma FPBDIV option above..
    SYSTEMConfig(SYS_FREQ, SYS_CFG_WAIT_STATES | SYS_CFG_PCACHE);

    CloseADC10();   // Ensure the ADC is off before setting the configuration

    #if defined (__32MX460F512L__) || (__32MX795F512L__) || (__32MX430F064L__) || (__32MX220F032D__) || (__32MX250F128D__)
    // Configure to sample AN4 & AN2
    // Use ground as neg ref for A | use AN4 for input A | use ground as neg ref for A | use AN2 for input B
    SetChanADC10( ADC_CH0_NEG_SAMPLEA_NVREF | ADC_CH0_POS_SAMPLEA_AN4 |  ADC_CH0_NEG_SAMPLEB_NVREF | ADC_CH0_POS_SAMPLEB_AN2);
    #elif defined (__32MX360F512L__)
    // Configure to sample AN4 & AN5
    // Use ground as neg ref for A | use AN4 for input A | use ground as neg ref for A | use AN5 for input B
    SetChanADC10( ADC_CH0_NEG_SAMPLEA_NVREF | ADC_CH0_POS_SAMPLEA_AN4 |  ADC_CH0_NEG_SAMPLEB_NVREF | ADC_CH0_POS_SAMPLEB_AN5);
    #endif

    // Configure ADC using the parameters defined above
    OpenADC10( config1, config2, config3, configport, configscan );

    EnableADC10(); // Enable the ADC

    while (1)
    {
        while ( !mAD1GetIntFlag() )
        {
            // Wait for the first conversion to complete so there will be vaild data in ADC result registers
        }

        // Determine which buffer is idle and create an offset
        offset = 8 * ((~ReadActiveBufferADC10() & 0x01));

        // Read the result of temperature sensor conversion from the idle buffer
        tempSensor = ReadADC10(offset);

        // Read the result of pot conversion from the idle buffer
        pot = ReadADC10(offset + 1);

        mAD1ClearIntFlag();
    }
        
    while(1);
}
I'll post repost the lines that I'm particular sections that I'm confused about.

Rich (BB code):
#if defined (__32MX460F512L__) || (__32MX795F512L__) || (__32MX430F064L__) || (__32MX220F032D__) || (__32MX250F128D__)
// AN4 = Temperature Sensor, AN2 = POT
#define configport  ENABLE_AN4_ANA | ENABLE_AN2_ANA
#elif defined (__32MX360F512L__)
// AN4 = Temperature Sensor, AN5 = POT
#define configport  ENABLE_AN4_ANA | ENABLE_AN5_ANA
#endif
I don't understand what this section of code is for? It look like there is if statements mixed with define statements? Usually with if statements we don't see the # before it either.

In my past experience with microcontrollers, I've configured the appropriate ports for different modules by simply writing the appropriate configuration bits to the appropriate configuration registers for that module. Often times it also involved configuring a particular register within the general I/O registers to be used with a module. (i.e. certain general I/O register must be configured as input or output in order for the module to function correctly)

All these #define statements look like something thats usually tucked away in a seperate header file, no?

The part that is confusing for me is that they are not writing to any of the ADC control registers such as ADCON etc... Is this being done by the OpenADC function? I really don't understand what the OpenADC function even does.

Thanks again for any help!
 

Attachments

Last edited:

Papabravo

Joined Feb 24, 2006
21,225
Whoa hold on there chief.

The #if statements are not actual code being generated. They are preprocessor statements that select text substitutions to be performed by the compiler BEFORE the actual compilation and code generation. Read K&R on the use of #define and #if and above all don't confuse #if with just plain if.
 

Thread Starter

jegues

Joined Sep 13, 2010
733
Whoa hold on there chief.

The #if statements are not actual code being generated. They are preprocessor statements that select text substitutions to be performed by the compiler BEFORE the actual compilation and code generation. Read K&R on the use of #define and #if and above all don't confuse #if with just plain if.
Hi Papabravo! :)

Thanks for the clarification regarding the difference between the preprocessor statments and the actual code! (it's been quite sometime since I've done any of this stuff!)

Do you know anything about this OpenADC stuff? I'm looking through the reference manual and its not mentioned anywhere.

I'm sure I could get the ADC running by just properly configuring the appropriate control registers as per the reference manual.

What do you think?

Cheers!
 
Top