Other Parts Discussed in Thread: EK-TM4C123GXL
Hello,
I'm using TM4C123GH6PM launchpad to read an analog input and convert to digital.
In settings, ADC0 is used, AIN0 (PE3) is the analogy voltage input. I use a trim to input 1.3V to PE3.
The IDE I used is IAR Arm 9.32.1.
I followed the TM4C123GH6PM Microcontroller datasheet, first initialize the ADC module, configure the Sample Sequencer, then go to the infinity while loop to sample voltage input.
In debug, I checked every register setting, they were set as coded. Unfortunately, the output is always 0x0dcd, there was no data converted.
Could anyone help me to check if I made some mistakes?
Thank you!
The code is below:
#include <stdint.h>
#include "lm4f120h5qr.h"
int main()
{
unsigned int volatile adc_value = 0;
// ADC frequency initialization
SYSCTL_RCC_R &= ~(SYSCTL_RCC_PWRDN); // PLL Power Down
ADC0_CC_R |= ADC_CC_CS_PIOSC; // Set clock source: PIOSC
SYSCTL_RCC_R |= SYSCTL_RCC_PWRDN; // PLL Power On
// ADC Module Initialization
SYSCTL_RCGCADC_R |= SYSCTL_RCGCADC_R0; // Enable ADC0 Clock Gating
SYSCTL_RCGCGPIO_R |= SYSCTL_RCGCGPIO_R4; // Enable GPIO Port E Run Mode Clock Gating
GPIO_PORTE_DIR_R &= ~(1U << 3); // Set PE3 is an input
GPIO_PORTE_AFSEL_R |= (1U << 3); // Set PE3 functions as peripheral signal
GPIO_PORTE_DEN_R &= ~(1U << 3); // The digital function for PE3 is disabled
GPIO_PORTE_AMSEL_R |= (1U << 3); // Enable PE3 analog function
// ADC Sample Sequencer Configuration
ADC0_ACTSS_R &= ~(ADC_ACTSS_ASEN3); // ADC0 SS3 is Disabled
ADC0_EMUX_R |= ADC_EMUX_EM3_ALWAYS; // Set ADC0 SS3 triger as always (continuously sample)
ADC0_SSMUX3_R &= ~(ADC_SSMUX3_MUX0_M); // ADC SS3 first sample input select
ADC0_SSCTL3_R |= ADC_SSCTL3_IE0 | ADC_SSCTL3_END0; // 1st Sample is End of Sequence and set IE flag
ADC0_IM_R |= ADC_IM_MASK3; // SS3 Interrupt sent to interrupt controller
ADC0_ACTSS_R |= ADC_ACTSS_ASEN3; // ADC SS3 is Enabled
while (1) {
ADC0_PSSI_R |= ADC_PSSI_SS3; // Begin sampling on Sample Sequencer 3
while ((ADC0_RIS_R & ADC_RIS_INR3) == 0); // Wait while sample finish
adc_value = ADC0_SSFIFO3_R & 0xFFF; // Read conversion value from FIFO3
ADC0_ISC_R |= ADC_ISC_IN3; // SS3 Interrupt Status and Clear
}
}