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.

ADC output

Other Parts Discussed in Thread: TM4C123GH6PM

HI I am using TM4C123GH6PM device for ADC. the following code produce 0 output value from function generator which is set for 5. Can anyone clarify the mistake? 

#include <stdbool.h>
#include <stdint.h>
#include "inc/hw_memmap.h"
#include "driverlib/adc.h"
#include "driverlib/gpio.h"
#include "driverlib/sysctl.h"
#include "driverlib/adc.h"
#include "inc/hw_types.h"
#include "driverlib/debug.h"

main(void)
{

uint32_t pui32ADC0Value[1];
uint32_t pui32ADC1Value[1];

volatile uint32_t ui32CurrentAvg0;
volatile uint32_t ui32CurrentAvg1;
volatile uint32_t ui32Voltage0;
volatile uint32_t ui32Voltage1;
volatile uint32_t ui32PowerGe;
volatile uint32_t ui32PowerGr;
volatile uint32_t ui32PowerAc;
volatile uint32_t ui32PowerDc;
volatile uint32_t ui32PowerLoad;
volatile uint32_t ui32PowerDemand;
volatile uint32_t ui32Inv;
volatile uint32_t ui32Rec;
volatile uint32_t ui32Grid;
volatile uint32_t ui32App;

SysCtlClockSet(SYSCTL_SYSDIV_10 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN| SYSCTL_XTAL_16MHZ);                                                                         
SysCtlPeripheralEnable(SYSCTL_PERIPH_ADC0);
SysCtlPeripheralEnable(SYSCTL_PERIPH_ADC1);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE);

ADCReferenceSet(ADC0_BASE, ADC_REF_INT);
ADCReferenceSet(ADC1_BASE, ADC_REF_INT);                              
GPIOPinTypeADC(GPIO_PORTE_BASE, GPIO_PIN_1);
GPIOPinTypeADC(GPIO_PORTE_BASE, GPIO_PIN_2);

ADCSequenceDisable(ADC0_BASE, 3);
ADCSequenceConfigure(ADC0_BASE, 3, ADC_TRIGGER_PROCESSOR, 0);
ADCSequenceDisable(ADC1_BASE, 3);
ADCSequenceConfigure(ADC1_BASE, 3, ADC_TRIGGER_PROCESSOR, 0);
ADCSequenceStepConfigure(ADC0_BASE, 3, 0,ADC_CTL_CH8 | ADC_CTL_IE | ADC_CTL_END);
ADCSequenceStepConfigure(ADC1_BASE, 3, 0,ADC_CTL_CH8 | ADC_CTL_IE | ADC_CTL_END);                                                       
ADCSequenceEnable(ADC0_BASE, 3);  
ADCIntClear(ADC0_BASE, 3);    
ADCSequenceEnable(ADC1_BASE, 3);
ADCIntClear(ADC1_BASE, 3);

while (1) {
ADCProcessorTrigger(ADC0_BASE, 3);
ADCProcessorTrigger(ADC1_BASE, 3);

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

ADCIntClear(ADC0_BASE, 3);
ADCIntClear(ADC1_BASE, 3);
ADCSequenceDataGet(ADC0_BASE, 3, pui32ADC0Value);
ADCSequenceDataGet(ADC1_BASE, 3, pui32ADC1Value);

}

}

  • Hello M. Mary,

    Channel 8 is not PE1 or PE2. It is PE5. So you would need to correct the channel mapping

    Please have a look at the ADC Chapter of the Data Sheet for Signal Description. The Column "Pin Name" has the analog channel and Pin Mux/Assignment of which Physical Port-Pin it maps to

    Regards

    Amit

  • Thank you Amit.

    I try to configure a simple program to get external input from PE5 by referring ADC chapter . But that also produces same 0 output. I don't know what is the problem.The code is,

    #include <stdbool.h>
    #include <stdint.h>
    #include "inc/hw_memmap.h"
    #include "driverlib/adc.h"
    #include "driverlib/gpio.h"
    #include "driverlib/sysctl.h"
    #include "driverlib/adc.h"
    #include "inc/hw_types.h"
    #include "driverlib/debug.h"

    main(void) {

    uint32_t pui32ADC0Value[1];

    SysCtlClockSet(SYSCTL_SYSDIV_10 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN| SYSCTL_XTAL_16MHZ);                                                                          
    SysCtlPeripheralEnable(SYSCTL_PERIPH_ADC0);
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE);

    GPIOPinTypeADC(GPIO_PORTE_BASE, GPIO_PIN_5);
    GPIOPinTypeGPIOInput(GPIO_PORTE_BASE, GPIO_PIN_5);
    GPIODirModeSet(GPIO_PORTE_BASE, GPIO_PIN_5,GPIO_DIR_MODE_IN);                           
    GPIOIntEnable(GPIO_PORTE_BASE, GPIO_PIN_5);

    ADCIntEnable(ADC0_BASE, 3);
    ADCSequenceDisable(ADC0_BASE, 3);
    ADCSequenceConfigure(ADC0_BASE, 3, ADC_TRIGGER_PROCESSOR, 0);
    ADCSequenceStepConfigure(ADC0_BASE, 3, 0,ADC_CTL_CH8 | ADC_CTL_IE | ADC_CTL_END);
                                                       

    ADCSequenceEnable(ADC0_BASE, 3);
    while (1) {
    ADCProcessorTrigger(ADC0_BASE, 3);  
    while (!ADCIntStatus(ADC0_BASE, 3, false))
    {
    }

    ADCIntClear(ADC0_BASE, 3);
    ADCSequenceDataGet(ADC0_BASE, 3, pui32ADC0Value);
    SysCtlDelay(SysCtlClockGet() / 12);
    }

    }

  • Hello M. Mary,

    The GPIO Configuration needed an update. Other than that I ran the code and tested it for +3.3V and 0V and it gives the correct code, when the voltage is applied on PE5 pin.

    main(void) {

        uint32_t pui32ADC0Value[1];

        SysCtlClockSet(SYSCTL_SYSDIV_10 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN| SYSCTL_XTAL_16MHZ);
        SysCtlPeripheralEnable(SYSCTL_PERIPH_ADC0);
        SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE);


        GPIOPinTypeADC(GPIO_PORTE_BASE, GPIO_PIN_5);

        ADCIntEnable(ADC0_BASE, 3);
        ADCSequenceDisable(ADC0_BASE, 3);
        ADCSequenceConfigure(ADC0_BASE, 3, ADC_TRIGGER_PROCESSOR, 0);
        ADCSequenceStepConfigure(ADC0_BASE, 3, 0,ADC_CTL_CH8 | ADC_CTL_IE | ADC_CTL_END);


        ADCSequenceEnable(ADC0_BASE, 3);
        while (1) {
            ADCProcessorTrigger(ADC0_BASE, 3);
            while (!ADCIntStatus(ADC0_BASE, 3, false))
            {
            }

            ADCIntClear(ADC0_BASE, 3);
            ADCSequenceDataGet(ADC0_BASE, 3, pui32ADC0Value);
            SysCtlDelay(SysCtlClockGet() / 12);
        }

    }

    Regards

    Amit

  • Before searching for problems in the code, confirm the expected voltage at the ADC input pin of the MCU, and I mean the MCU pin (or at least as close as possible).

    the following code produce 0 output value from function generator which is set for 5.

    I hope that does not mean 5V.

  • HI Amit,

    Thank you.

    I think I made mistake while testing. Can you tell me how i can test for external input?

  • Hello M Mary,

    If you are using the EK-TM4C123, then connect the Pin to the +3.3V header and/or GND to see that the conversion happens as per the analog input on the pin.

    Regards

    Amit

  • Thank you Amit,

    I checked that by connecting PE5 to 3.3v header in J1 row. Then given input from function generator to PE5 pin and Gnd in J3 row. But i got 3900,3906... like values. What is wrong? any other hardware connections missed by me?

  • Hi Mary,

    So when you connected it to the 3.3V it was all alright?

    Only the fuction generator gave those values?

  • Hi Luis,

    I check ADC with   the program posted by Amit. That is

    #include <stdbool.h>
    #include <stdint.h>
    #include "inc/hw_memmap.h"
    #include "driverlib/adc.h"
    #include "driverlib/gpio.h"
    #include "driverlib/sysctl.h"
    #include "driverlib/adc.h"
    #include "inc/hw_types.h"
    #include "driverlib/debug.h"

    main(void) {

        uint32_t pui32ADC0Value[1];

        SysCtlClockSet(SYSCTL_SYSDIV_10 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN| SYSCTL_XTAL_16MHZ);
        SysCtlPeripheralEnable(SYSCTL_PERIPH_ADC0);
        SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE);

        GPIOPinTypeADC(GPIO_PORTE_BASE, GPIO_PIN_5);

        ADCIntEnable(ADC0_BASE, 3);
        ADCSequenceDisable(ADC0_BASE, 3);
        ADCSequenceConfigure(ADC0_BASE, 3, ADC_TRIGGER_PROCESSOR, 0);
        ADCSequenceStepConfigure(ADC0_BASE, 3, 0,ADC_CTL_CH8 | ADC_CTL_IE | ADC_CTL_END);


        ADCSequenceEnable(ADC0_BASE, 3);
        while (1) {
            ADCProcessorTrigger(ADC0_BASE, 3);
            while (!ADCIntStatus(ADC0_BASE, 3, false))
            {
            }

            ADCIntClear(ADC0_BASE, 3);
            ADCSequenceDataGet(ADC0_BASE, 3, pui32ADC0Value);
            SysCtlDelay(SysCtlClockGet() / 12);
        }

    }

    Initially I check the output without giving input from function generator and all for some clarifications. That produces output as 2839,2838.... Then I give input from FG with  settings: freq=50Hz and 3.0v. And 3.3v header is connected to PE5 pin. Now I got 2837,2830... like values. These values are the  pui32ADC0Value values. I dont know what is the problem.

  • Hi Mary,

    without notigh connected the pin is floating and any value can apear.

    You should connect only the MCU 3.3V with a resistor to that pin and see what values it gives.

    The fuction generator is making a analog wave right? Well then depending on what part of the wave the ADC is fired any value can apear since the wave would go from 0 to 3V. If you have the 3.3V header conected at the same time as the FG of course it will give weird values.

    I belive Amit sugested conecting PE5 to 3.3V or GND just to test if the ADC was working since it would give a value arround 4095 (3.3V) and 0(GND). Am i correct @? (does mention user work here?)

  • Thank you all.

    Now i get understand the process in ADC. Thank you so much.

  • Hello Luis

    Yes, your point is correct. the Test rig I proposed for the LaunchPad is the simplest method of making sure it works as expected before connecting an Analog Signal

    Regards

    Amit