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.

ADC interrupt Code for TIVA

Dear All,

I am working on a project using TIVA Launchpad

I am using 12 bit ADC with polling method and it works fine

but I have some trouble with ADC interrupt generation or handling.

 So please send me example code for ADC using  interrupt method.

Regards:-

utpal

 

  • Hello Utpal,

    What exactly is the trouble? Putting such information out would be more useful.

    Also if you do a search on the forum for ADC issues being discussed, you would see plenty of attachments and code posted by forum members.

    Regards

    Amit

  • Hi, this is the code i use in order to access the ADC interrupts periodically triggered by the timer0:

    #include <stdint.h>
    #include <stdbool.h>
    #include "inc/hw_memmap.h"
    #include "inc/hw_types.h"
    #include "inc/tm4c123gh6pm.h"
    #include "driverlib/pin_map.h"
    #include "driverlib/sysctl.h"
    #include "driverlib/debug.h"
    #include "driverlib/gpio.h"
    #include "driverlib/adc.h"
    #include "driverlib/rom.h"
    #include "driverlib/timer.h"
    #include "driverlib/interrupt.h"
    
    // ****** Constants ******
    const int sampleFreq = 8000; // Sample Frequency
    
    // ****** Variables ******
    uint32_t ADC0Value[1];
    uint16_t samplePeriod;
    
    // Definitions
    void systemSetup(void){
    	// *** Clock
    	ROM_SysCtlClockSet(SYSCTL_SYSDIV_2_5|SYSCTL_USE_PLL|SYSCTL_XTAL_16MHZ|SYSCTL_OSC_MAIN);
    
    	// *** Peripheral Enable
    	ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_ADC0);
    	ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0);
    	ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD);
    
    	// *** ADC0
    	ROM_ADCHardwareOversampleConfigure(ADC0_BASE,4);
    	ROM_ADCReferenceSet(ADC0_BASE,ADC_REF_INT);
    	ROM_ADCSequenceConfigure(ADC0_BASE,3,ADC_TRIGGER_TIMER,0);
    	ROM_ADCSequenceStepConfigure(ADC0_BASE,3,0,ADC_CTL_CH7|ADC_CTL_IE|ADC_CTL_END);
    	ROM_ADCSequenceEnable(ADC0_BASE,3);
    
    	// *** Timer0
    	ROM_TimerConfigure(TIMER0_BASE,TIMER_CFG_PERIODIC);
    	samplePeriod = ROM_SysCtlClockGet()/sampleFreq;
    	ROM_TimerLoadSet(TIMER0_BASE,TIMER_A,samplePeriod - 1);
    	ROM_TimerControlTrigger(TIMER0_BASE,TIMER_A,true);
    
    	// *** GPIO
    	ROM_GPIOPinTypeADC(GPIO_PORTD_BASE,GPIO_PIN_0);
    
    	// *** Interrupts
    	ROM_IntEnable(INT_TIMER0A);
    	ROM_TimerIntEnable(TIMER0_BASE,TIMER_TIMA_TIMEOUT);
    	ROM_IntEnable(INT_ADC0SS3);
    	ROM_ADCIntEnable(ADC0_BASE,3);
    
    	ROM_IntMasterEnable();
    	ROM_TimerEnable(TIMER0_BASE,TIMER_A);
    }
    
    // Main program
    int main(void) {
    	systemSetup();
    
    	while(1){
    	}
    }
    
    // Interrupts
    void Timer0IntHandler(void){
    	ROM_TimerIntClear(TIMER0_BASE, TIMER_TIMA_TIMEOUT);
    }
    
    void ADC0SS3IntHandler(void){
    	ROM_ADCIntClear(ADC0_BASE,3);
    
    	ROM_ADCSequenceDataGet(ADC0_BASE,3,ADC0Value); // Get Data from ADC and store it in ADC0Value
    }

    Don't forget to add the Timer0IntHandler and ADC0SS3IntHandler to the vector table. I hope it will be useful for you.

  • Hello Edgar,

    Thanks for sharing your code. The forum would find it useful with more such code examples from members.

    Regards

    Amit

  • Thank you Edgar,

    This is indeed a very nice, clean and short example.
    Especially as I yet wasn't able to find any examples in the TivaWare for ADC with Intrerrupts.