I have a situation where the majority of the code is residing inside timer interrupt functions.

Setup() does what it has, loop() basically reads ADCs and then all the code is inside timer interrupts.

void loop()
  //Read the ADCs
  while((ADC->ADC_ISR & 0x1CC0)!=0x1CC0);   // wait for ADC 0, 1, 8, 9, 10 conversion complete.
  in_ADC0=ADC->ADC_CDR[7];                  // read data from ADC0
  in_ADC1=ADC->ADC_CDR[6];                  // read data from ADC1  
  POT0=ADC->ADC_CDR[10];                    // read data from ADC8        
  POT1=ADC->ADC_CDR[11];                    // read data from ADC9   
  POT2=ADC->ADC_CDR[12];                    // read data from ADC10    

void TC4_Handler()
 if (effect==0)       {/*HERE THE CODE FOR EFFECT 0*/}
  else if (effect==1)  {/*HERE THE CODE FOR EFFECT 1*/}
    else if (effect==2){/*HERE THE CODE FOR EFFECT 2*/}
      else             {/*HERE THE CODE FOR EFFECT 3*/}

  // We need to get the status to clear it and allow the interrupt to fire again
  TC_GetStatus(TC1, 1);

The user selects the effect he wants via a button and a hardware interrupt.

What happens when the timer interrupt function ends? Do we go back to loop(), or the timer function is re-run? In this example, all the vars needed inside each functino of the timer interrupt are declared globally.

I was considering if i can declare variables inside each function in the Timer Interrupt function. Therefore, we would save space, by not declaring variables that are not run at the moment. And we would only initialize variables for the effect we want.

Is this possible? Because if the effect i have just selected just re-loops, maybe its not wise for vars to get re-initialized (and their values re-set) each and every time it is looped.

So ultimately, variables taht will be used inside a timer interrupt function, globally or locally?

1 Answer 1


If they are only used in the interrupt then yes they can be declared in the interrupt. If you want them to retain their value from one call to another they must be declared static, and then they will be the same as global variables as far as memory allocation is concerned.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.