fbpx

Reading Analog Sensor Values with the ADC on TM4C123

Share




This example shows how to measure the analog input of a sensor using the ADC on TM4C123 Tiva C LaunchPad.

Configuring the GPIO for ADC input

1 Enable the clock to GPIO pin by using RCGCGPIO.

 SYSCTL -> RCGCGPIO ; //Enable clock to PORTE 

2. Set the GPIOAFSEL (GPIO alternate function) bit for ADC input pin to 1.

 GPIOE -> AFSEL |=8;  

3. Configure AINx signal to be used as analog input by clearing the bit in the GPIODEN (GPIO Digital enable) register.

 GPIOE -> DEN &= ~8; //Disable digital function 

4. Disable the Analog isolation circuit for ADC input pins by writing a 1 to the GPIOAMSEL register.

 GPIOE -> AMSEL |=8; //Enable analog function 

Configuring and reading the ADC

1.Enable the clock to the ADC. e.g. ADC0

 SYSCTL ->RCGCADC |=3; // Enable ADC0 clock 

2.Disable Sample Sequencer before making changes

 ADC0 -> ACTSS &= ~8 ;// Disable SS3 

3.Select start conversion trigger method .e.g. using software trigger

 ADC0 -> EMUX &= ~0xF000; //Use software trigger

4.Select ADC input channel

 ADC0 -> SSMUX3 =0;  // Select AIN0 connected to PE3

5. Set Sample Sequencer Control characteristics

 ADC0 ->SSCTL3 |= 6; // single-ended, one-conversion and raw interrupt 

6.Enable Sample Sequencer

 ADC0 -> ACTSS |= 8; //Enable Sample Sequencer 3 

7.Start conversion

 ADC0 -> PSSI |= 8 ; //Start conversion by SS3 

8. Poll for the end-of-conversion flag

 while((ADC0 -> RIS & 8) == 0) ;//Wait for conversion to complete

9.Read ADC results when conversion is complete

 ADC0 -> SSFIFO3; // Read conversion results 

10. Clear conversion complete flag

 ADC0 -> ISC =8 ; 

Full Program Source

#include "TM4C123.h";                    // Device header
int main(void)
{
    volatile int result;

    /* enable clocks */
    SYSCTL->RCGCGPIO |= 0x10;   /* enable clock to GPIOE (AIN0 is on PE3) */
    SYSCTL-> |= 1;       /* enable clock to ADC0 */

    /* initialize PE3 for AIN0 input  */
    GPIOE->AFSEL |= 8;       /* enable alternate function */
    GPIOE->DEN &= ~8;        /* disable digital function */
    GPIOE->AMSEL |= 8;       /* enable analog function */

    /* initialize ADC0 */
    ADC0->ACTSS &= ~8;        /* disable SS3 during configuration */
    ADC0->EMUX &= ~0xF000;    /* software trigger conversion */
    ADC0->SSMUX3 = 0;         /* get input from channel 0 */
    ADC0->SSCTL3 |= 6;        /* take one sample at a time, set flag at 1st sample */
    ADC0->ACTSS |= 8;         /* enable ADC0 sequencer 3 */

    while(1)
    {
        ADC0->PSSI |= 8;        /* start a conversion sequence 3 */
        while((ADC0->RIS & 8) == 0) ;   /* wait for conversion complete */
        result = ADC0->SSFIFO3; /* read conversion result */
        ADC0->ISC = 8;          /* clear completion flag */
    }
}
London, U.K
© 2019 BOHOBIOM LTD