0
0
Embedded Cprogramming~10 mins

Timer interrupt for periodic tasks in Embedded C - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Timer interrupt for periodic tasks
Start Timer
Timer counts up
Timer reaches compare value
Interrupt triggered
Interrupt Service Routine (ISR) runs
Perform periodic task
Clear interrupt flag
Return to main program
Back to Timer counts up
The timer counts up until it reaches a set value, triggering an interrupt that runs a special function to perform tasks periodically, then clears the interrupt and continues.
Execution Sample
Embedded C
volatile int flag = 0;
void ISR_Timer() {
  flag = 1; // Set flag on interrupt
  clear_interrupt_flag(); // Clear interrupt flag inside ISR
}
int main() {
  setup_timer_interrupt();
  while(1) {
    if(flag) { flag=0; do_task(); }
  }
}
This code sets a flag in the timer interrupt and the main loop checks the flag to run a periodic task.
Execution Table
StepTimer CountInterrupt?ISR ActionFlag ValueMain Loop Action
10NoNo0No task
21NoNo0No task
32NoNo0No task
43YesSet flag=11Task runs, flag reset to 0
50NoNo0No task
61NoNo0No task
72NoNo0No task
83YesSet flag=11Task runs, flag reset to 0
💡 Timer keeps running and interrupt triggers periodically at count 3
Variable Tracker
VariableStartAfter Step 4After Step 4 Main LoopAfter Step 8After Step 8 Main Loop
flag01010
Timer Count03333
Key Moments - 2 Insights
Why does the main loop check the flag instead of running the task directly in the ISR?
The ISR should be very fast and not do heavy work. The flag signals the main loop to run the task safely outside the interrupt (see execution_table step 4 and main loop action).
What happens if the interrupt flag is not cleared inside the ISR?
The interrupt will keep triggering repeatedly, causing the ISR to run continuously and blocking the main program (implied in concept_flow step 'Clear interrupt flag').
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4, what is the value of 'flag' after the ISR runs?
A0
B1
CUndefined
D-1
💡 Hint
Check the 'Flag Value' column at step 4 in the execution_table.
At which step does the main loop reset the flag back to 0?
AStep 4 Main Loop Action
BStep 3
CStep 4
DStep 5
💡 Hint
Look at the 'Main Loop Action' column in the execution_table after step 4.
If the timer compare value changes from 3 to 5, how would the interrupt steps change?
AInterrupt never triggers
BInterrupt triggers at Timer Count 3 still
CInterrupt triggers at Timer Count 5 instead of 3
DInterrupt triggers continuously
💡 Hint
Refer to the 'Timer Count' and 'Interrupt?' columns in the execution_table.
Concept Snapshot
Timer interrupt runs a special function when timer reaches a set count.
ISR sets a flag to signal main code.
Main loop checks flag and runs periodic tasks.
ISR must be short and clear interrupt flags.
Timer repeats to create periodic behavior.
Full Transcript
A timer counts up continuously. When it reaches a preset value, it triggers an interrupt. The interrupt runs a small function called ISR that sets a flag variable. The main program loop checks this flag regularly. When the flag is set, the main loop runs the periodic task and resets the flag. This way, the heavy work is done outside the interrupt, keeping the system responsive. The interrupt flag must be cleared inside the ISR to avoid repeated triggers. Changing the timer compare value changes how often the interrupt happens.