ADC on PIC32MX795F512L

Discussion in 'Embedded Systems and Microcontrollers' started by jegues, Aug 29, 2013.

  1. jegues

    Thread Starter Well-Known Member

    Sep 13, 2010
    735
    43
    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,

    Code ( (Unknown Language)):
    1.  
    2. #elif defined (__32MX220F032D__) || (__32MX250F128D__)
    3. // Configuration Bit settings
    4. // SYSCLK = 48 MHz (8MHz Crystal / FPLLIDIV * FPLLMUL / FPLLODIV)
    5. // PBCLK = 48 MHz (SYSCLK / FPBDIV)
    6. // Primary Osc w/PLL (XT+,HS+,EC+PLL)
    7. // WDT OFF
    8. // Other options are don't care
    9. #pragma config FPLLMUL = MUL_24, FPLLIDIV = DIV_2, FPLLODIV = DIV_2, FWDTEN = OFF
    10. #pragma config POSCMOD = HS, FNOSC = PRIPLL, FPBDIV = DIV_1
    11. #define SYS_FREQ (48000000L)
    12. #endif
    13.  
    14. // Define setup parameters for OpenADC10 function
    15. // Turn module on | Ouput in integer format | Trigger mode auto | Enable autosample
    16. #define config1     ADC_FORMAT_INTG | ADC_CLK_AUTO | ADC_AUTO_SAMPLING_ON
    17. // ADC ref external | Disable offset test | Disable scan mode | Perform 2 samples | Use dual buffers | Use alternate mode
    18. #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
    19. // Use ADC internal clock | Set sample time
    20. #define config3     ADC_CONV_CLK_INTERNAL_RC | ADC_SAMPLE_TIME_15
    21. // Do not assign channels to scan
    22. #define configscan  SKIP_SCAN_ALL
    23.  
    24. #if defined (__32MX460F512L__) || (__32MX795F512L__) || (__32MX430F064L__) || (__32MX220F032D__) || (__32MX250F128D__)
    25. // AN4 = Temperature Sensor, AN2 = POT
    26. #define configport  ENABLE_AN4_ANA | ENABLE_AN2_ANA
    27. #elif defined (__32MX360F512L__)
    28. // AN4 = Temperature Sensor, AN5 = POT
    29. #define configport  ENABLE_AN4_ANA | ENABLE_AN5_ANA
    30. #endif
    31.  
    32. unsigned int tempSensor;    // Connected to AN4
    33. unsigned int pot;   // Connected to AN2 or AN5 (depending on device)
    34. unsigned int offset;    // Buffer offset to point to the base of the idle buffer
    35.  
    36. int main(void)
    37. {
    38.     // Configure the device for maximum performance but do not change the PBDIV
    39.     // Given the options, this function will change the flash wait states and
    40.     // enable prefetch cache but will not change the PBDIV. The PBDIV value
    41.     // is already set via the pragma FPBDIV option above..
    42.     SYSTEMConfig(SYS_FREQ, SYS_CFG_WAIT_STATES | SYS_CFG_PCACHE);
    43.  
    44.     CloseADC10();   // Ensure the ADC is off before setting the configuration
    45.  
    46.     #if defined (__32MX460F512L__) || (__32MX795F512L__) || (__32MX430F064L__) || (__32MX220F032D__) || (__32MX250F128D__)
    47.     // Configure to sample AN4 & AN2
    48.     // Use ground as neg ref for A | use AN4 for input A | use ground as neg ref for A | use AN2 for input B
    49.     SetChanADC10( ADC_CH0_NEG_SAMPLEA_NVREF | ADC_CH0_POS_SAMPLEA_AN4 |  ADC_CH0_NEG_SAMPLEB_NVREF | ADC_CH0_POS_SAMPLEB_AN2);
    50.     #elif defined (__32MX360F512L__)
    51.     // Configure to sample AN4 & AN5
    52.     // Use ground as neg ref for A | use AN4 for input A | use ground as neg ref for A | use AN5 for input B
    53.     SetChanADC10( ADC_CH0_NEG_SAMPLEA_NVREF | ADC_CH0_POS_SAMPLEA_AN4 |  ADC_CH0_NEG_SAMPLEB_NVREF | ADC_CH0_POS_SAMPLEB_AN5);
    54.     #endif
    55.  
    56.     // Configure ADC using the parameters defined above
    57.     OpenADC10( config1, config2, config3, configport, configscan );
    58.  
    59.     EnableADC10(); // Enable the ADC
    60.  
    61.     while (1)
    62.     {
    63.         while ( !mAD1GetIntFlag() )
    64.         {
    65.             // Wait for the first conversion to complete so there will be vaild data in ADC result registers
    66.         }
    67.  
    68.         // Determine which buffer is idle and create an offset
    69.         offset = 8 * ((~ReadActiveBufferADC10() & 0x01));
    70.  
    71.         // Read the result of temperature sensor conversion from the idle buffer
    72.         tempSensor = ReadADC10(offset);
    73.  
    74.         // Read the result of pot conversion from the idle buffer
    75.         pot = ReadADC10(offset + 1);
    76.  
    77.         mAD1ClearIntFlag();
    78.     }
    79.        
    80.     while(1);
    81. }
    I'll post repost the lines that I'm particular sections that I'm confused about.

    Code ( (Unknown Language)):
    1.  
    2. #if defined (__32MX460F512L__) || (__32MX795F512L__) || (__32MX430F064L__) || (__32MX220F032D__) || (__32MX250F128D__)
    3. // AN4 = Temperature Sensor, AN2 = POT
    4. #define configport  ENABLE_AN4_ANA | ENABLE_AN2_ANA
    5. #elif defined (__32MX360F512L__)
    6. // AN4 = Temperature Sensor, AN5 = POT
    7. #define configport  ENABLE_AN4_ANA | ENABLE_AN5_ANA
    8. #endif
    9.  
    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!
     
    Last edited: Aug 29, 2013
  2. Papabravo

    Expert

    Feb 24, 2006
    10,137
    1,786
    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.
     
  3. jegues

    Thread Starter Well-Known Member

    Sep 13, 2010
    735
    43
    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!
     
  4. Papabravo

    Expert

    Feb 24, 2006
    10,137
    1,786
    I'm not familiar with the OpenADC stuff, but you can certainly roll your own.
     
  5. nsaspook

    AAC Fanatic!

    Aug 27, 2009
    2,907
    2,165
  6. jegues

    Thread Starter Well-Known Member

    Sep 13, 2010
    735
    43
Loading...