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.

tm4c123gh6pm ADC12 sampled with TIMER0

Other Parts Discussed in Thread: TM4C123GH6PM

Hey guys, I'm trying to use TIMER0 to sample an ADC input pin every second. I've gone through the interrupt and ADC12 tuts from the workbook, but am having a bit of trouble combining them. Currently I get an ISR fault at ADCSequenceEnable. Any help appreciated.

#include <stdint.h>
#include <stdbool.h>
#include "inc/hw_types.h"
#include "driverlib/sysctl.h"
#include "inc/tm4c123gh6pm.h"
#include "driverlib/adc.h"
#include "driverlib/gpio.h"
#include "driverlib/debug.h"
#include "inc/hw_memmap.h"
#include "driverlib/timer.h"
#include "driverlib/interrupt.h"

uint32_t ADC0Values[1]; // global ADC value

int main(void) {

    SysCtlClockSet(SYSCTL_SYSDIV_5|SYSCTL_USE_PLL|SYSCTL_OSC_MAIN|SYSCTL_XTAL_16MHZ); // 40 Mhz

    // Timer Stuff
    SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0);
    TimerConfigure(TIMER0_BASE, TIMER_CFG_PERIODIC);
    TimerLoadSet(TIMER0_BASE, TIMER_A, SysCtlClockGet()); // 1 Hz
    TimerEnable(TIMER0_BASE, TIMER_A);

    // ADC - setup channel 0 with SS3
    SysCtlPeripheralEnable(SYSCTL_PERIPH_ADC0);
    ADCSequenceDisable(ADC0_BASE, 3); // disable for config
    ADCSequenceConfigure(ADC0_BASE, 3, ADC_TRIGGER_TIMER, 0);
    ADCSequenceStepConfigure(ADC0_BASE,3,0, ADC_CTL_CH0|ADC_CTL_IE|ADC_CTL_END);
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE);
    GPIOPinTypeADC(GPIO_PORTE_BASE, GPIO_PIN_6); // E5 is input
    SysCtlADCSpeedSet(SYSCTL_ADCSPEED_500KSPS);
    ADCSequenceEnable(ADC0_BASE, 3);
    ADCIntEnable(ADC0_BASE, 3);
    TimerControlTrigger(TIMER0_BASE, TIMER_A, 1);

    ADCIntClear(ADC0_BASE, 1);
    ADCIntEnable(ADC0_BASE, 1);
    IntEnable(INT_ADC0SS3); // interrupts on sequence 3

    while(1) {
        // NOP
    }
}

void Timer0IntHandler(void) {
    ADCIntClear(ADC0_BASE, 1);
    ADCSequenceDataGet(ADC0_BASE, 3, ADC0Values);
    ADCIntClear(ADC0_BASE, 1);
}

  • Hello James,

    I see quite a few issues

    1. The setup of the device should be done from GPIO Configuration, then ADC and finally GPTimer. It makes setup very modular and in order of signal chaining.

    2. The ADC Channel 0 is PE3 and not PE6

    3. Why is a timer interrupt handler made for ADC? Can you check the startup_ccs.c if the Interrupt is correctly mapped to the ISR Vector Table.

    4. Is the Fault for ADCSequenceEnable or ADCSequenceDisable? You can add a SysCtlPeripheralReady Check to make sure that the Peripheral Clocks have started correctly?

    5. Can you do a register dump of the ADC Registers called PPADC, RCGCADC and SRADC in System Control Module and post it.

    Regards

    Amit

  • Thanks for your help Amit. I think I was a bit confused about the timer interrupt vs the ADC interrupt. If I want to sample the ADC every second, then this is a timer interrupt? I've updated the code a bit if you wanted to take a look. Currently it's giving a fault ISR if I run it and then pause (it never gets to the timer interrupt), and if I step through it gets stuck in the while(1) loop at the end of main.

    #include <stdint.h>
    #include <stdbool.h>
    #include "inc/hw_types.h"
    #include "driverlib/sysctl.h"
    #include "inc/tm4c123gh6pm.h"
    #include "driverlib/adc.h"
    #include "driverlib/gpio.h"
    #include "driverlib/debug.h"
    #include "inc/hw_memmap.h"
    #include "driverlib/timer.h"
    #include "driverlib/interrupt.h"
    
    uint32_t ADC0Values[1]; // ADC value
    
    int main(void) {
    
    	SysCtlClockSet(SYSCTL_SYSDIV_5|SYSCTL_USE_PLL|SYSCTL_OSC_MAIN|SYSCTL_XTAL_16MHZ); // 40 Mhz
    
    	// GPIO configuration
    	SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE);
    	GPIOPinTypeADC(GPIO_PORTE_BASE, GPIO_PIN_3); // E3 is input
    //	SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF); // LEDs
    //	GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3);
    
    	// ADC configuration - setup channel 0 with SS1
    	SysCtlADCSpeedSet(SYSCTL_ADCSPEED_500KSPS);
    	SysCtlPeripheralEnable(SYSCTL_PERIPH_ADC0);
    	ADCSequenceDisable(ADC0_BASE, 1); // disable for configuration
    	ADCSequenceConfigure(ADC0_BASE, 1, ADC_TRIGGER_PROCESSOR, 0);
    	ADCSequenceStepConfigure(ADC0_BASE,1,0, ADC_CTL_CH0||ADC_CTL_END); // why channel 0?
    	ADCSequenceEnable(ADC0_BASE, 1);
    
    	// Timer configuration
    	SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0);
    	TimerConfigure(TIMER0_BASE, TIMER_CFG_PERIODIC);
    	TimerLoadSet(TIMER0_BASE, TIMER_A, SysCtlClockGet()); // 1 Hz
    
    	// Interrupts configuration
    	IntEnable(INT_TIMER0A); // enable interrupts 3 times for some reason
    	TimerIntEnable(TIMER0_BASE, TIMER_TIMA_TIMEOUT);
    	IntMasterEnable();
    	TimerEnable(TIMER0_BASE, TIMER_A);
    
    	while(1) {
    		// NOP
    	}
    }
    
    void Timer0IntHandler(void) {
    	TimerIntClear(TIMER0_BASE, TIMER_TIMA_TIMEOUT);
    	ADCIntClear(ADC0_BASE, 1);
    	ADCProcessorTrigger(ADC0_BASE, 1);
    	while(!ADCIntStatus(ADC0_BASE, 1, false)) {
    		// wait
    	}
    	ADCSequenceDataGet(ADC0_BASE, 1, ADC0Values);
    
    	// blink LED so we know you got a value
    //	if(GPIOPinRead(GPIO_PORTF_BASE, GPIO_PIN_2)) {
    //		GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3, 0);
    //	} else {
    //		GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_2, 4);
    //	}
    }
    

  • Hello James,

    But which function causes the Fault? You would have to Step through the code to figure that part out.

    I will update the code that may point to the issue. After every PeripheralEnable I am adding a loop to check if Peripheral is Read. Secondly the ADCData variable has to be parsed as Address.

    #include <stdint.h>
    #include <stdbool.h>
    #include "inc/hw_types.h"
    #include "driverlib/sysctl.h"
    #include "inc/tm4c123gh6pm.h"
    #include "driverlib/adc.h"
    #include "driverlib/gpio.h"
    #include "driverlib/debug.h"
    #include "inc/hw_memmap.h"
    #include "driverlib/timer.h"
    #include "driverlib/interrupt.h"
    
    uint32_t ADC0Values[1]; // ADC value
    
    int main(void) {
    
    	SysCtlClockSet(SYSCTL_SYSDIV_5|SYSCTL_USE_PLL|SYSCTL_OSC_MAIN|SYSCTL_XTAL_16MHZ); // 40 Mhz
    
    	// GPIO configuration
    	SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE);
            while(!SysCtlPeripheralReady(SYSCTL_PERIPH_GPIOE));

    GPIOPinTypeADC(GPIO_PORTE_BASE, GPIO_PIN_3); // E3 is input // SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF); // LEDs // GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3); // ADC configuration - setup channel 0 with SS1 SysCtlADCSpeedSet(SYSCTL_ADCSPEED_500KSPS); SysCtlPeripheralEnable(SYSCTL_PERIPH_ADC0); while(!SysCtlPeripheralReady(SYSCTL_PERIPH_ADC0));

    ADCSequenceDisable(ADC0_BASE, 1); // disable for configuration ADCSequenceConfigure(ADC0_BASE, 1, ADC_TRIGGER_PROCESSOR, 0); ADCSequenceStepConfigure(ADC0_BASE,1,0, ADC_CTL_CH0||ADC_CTL_END); // why channel 0? ADCSequenceEnable(ADC0_BASE, 1); // Timer configuration SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0); while(!SysCtlPeripheralReady(SYSCTL_PERIPH_TIMER0));

    TimerConfigure(TIMER0_BASE, TIMER_CFG_PERIODIC); TimerLoadSet(TIMER0_BASE, TIMER_A, SysCtlClockGet()); // 1 Hz // Interrupts configuration IntEnable(INT_TIMER0A); // enable interrupts 3 times for some reason TimerIntEnable(TIMER0_BASE, TIMER_TIMA_TIMEOUT); IntMasterEnable(); TimerEnable(TIMER0_BASE, TIMER_A); while(1) { // NOP } } void Timer0IntHandler(void) { TimerIntClear(TIMER0_BASE, TIMER_TIMA_TIMEOUT); ADCIntClear(ADC0_BASE, 1); ADCProcessorTrigger(ADC0_BASE, 1); while(!ADCIntStatus(ADC0_BASE, 1, false)) { // wait } ADCSequenceDataGet(ADC0_BASE, 1, &ADC0Values); // blink LED so we know you got a value // if(GPIOPinRead(GPIO_PORTF_BASE, GPIO_PIN_2)) { // GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3, 0); // } else { // GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_2, 4); // } }

    Regards

    Amit

  • Thanks again Amit, if I run that code then it gets stuck in the while(1) loop in main most of the time, sometimes it goes into the interrupt (I have a breakpoint on TimerIntClear) but waits at the ADCIntStatus loop. I'm confused about why it would have different behavior on multiple runs.

    Stepping through the code doesn't give an error, it just gets to while(1) in main and stays there.

  • Hello James,

    Csn you please send a updated code? Also since you have put a breakpoint and the timer is running at 1Hz, it may be possible that before you can hit the run button the next interrupt has already fired. Also after getting the data you should clear the ADCIntStatus as well.

    Regards

    Amit