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 Launchpad ADC reading incorrect

EK-TM4C123G - Tiva Launchpad.

I have tried example code /example/peripherals/adc as well my own code to read the ADC value. even I tried different input channels like AIN0, AIN2 & AIN5. But always 

ADCSequenceDataGet(ADC0_BASE, ADC0_SEQUENCER3, adcValue);

Gives me the value in-between 0x145 to 0x170  for 0 to 3.3V.

I don't know what really going wrong, because I have configured it properly and getting interrupt also correctly.

ComparatorConfigure(COMP_BASE, 1, (COMP_TRIG_FALL |	COMP_ASRCP_PIN | COMP_OUTPUT_NORMAL ));
        ADCSequenceDisable(ADC0_BASE, ADC0_SEQUENCER3);
ADCSequenceConfigure(ADC0_BASE, ADC0_SEQUENCER3, ADC_TRIGGER_COMP1, 0 );
ADCSequenceStepConfigure(ADC0_BASE, ADC0_SEQUENCER3, 0, ADC_CTL_CH0 | ADC_CTL_IE | ADC_CTL_END );
ADCSequenceEnable(ADC0_BASE, ADC0_SEQUENCER3);
ADCIntClear(ADC0_BASE, ADC0_SEQUENCER3);
ADCIntEnable(ADC0_BASE, ADC0_SEQUENCER3);
Hwi_enableInterrupt(33);

isr()
{
unsigned int adcValue[1];
ADCIntClear(ADC0_BASE, ADC0_SEQUENCER3);
ADCSequenceDataGet(ADC0_BASE, ADC0_SEQUENCER3, adcValue);

}

Any suggestions? 

  • I do not see reference settings.

    Please refer to http://www.ti.com/lit/ds/symlink/tm4c123gh6pm.pdf 13.3.4.1 Voltage Reference

    Regards,
    Maciej 

  • Thanks Maciej for pointer.

    I have added ADCReferenceSet(ADC0_BASE,ADC_REF_INT);

    But still the value is in-between 0x140 to 0x170, no matter what is i/p voltage (0 to 3.3 volts) always getting reading in this rang. Even when I kept the pin floating still read value is in given range. 

    Seems ADC reading noise or not activated correctly. ( input voltage is correctly applied externally, I have verified the voltage level on Pin).

    Any suggestions??

  • Hi,

    I think you did not configured the ADC input pin as  analog pin-try read the data sheet and/or read also the driver lib function GPIOPinTypeADC(). 

    Petrei

  • Yes Petrei, I did it.

    Forgot to mentioned in code snippet.

    Void EK_TM4C123GXL_ADC(Void)
    {
    
    /* ADC0 */
    SysCtlPeripheralEnable(SYSCTL_PERIPH_ADC0);
    
    /* enable pin for analog input */
    GPIOPinTypeADC(GPIO_PORTD_BASE, GPIO_PIN_2 ); //Fix Me
    
    
    }
  • Ok, you haven't posted complete code therefore it is hard to tell what is missing.

    To use ADC you need to:

    1. Initialize clocks
    2. Initialize GPIO ( I do not see GPIOPinTypeADC)
    3. Initialize ADC
    Please see the following materials (Chapter V is on ADC):
  • I apologies for not posting complete code. here is the complete code and I am having problem using this code.

    
    
    #define  ADC0_SEQUENCER3   3
    
    
    Void EK_TM4C123GXL_initGeneral(Void)
    {
        SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
        SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);
        SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOC);
        SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD);
        SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE);
        SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
    }
    
    /*
     *  ======== EK_TM4C123GXL_initCOMP ========
     */
    
    Void EK_TM4C123GXL_ADC(Void)
    {
    
        /* ADC0 */
        SysCtlPeripheralEnable(SYSCTL_PERIPH_ADC0);
    
        /* enable pin for analog input */
        GPIOPinTypeADC(GPIO_PORTD_BASE, GPIO_PIN_2 ); //Fix Me
    
    
    }
    
    Int main(Void)
    {
    	int i;
    	FPMsgObj *msg;
    	Error_Block eb;
    
        SysCtlClockSet(SYSCTL_SYSDIV_10 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN |
                           SYSCTL_XTAL_16MHZ);
    
        /* Call board init functions */
        EK_TM4C123GXL_initGeneral();
        EK_TM4C123GXL_ADC();
        ... // other init for UART, COMP1 and GPIO
    
    ComparatorConfigure(COMP_BASE, 1, (COMP_TRIG_FALL | COMP_ASRCP_PIN | COMP_OUTPUT_NORMAL ));
    ADCSequenceDisable(ADC0_BASE, ADC0_SEQUENCER3);
    ADCReferenceSet(ADC0_BASE,ADC_REF_INT);
    ADCSequenceConfigure(ADC0_BASE, ADC0_SEQUENCER3, ADC_TRIGGER_COMP1, 0 );
    ADCSequenceStepConfigure(ADC0_BASE, ADC0_SEQUENCER3, 0, ADC_CTL_CH0 | ADC_CTL_IE | ADC_CTL_END );
    ADCSequenceEnable(ADC0_BASE, ADC0_SEQUENCER3);
    ADCIntClear(ADC0_BASE, ADC0_SEQUENCER3);
    ADCIntEnable(ADC0_BASE, ADC0_SEQUENCER3);
    Hwi_enableInterrupt(33);
    BIOS_start();
    
    return (0);
    }
    
    isr()
    {
    unsigned int adcValue[1];
    ADCIntClear(ADC0_BASE, ADC0_SEQUENCER3);
    ADCSequenceDataGet(ADC0_BASE, ADC0_SEQUENCER3, adcValue);
     
    }

     
  • Hi,

    Nirav Patel2 said:
    ADCSequenceStepConfigure(ADC0_BASE, ADC0_SEQUENCER3, 0, ADC_CTL_CH0 | ADC_CTL_IE | ADC_CTL_END );

       You, are configuring PD2 as Analog input. Based from datasheet PD2 alternates as AIN5. Try ADC_CTL_CH5 instead of ADC_CTL_CH0.

    -kel

  • Thanks Kel,

    It's working, able to read proper ADC values.

    I didn't know that the Channel ADC_CTL_CHX must be the same corresponding AINX input we select.

  • Markel Robregado,
    can you help me in the similar problem , i am working on the project "Acoustic Source Localisation" ,In that project I am using Two mics (MAX9812) for estimate the Direction of arrival of sound
    So for I am using Adc0 sequencer 3 , and i am required to take signal from two channels (for two mics)
    I have initialised as given below
    void ADC0_Init(void){ volatile unsigned long delay;
    SYSCTL_RCGC2_R |= 0x00000010; // 1) activate clock for Port E
    delay = SYSCTL_RCGC2_R; // allow time for clock to stabilize
    GPIO_PORTE_DIR_R &= ~0x05; // 2) make PE2 pe0 input
    GPIO_PORTE_AFSEL_R |= 0x05; // 3) enable alternate function on PE2 PE0
    GPIO_PORTE_DEN_R &= ~0x05; // 4) disable digital I/O on PE2 PE0
    GPIO_PORTE_AMSEL_R |= 0x05; // 5) enable analog function on PE2 PE0
    SYSCTL_RCGC0_R |= 0x00010000; // 6) activate ADC0
    delay = SYSCTL_RCGC0_R;
    SYSCTL_RCGC0_R |=0x00000300; // 7) configure for 1M Samplespersec
    ADC0_SSPRI_R = 0x0123; // 8) Sequencer 3 is highest priority
    ADC0_ACTSS_R &= ~0x0008; // 9) disable sample sequencer 3
    ADC0_EMUX_R &= ~0xF000; // 10) seq3 is software trigger
    ADC0_SSMUX3_R &= ~0x000F; // 11) clear SS3 field
    ADC0_SSMUX3_R += 3; // set channel Ain3 (PE0)
    ADC0_SSCTL3_R = 0x0006; // 12) no TS0 D0, yes IE0 END0
    ADC0_ACTSS_R |= 0x0008; // 13) enable sample sequencer 3
    }
    I multiplex both mic signal by changing
    ADC0_SSMUX3_R &= ~0x000F; // 11) clear SS3 field
    ADC0_SSMUX3_R += 3; // set channel Ain3(PE0)
    and
    ADC0_SSMUX3_R &= ~0x000F; // 11) clear SS3 field
    ADC0_SSMUX3_R += 1; // set channel Ain1 (PE2)
    In the course "Embedded system "PE2 was used as channel
    Now in my project adc conversion from PE2 is working properly but from PE0 even if there is no sound its adc converted data in varying unexpectedly while from that of PE2 remains quite constant when sound is applied(Pe2 is working properly)

    So please tell what I am doing Wrong in this project
  • Hi mohd irfan,

    I am not available to help at the moment, so wait for other forum members to help you.

    I suggest you use Tivaware C API's. Also provide more information about your hardware set-up, like what Tiva board are you using?

    - kel