Select Page

Using ADC0 to convert input from AIN0

Configuring GPIO for ADC input

In using ADC, we must also configure the GPIO pins to allow the connection of an analog signal through the input pin. In this regard, it is the same as all other peripherals. The steps are as follows:

  1. Enable the clock to GPIO pin by using RCGCGPIO.
  2. Set the GPIOAFSEL (GPIO alternate function) bit for ADC input pin to 1.
  3. Configure AINx signal to be used as analog input by clearing the bit in the GPIODEN (GPIO Digital enable) register.
  4. Disable the Analog isolation circuit for ADC input pins by writing a 1 to the GPIOAMSEL register.

Configuring ADC and reading ADC channel

After the GPIO configuration, we need to take the following steps to configure the ADC for Sample Sequencer 3 (SS3):

  1. Enable the clock to ADC0 or ADC1 modules using RCGCADC register of System Registers.
    The SYSCTL_RCGCADC_R |= 3 will enable the clock to both ADC0 and ADC1 modules. For ADC0 module use SYSCTL_RCGCADC_R |= 1
  2. Disable the Sample Sequencer using the ADCACTSS register before changing the configuration of the sequencer.
    ADC0_ACTSS_R &= ~8 disables the SS3.
  3. Choose the software trigger using the ADCEMUX register.
    Use ADC0_EMUX_R &= ~0xF000 for software trigger.
  4. Select the ADC input channel using the ADCSSMUXn register. In the case of SS3 we use the ADCSSMUX3.
    For example, The ADC0_SSMUX3_R = 0 selects AIN0 channel on pin PE3.
  5. Select the single-ended option, one-conversion per sample, and raw interrupt bit for end-of-conversion using the ADCSSCTL3 register.
    Use ADC0_SSCTL3_R |= 6 for single-ended, one-conversion, and raw interrupt.
  6. Enable the Sample Sequencer SS3 using ADCACTSS register.
    ADC0_ACTSS_R |= 8 enables the SS3.
  7. Use ADCPSSI register to start a new conversion.
    Use ADC0_PSSI_R |= 8 to start a conversion by Sample Sequencer 3.
  8. Keep monitoring the end-of-conversion INR3 flag in ADCRIS (ADC0_RIS_R) register.
  9. When the INR3 goes HIGH, read the ADC result from the ADCSSFIFO3 (ADC0_SSFIFO3_R) and save it.
  10. After reading the ADC result in step 9, clear the INR3 flag in ADCRIS register to allow for the next conversion.
    To clear INR3 flag, write to ADCICS register with ADC0_ISC_R = 8.
  11. Repeat steps 7 through 10 for the next conversion.
Figure 11.31: ADC Connection for the following Program

Source Code

  1. /* A to D conversion
  2.  *
  3.  * This program converts the analog input from AIN0 (J3.9 of LaunchPad)
  4.  * using sample sequencer 3 and software trigger continuously.
  5.  *
  6.  * Note: AIN0 channel is on PE3 pin.
  7.  *
  8.  */
  9. 
    
  10. #include <stdint.h>
  11. #include "inc/tm4c123gh6pm.h"
  12. 
    
  13. int main(void)
  14. {
  15.     volatile int result;
  16. 
    
  17.     /* enable clocks */
  18.     SYSCTL_RCGCGPIO_R |= 0x10; /* enable clock to PE (AIN0 is on PE3) */
  19.     SYSCTL_RCGCADC_R |= 1;     /* enable clock to ADC0 */
  20. 
    
  21.     /* initialize PE3 for AIN0 input  */
  22.     GPIO_PORTE_AFSEL_R |= 8;   /* enable alternate function */
  23.     GPIO_PORTE_DEN_R &= ~8;    /* disable digital function */
  24.     GPIO_PORTE_AMSEL_R |= 8;   /* enable analog function */
  25. 
    
  26.     /* initialize ADC0 */
  27.     ADC0_ACTSS_R &= ~8;        /* disable SS3 during configuration */
  28.     ADC0_EMUX_R &= ~0xF000;    /* software trigger conversion */
  29.     ADC0_SSMUX3_R = 0;         /* get input from channel 0 */
  30.     ADC0_SSCTL3_R |= 6;        /* take one sample at a time, set flag at 1st sample */
  31.     ADC0_ACTSS_R |= 8;         /* enable ADC0 sequencer 3 */
  32. 
    
  33.     while(1) {
  34.         ADC0_PSSI_R |= 8;      /* start a conversion sequence 3 */
  35.         while((ADC0_RIS_R & 8) == 0)
  36.             ;                  /* wait for conversion complete */
  37.         result = ADC0_SSFIFO3_R; /* read conversion result */
  38.         ADC0_ISC_R = 8;        /* clear completion flag */
  39.     }
  40. }

Note

  • Short 3 & 4 of J7 on the EduARM4 Trainer Board
  • To change the input voltage to the ADC, Vary the RV2 potentiometer.