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.

TM4C123GH6PM: TM4C123 ADC analog read from two ports, comments requested.

Part Number: TM4C123GH6PM


Hello,

I need to read PD2 and PD3 as analog inputs. So I have done:

uint32_t fifo_left[4];
uint32_t fifo_right[4];

void init_adc() {

    // enable adc0
    SysCtlPeripheralEnable(SYSCTL_PERIPH_ADC0);

    // PD2, PD3
    GPIOPinTypeADC(GPIO_PORTD_BASE, GPIO_PIN_2 | GPIO_PIN_3);

    // configure two sequences
    ADCSequenceConfigure(ADC0_BASE, 1, ADC_TRIGGER_PROCESSOR, 0);
    ADCSequenceConfigure(ADC0_BASE, 2, ADC_TRIGGER_PROCESSOR, 1);

    // sequence 1 step configuration
    ADCSequenceStepConfigure(ADC0_BASE, 1, 0, ADC_CTL_CH4);
    ADCSequenceStepConfigure(ADC0_BASE, 1, 1, ADC_CTL_CH4);
    ADCSequenceStepConfigure(ADC0_BASE, 1, 2, ADC_CTL_CH4);
    ADCSequenceStepConfigure(ADC0_BASE, 1, 3, ADC_CTL_CH4 | ADC_CTL_IE | ADC_CTL_END);

    // sequence 2 step configuration
    ADCSequenceStepConfigure(ADC0_BASE, 2, 0, ADC_CTL_CH5);
    ADCSequenceStepConfigure(ADC0_BASE, 2, 1, ADC_CTL_CH5);
    ADCSequenceStepConfigure(ADC0_BASE, 2, 2, ADC_CTL_CH5);
    ADCSequenceStepConfigure(ADC0_BASE, 2, 3, ADC_CTL_CH5 | ADC_CTL_IE | ADC_CTL_END);

    // enable sequences
    ADCSequenceEnable(ADC0_BASE, 1);
    ADCSequenceEnable(ADC0_BASE, 2);

    // clear interrupt
    ADCIntClear(ADC0_BASE, 1);
    ADCIntClear(ADC0_BASE, 2);

}

and in the while loop:

        // trigger adc, wait for result, clear int, and get data to fifo
        ADCProcessorTrigger(ADC0_BASE, 1);
        while(!ADCIntStatus(ADC0_BASE, 1, false)) { }
        ADCIntClear(ADC0_BASE, 1);
        ADCSequenceDataGet(ADC0_BASE, 1, fifo_right);

        ADCProcessorTrigger(ADC0_BASE, 2);
        while(!ADCIntStatus(ADC0_BASE, 2, false)) { }
        ADCIntClear(ADC0_BASE, 2);
        ADCSequenceDataGet(ADC0_BASE, 2, fifo_left);

and it works. I am using two sequences (1 and 2) on ADC0, and sampling CH4 and CH5.

Here are my questions: Could I configure the steps of the sequence all at once? Like, read ch4 into fifo, and then end generating intterrupt?

And the second question: in the while loop, i trigger sequence 1, and then wait and then read. then for the sequence 2. could i trigger both sequences at the same time, or is there a better way of doing it?

With best regards,

Can

  • If you want 4 conversions of channel 4 followed by 4 conversions of channel 5, this is fine. You could use sequence 0 and use 8 steps, 4 with channel 4 and 4 with channel 5, then you only need to trigger the sequence once. If you want the samples to be closer in time, you can alternate the channel number on each step so that the sequencer converts channel 4 first, then channel 5 then back to 4 again and so on. You also have the option of using hardware averaging (if that is why you are taking 4 samples of each channel). If you trigger the second sequence before the first one completes the two sequences should be set to different priority levels so the order of sampling is consistent.

  • Thank you for your answer, Bob Crosby. Is there

    What options do I have for hardware averaging? I get data in an array of n (where n is sequencer depth) and then programmatically calculate the average by dividing to n. Is there a better way?

    Best regards,

    Can

  • You can configure the ADC converter to do 2, 4, 8, 16, 32 or 64 samples and store the results in a single FIFO location. Hardware averaging applies to all of the sequences on that ADC converter, so it is not as flexible as doing it in software, but it reduces CPU overhead.