Analog-to-Digital Converter (ADC) ​
Complete Lab Manual
For the complete experiment including learning objectives, theoretical background, and detailed explanations, download the PDF manual: Download Experiment 8 PDF
Examples ​
This section demonstrates how to configure and use the ADC module on the TM4C123 microcontroller using both polling and interrupt-driven sampling methods. A potentiometer is used as a controllable analog input source by wiring its terminals to 3.3 V and ground, with the adjustable rotor connected to an ADC input such as AIN0 (PE3). Rotating the potentiometer varies the rotor voltage between 0 V and 3.3 V, allowing you to generate different analog levels and observe the corresponding ADC conversion results.
Single ADC Channel Reading (Polling Method) ​
In this example, you will configure the ADC to read a single analog input channel (AIN0) using sample sequencer 3 (SS3) and turn on an LED if the voltage exceeds a certain threshold.
#include "TM4C123.h"
#define GREEN_LED 1 << 3
volatile unsigned int adc_value;
int main(void)
{
SYSCTL->RCGCGPIO |= (1 << 4); // Enable Clock to GPIOE or PE3/AN0
SYSCTL->RCGCADC |= (1 << 0); // AD0 clock enable
GPIOE->AFSEL |= (1 << 3); // enable alternate function
GPIOE->DEN &= ~(1 << 3); // disable digital function
GPIOE->AMSEL |= (1 << 3); // enable analog function
ADC0->ACTSS &= ~(1 << 3); // disable SS3 during configuration
ADC0->EMUX &= ~0xF000; // Software Trigger
ADC0->SSMUX3 = 0; // get input from channel 0
ADC0->SSCTL3 |= (1 << 1) | (1 << 2); // take one sample at a time, set flag at 1st sample
ADC0->ACTSS |= (1 << 3); // enable ADC0 sequencer 3
SYSCTL->RCGCGPIO |= 0x20; // turn on bus clock for GPIOF
GPIOF->DIR |= GREEN_LED; // set GREEN pin as a digital output pin
GPIOF->DEN |= GREEN_LED; // Enable PF3 pin as a digital pin
while (1)
{
ADC0->PSSI |= (1 << 3); // initiate SS3 conversion
while ((ADC0->RIS & 8) == 0); // Wait untill sample conversion completed
adc_value = ADC0->SSFIFO3; // read adc coversion result from SS3 FIFO
ADC0->ISC = 8; // clear coversion clear flag bit
if (adc_value >= 2048)
GPIOF->DATA |= GREEN_LED; // turn on green LED
else if (adc_value < 2048)
GPIOF->DATA &= ~GREEN_LED; // turn off green LED
}
}Reading Internal Temperature Sensor ​
In this example, you will configure the ADC to read the internal temperature sensor using sample sequencer 3 (SS3) and display the temperature value on the debugger using the Watch window.
#include "TM4C123.h"
volatile unsigned int adc_value;
volatile float temp;
void ADC0SS3_Handler(void)
{
adc_value = ADC0->SSFIFO3 & 0xFFF; // Read 12-bit ADC value from FIFO
temp = 147.5 - (75 * 3.3f * adc_value)/ 4096; // Convert to temperature in Celsius
ADC0->ISC = 8; // Clear SS3 interrupt flag
}
int main(void)
{
SYSCTL->RCGCGPIO |= (1 << 4); // Enable clock to Port E
SYSCTL->RCGCADC |= (1 << 0); // Enable clock to ADC0
GPIOE->AFSEL |= (1 << 3); // Enable alternate function on PE3
GPIOE->DEN &= ~(1 << 3); // Disable digital I/O on PE3
GPIOE->AMSEL |= (1 << 3); // Enable analog input on PE3
ADC0->ACTSS &= ~(1 << 3); // Disable SS3 during configuration
ADC0->EMUX |= 0xF000; // Set SS3 to continuous sampling mode
ADC0->SSMUX3 = 0; // Configure SS3 to sample AIN0 (PE3)
ADC0->SSCTL3 |= (1 << 1) | (1 << 2) | (1 << 3); // Set end of sequence, enable interrupt, enable temperature sensor
ADC0->IM |= (1 << 3); // Enable SS3 interrupt mask
NVIC_EnableIRQ(ADC0SS3_IRQn); // Enable ADC0 SS3 interrupt in NVIC
ADC0->ACTSS |= (1 << 3); // Enable SS3
while (1) {} // Wait for interrupts
}Tasks ​
Task 1: Timer-Based Temperature Measurement ​
Modify Example 2 so that the ADC sampling is triggered by a timer every second, instead of using continuous sampling mode. Connect an analog temperature sensor (e.g., TMP36) to an ADC input channel (such as AIN0 on PE3). Configure the ADC to read the sensor voltage on each timer trigger and calculate the corresponding temperature. Monitor the temperature values in real time by adding the variable \texttt{temp} to the Watch window in the debugger.
Task 2: Comparing Two ADC Channels ​
Extend Example 2 to read two different analog inputs using separate ADC channels (e.g., AIN0 on PE3 and AIN1 on PE2) with sample sequencer 3 (SS3). Configure the ADC to use interrupts so that each conversion triggers an ISR. In the ISR, compare the two ADC values and use the result to control LEDs on GPIOF: for example, turn on the green LED if the first channel has a higher voltage, or turn on the red LED if the second channel is higher. Display both ADC values in the debugger Watch window to observe the comparison in real time.