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 CODE: TM4c123g

Other Parts Discussed in Thread: TM4C123GH6PM

Hello Sir,

I have written the following code for ADC0 initialisation using sample sequencer 3 where the input has been taken from PE3.

I have used interrupt to check if there is data in sample sequencer fifo ...and then extract the data....Please check and let me know if I have done anytn wrong

#include<stdint.h>
#include<stdbool.h>
#include<tm4c123gh6pm.h>
#include<adc.h>
#include<interrupt.h>
#define ADCIM (*((unsigned long *) 0x40038008))

void init_module(void);
void init_sam_seq(void);

int main(void) {

init_module();
init_sam_seq();

return 0;
}


void init_module(void)
{
SYSCTL_RCGCADC_R=SYSCTL_RCGCADC_R0; // Enable clock for ADC 0
SYSCTL_RCGCGPIO_R=SYSCTL_RCGCGPIO_R4;//Enable clock for Port E PE3
GPIO_PORTE_AFSEL_R=0x08;
GPIO_PORTE_DEN_R&=~0x08;
GPIO_PORTE_AMSEL_R=0x00000008;
}
void init_sam_seq(void)
{
ADC0_ACTSS_R&=0x11111117;
ADC0_EMUX_R&=ADC_EMUX_EM3_PROCESSOR;
ADC0_SSMUX3_R=0x00000000;
ADC0_SSCTL3_R=0x00000004;
ADCIM=0x00000008;
//IntEnable(ADC_INT_SS3);
ADCIntEnable(0x40038000,ADC_INT_SS3);
IntMasterEnable();

ADC0_ACTSS_R=ADC_ACTSS_ASEN3;
}

void ADC0IntHandler(void)
{uint32_t adc_data;
ADCIntClear(0x40038000,ADC_INT_SS3);
while(!ADCIntStatus(0x40038000, 0, true))
{
}
adc_data=ADC0_SSFIFO3_R&ADC_SSFIFO3_DATA_M;

}

Regards,

Vikram

  • Hello Vikram,

    Why use the Direct register access macro's when the TivaWare example already has the same code based on TivaWare API's? I would strongly urge you to change the code to the API's instead of using Direct macros for the very simple reason: The code is readable for all and it is "not easy" missing wrong programming when we review the same+The example code works!!!

    Regards

    Amit

  • Hello,

    I have rewritten the code using the APIs. Please note that the UART is working perfectly fine and there is absolutely no problem with it.

    Moreover, I have checkde my slide pot for errors however , It works perfectly fine when tested .

    The problem in this code according to me, is in the ADC section only. I am not able to figure out what it is.

    The initialisation for PORT E has been done using direct register method. It is withour errors too I think.

    /*
    * main.c
    */
    #include<stdint.h>
    #include<stdbool.h>
    #include<tm4c123gh6pm.h>
    #include<adc.h>
    #include <hw_memmap.h>
    #include <gpio.h>
    #include <uart.h>
    void uart_init(void);
    void api_init(void);


    void main(void) {
    uint32_t pui32ADC0Value;


    uart_init();
    api_init();
    ADCProcessorTrigger(ADC0_BASE, 3);

    while(1){

    // Wait until the sample sequence has completed.
    //
    while(!ADCIntStatus(ADC0_BASE, 3, false))
    {
    }
    //
    // Read the value from the ADC.
    //
    pui32ADC0Value=ADCSequenceDataGet(ADC0_BASE, 3, &pui32ADC0Value);

    while(((UART0_FR_R & UART_FR_BUSY)==UART_FR_BUSY)){};

    UARTCharPutNonBlocking(UART0_BASE,(unsigned char)pui32ADC0Value);

    }


    }

    void uart_init()
    {
    SYSCTL_RCGC2_R|=(SYSCTL_RCGC2_GPIOA);
    SYSCTL_RCGC1_R=SYSCTL_RCGC1_UART0;
    GPIO_PORTA_AFSEL_R|=0x00000003;
    GPIO_PORTA_PCTL_R=GPIO_PCTL_PA0_U0RX|GPIO_PCTL_PA1_U0TX;
    GPIO_PORTA_AMSEL_R&=~0x03;
    GPIO_PORTA_DEN_R=0x03;

    UART0_IBRD_R=8;
    UART0_FBRD_R=43;
    UART0_LCRH_R=UART_LCRH_WLEN_8|UART_LCRH_FEN;
    UART0_CC_R=UART_CC_CS_SYSCLK;
    UART0_CTL_R=UART_CTL_UARTEN | UART_CTL_TXE |UART_CTL_RXE;

    }
    void api_init()
    { unsigned char C;
    SYSCTL_RCGC2_R|=0x00000010;//Enable clock for Port E PE3

    C=SYSCTL_RCGC2_R;

    GPIO_PORTE_DIR_R&=~0x08;
    //to select the alternate function of the pin
    GPIO_PORTE_AFSEL_R|=0x08;

    //disable the digital pin
    GPIO_PORTE_DEN_R&=~0x08;

    //to disable the analog isolation circuitry
    GPIO_PORTE_AMSEL_R|=0x08;

    ADCSequenceConfigure(ADC0_BASE, 3, ADC_TRIGGER_PROCESSOR, 0);
    ADCSequenceStepConfigure(ADC0_BASE, 3, 0,ADC_CTL_IE | ADC_CTL_END | ADC_CTL_CH0);
    ADCSequenceEnable(ADC0_BASE, 0);

    }

  • Hi,

    Several problems with your last code version:

    a) as it is written now, it is "single shot", i.e. executes only once and then do nothing. More, there is the errata sheet  saying for SS3 the first two samples are wrong. To overcome this, move the line ADCProcessorTrigger(ADC0_BASE, 3); into while(1) loop.

    b) the ADC result is a 12 bit number, and you cannot just simply shrink it to 8 bits and sending to UART - you need to convert it to a string representation and send it to UART to be human read. There is the possibility to be sent also in binary form, but to read it on the PC terminal you need some software to convert it to human reading form.

    c) when using the ADC and need to configure it, first disable the sequencer, make the configurations and then enable the sequencer (user manual).

    Petrei