This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

TIVA C TM4C123G Launchpad Multiple ADC configure

Dear all,

I want to use 2 ADCs.The code I am writing is attached. Both the ADC's are reading same. kindly help.

#include <stdint.h>
#include <stdbool.h>
#include "inc/hw_gpio.h"
#include "inc/hw_types.h"
#include "inc/hw_memmap.h"
#include "driverlib/sysctl.h"
#include "driverlib/pin_map.h"
#include "driverlib/gpio.h"
#include "driverlib/pwm.h"
#include "driverlib/adc.h"
#include "driverlib/sysctl.h"
unsigned long ulPeriod;
uint32_t pui32ADC0Value[1],a[1];
double d,x;
double v_cos,v_sin,sum,v_cos2,v_cos_act;
double Ts,k1,k2;
int main(void)
{
v_cos=0;
pui32ADC0Value[0]=1000;


   SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC |   SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ);

    SysCtlPeripheralEnable(SYSCTL_PERIPH_ADC0);

    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE);

    GPIOPinTypeADC(GPIO_PORTE_BASE, GPIO_PIN_3|GPIO_PIN_2);

    ADCSequenceConfigure(ADC0_BASE, 3, ADC_TRIGGER_PROCESSOR, 0);


    ADCSequenceStepConfigure(ADC0_BASE, 2, 0, ADC_CTL_CH0 | ADC_CTL_IE |
                                 ADC_CTL_END);
    ADCSequenceStepConfigure(ADC0_BASE, 3, 0, ADC_CTL_CH1 | ADC_CTL_IE |
                             ADC_CTL_END);


    ADCSequenceEnable(ADC0_BASE, 3);
    ADCSequenceEnable(ADC0_BASE, 2);

    ADCIntClear(ADC0_BASE, 3);
    ADCIntClear(ADC0_BASE, 2);
    while(1)
    {

        ADCProcessorTrigger(ADC0_BASE, 3);

        while(!ADCIntStatus(ADC0_BASE, 3, false))
        {
        }

        ADCIntClear(ADC0_BASE, 3); //put breakpoint here

        ADCSequenceDataGet(ADC0_BASE, 3, pui32ADC0Value);
        v_sin=(2.0*pui32ADC0Value[0])/4095.0;

        ADCProcessorTrigger(ADC0_BASE, 2);

                while(!ADCIntStatus(ADC0_BASE, 2, false))
                {
                }

                ADCIntClear(ADC0_BASE, 2); 

                ADCSequenceDataGet(ADC0_BASE, 2, a);
    }
}

  • Hi Prashant,

    I think the only problem is that you forgot to do ADCSequenceConfigure() for sequencer 2.
    Also do note that after enabling the ADC peripheral clock you need to wait a few cycles before accessing the registers. You do that by configuring the GPIO in between but I was not sure you were aware of that
  • uint32_t pui32ADC0Value[2];
    int main(void)
    {
    	SysCtlClockSet(SYSCTL_SYSDIV_5|SYSCTL_USE_PLL|SYSCTL_OSC_MAIN|SYSCTL_XTAL_16MHZ);
        SysCtlPeripheralEnable(SYSCTL_PERIPH_ADC0);
        SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE);
        GPIOPinTypeADC(GPIO_PORTE_BASE, GPIO_PIN_3|GPIO_PIN_2);
        ADCSequenceDisable(ADC0_BASE, 0);
        ADCSequenceConfigure(ADC0_BASE, 0, ADC_TRIGGER_PROCESSOR, 0);
        ADCSequenceStepConfigure(ADC0_BASE, 0, 0, ADC_CTL_CH0);
        ADCSequenceStepConfigure(ADC0_BASE,0, 1, ADC_CTL_CH1 | ADC_CTL_IE |
                                 ADC_CTL_END);
        ADCSequenceEnable(ADC0_BASE, 0);
        while(1)
        {
        	ADCIntClear(ADC0_BASE, 0);
            ADCProcessorTrigger(ADC0_BASE, 0);
            while(!ADCIntStatus(ADC0_BASE, 0, false))
            {
            }
            ADCSequenceDataGet(ADC0_BASE, 0, pui32ADC0Value);
        }
    }

    Hii Luis

    As per your instruction, i have changed the code. I am using sequencer 0 for both adc inputs. but still getting same output in both the channels even if one is not even fed with input. kindly see the attached code.

  • I missed this before,
    When using sequencer 0 you have use a table of size 8 even if you are just using 2 samples. So pui32ADC0Value should have size 8 instead of 2.

    Now, how are you reading the data? How do you see it?
    You know that pui32ADC0Value[0] should hold the data for CH0 and pui32ADC0Value[1] the data for CH1, correct?

    Try to, instead of having a infinite loop of values, do just 1 reading.

    Also, don't leave any input floating, have CH0 connected to GND and CH1 to 3.3V (with a resistor) for example. This is just to know exactly the values that the channels should be reading

  • Hello Luis,

    The pui32ADC0Value can have the number of entry as many as there are conversions and not the number of steps. The API reads as long as the FIFO has data.

    However your second point is valid. ADC does not like floating pins... Connect one to GND and other to 3.3V to ensure that are at a known level for conversions.

    Regards
    Amit
  • Amit Ashara said:
    The pui32ADC0Value can have the number of entry as many as there are conversions and not the number of steps. The API reads as long as the FIFO has data.



    The shame!!!

    I actually said that because of this is what's in Tivaware temperature sensor example.

    // This array is used for storing the data read from the ADC FIFO. It
    // must be as large as the FIFO for the sequencer in use. This example
    // uses sequence 3 which has a FIFO depth of 1. If another sequence
    // was used with a deeper FIFO, then the array size must be changed.


    In the Tivaware peripheral driver user guide does indeed say: "The number of samples available in the hardware FIFO are copied into the buffer, which is assumed to be large enough to hold that many samples"


    Thank you for the correction Amit

  • Thanks Luis and Amit. code working.
  • Hello Luis,

    That is OK. Sometimes comments can be read two or more ways

    Regards
    Amit
  • Hello Prashant

    What was the issue?

    Regards
    Amit