0
0
Embedded Cprogramming~10 mins

Writing an ISR (Interrupt Service Routine) in Embedded C - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Writing an ISR (Interrupt Service Routine)
Interrupt Occurs
CPU Pauses Main Code
Jump to ISR Function
Execute ISR Code
Clear Interrupt Flag
Return from ISR
Resume Main Code Execution
When an interrupt happens, the CPU stops the main program, runs the ISR code to handle the event, then returns to the main program.
Execution Sample
Embedded C
void ISR_Timer(void) {
    timer_flag = 1;  // Set flag to signal timer event
    clear_timer_interrupt();  // Clear interrupt flag
}
This ISR sets a flag when a timer interrupt occurs and clears the interrupt flag to allow future interrupts.
Execution Table
StepEventActionVariable ChangeNext Step
1Timer Interrupt OccursCPU pauses main codeNo changeJump to ISR_Timer
2Enter ISR_TimerSet timer_flag = 1timer_flag: 0 -> 1Clear interrupt flag
3Inside ISR_TimerCall clear_timer_interrupt()Interrupt flag clearedReturn from ISR
4Return from ISRCPU resumes main codeNo changeMain code continues
5Main codeCheck timer_flagtimer_flag = 1Handle timer event
💡 ISR finishes and returns, main program resumes with updated timer_flag
Variable Tracker
VariableStartAfter Step 2After Step 3Final
timer_flag0111
interrupt_flagsetsetclearedcleared
Key Moments - 3 Insights
Why must the interrupt flag be cleared inside the ISR?
If the interrupt flag is not cleared (see Step 3 in execution_table), the CPU will think the interrupt is still active and may immediately re-enter the ISR, causing an infinite loop.
Does the main program continue running while the ISR executes?
No, as shown in Step 1 and Step 4, the CPU pauses the main program to run the ISR, then resumes it after the ISR finishes.
Why do we set a flag variable inside the ISR instead of doing all work there?
The ISR should be short and fast (Step 2). Setting a flag lets the main program handle the detailed work later, avoiding long ISR execution.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of timer_flag after Step 2?
A1
B0
CUndefined
D2
💡 Hint
Check the 'Variable Change' column in Step 2 where timer_flag changes from 0 to 1.
At which step does the CPU resume the main program?
AStep 2
BStep 4
CStep 3
DStep 1
💡 Hint
Look at the 'Next Step' column and 'Action' in Step 4 indicating return from ISR and resume main code.
If the interrupt flag is not cleared in Step 3, what happens?
AISR will not run again
BMain program runs faster
CISR may run repeatedly causing a loop
Dtimer_flag resets to 0
💡 Hint
Refer to key_moments explanation about clearing interrupt flag to avoid repeated ISR calls.
Concept Snapshot
ISR (Interrupt Service Routine) runs when an interrupt occurs.
CPU pauses main code, jumps to ISR function.
ISR must clear interrupt flag to avoid repeated calls.
Keep ISR short; use flags to signal main code.
After ISR, CPU resumes main program.
Full Transcript
When an interrupt happens, the CPU stops the main program and jumps to the ISR function. The ISR runs code to handle the interrupt, such as setting a flag and clearing the interrupt flag. Clearing the interrupt flag is important to prevent the ISR from running repeatedly. After the ISR finishes, the CPU returns to the main program, which can check the flag and respond accordingly. This process ensures quick response to events without long delays in the ISR.