📖 Scenario: You are programming a microcontroller that reads a button press using an Interrupt Service Routine (ISR). To keep the system responsive and safe, you need to follow best practices for writing ISRs.
🎯 Goal: Build a simple program that sets up a button press ISR with best practices: keep ISR short, use a flag variable, and handle the main logic outside the ISR.
📋 What You'll Learn
Create a volatile flag variable to indicate button press
Write a short ISR that sets the flag
In the main loop, check the flag and clear it
Print a message when the button press is handled
💡 Why This Matters
🌍 Real World
Microcontrollers use ISRs to respond quickly to hardware events like button presses or sensor signals.
💼 Career
Embedded developers must write efficient ISRs to keep devices responsive and avoid bugs.
Progress0 / 4 steps
1
Create a volatile flag variable
Create a volatile int variable called button_pressed and set it to 0.
Embedded C
Hint
Use the keyword volatile to tell the compiler this variable can change unexpectedly.
2
Write a short ISR to set the flag
Write an ISR function called button_isr that sets button_pressed to 1. Keep the ISR short and do not add any other logic.
Embedded C
Hint
ISR should only set the flag and return quickly.
3
Check and clear the flag in main loop
In the main function, write a loop that checks if button_pressed is 1. If yes, print "Button pressed!" and reset button_pressed to 0.
Embedded C
Hint
Use an if statement to check the flag and printf to show the message.
4
Run and observe the output
Run the program and simulate the ISR by calling button_isr() once before the loop. The program should print "Button pressed!" once.
Embedded C
Hint
Calling button_isr() simulates the interrupt. The program prints the message once and exits.
Practice
(1/5)
1. What is the main reason to keep Interrupt Service Routine (ISR) code short in embedded C?
easy
A. To use more memory for ISR variables
B. To enable ISR to call blocking functions safely
C. To allow ISR to run complex calculations
D. To avoid delaying other interrupts and keep system responsive
Solution
Step 1: Understand ISR impact on system
ISRs temporarily stop main code and other interrupts, so long ISRs delay system response.
Step 2: Reason why short ISR is important
Short ISR avoids blocking other interrupts and keeps system fast and responsive.
Final Answer:
To avoid delaying other interrupts and keep system responsive -> Option D
Quick Check:
Keep ISR short = avoid delays [OK]
Hint: Short ISR avoids blocking other interrupts [OK]
Common Mistakes:
Thinking ISR can run long calculations
Using blocking calls inside ISR
Assuming ISR uses more memory for variables
2. Which of the following is the correct way to signal the main program from an ISR?
easy
A. Call a heavy processing function directly inside ISR
B. Print debug messages inside ISR
C. Set a flag variable that main code checks
D. Use a blocking delay inside ISR
Solution
Step 1: Identify safe communication from ISR
Heavy processing or blocking calls inside ISR are unsafe; printing can cause delays.
Step 2: Use flag to communicate
Setting a flag variable is fast and lets main code handle processing safely.
Final Answer:
Set a flag variable that main code checks -> Option C
Quick Check:
Use flags to signal main code [OK]
Hint: Use flags, not heavy calls, inside ISR [OK]
Common Mistakes:
Calling complex functions inside ISR
Using delays or blocking calls in ISR
Printing inside ISR causing slowdowns
3. Given this ISR code snippet, what will be the value of flag after the interrupt triggers once?
volatile int flag = 0;
void ISR() {
flag = 1;
}
medium
A. 1
B. 0
C. Undefined
D. ISR causes error
Solution
Step 1: Understand ISR effect on flag
ISR sets flag to 1 when triggered.
Step 2: Check initial and final flag value
Initially flag is 0, after ISR runs once, flag becomes 1.
Final Answer:
1 -> Option A
Quick Check:
ISR sets flag = 1 [OK]
Hint: ISR sets flag to 1 on trigger [OK]
Common Mistakes:
Assuming flag stays 0
Thinking ISR causes error
Confusing volatile keyword effect
4. Identify the problem in this ISR code:
void ISR() {
while(!data_ready) {
// wait for data
}
process_data();
}
medium
A. Not setting a flag variable
B. Using a blocking loop inside ISR
C. ISR missing return statement
D. Using volatile variable incorrectly
Solution
Step 1: Analyze ISR code behavior
ISR contains a while loop waiting for data_ready, which blocks ISR execution.
Step 2: Understand why blocking is bad in ISR
Blocking inside ISR delays other interrupts and main code, causing system issues.
Final Answer:
Using a blocking loop inside ISR -> Option B
Quick Check:
Blocking loops inside ISR cause problems [OK]
Hint: Avoid loops that wait inside ISR [OK]
Common Mistakes:
Ignoring blocking loops in ISR
Thinking ISR needs return statement
Confusing volatile usage
5. You want to handle a sensor interrupt by reading data and processing it. Which is the best approach following ISR best practices?
Options:
A) Read and process data fully inside ISR
B) Use delay inside ISR to wait for sensor
C) Set a flag in ISR and process data in main loop
D) Print sensor data inside ISR for debugging
hard
A. Set a flag in ISR and process data in main loop
B. Use delay inside ISR to wait for sensor
C. Read and process data fully inside ISR
D. Print sensor data inside ISR for debugging
Solution
Step 1: Evaluate ISR workload options
Processing data fully or using delay inside ISR blocks system; printing slows ISR.
Step 2: Choose safe communication method
Setting a flag in ISR and processing in main loop keeps ISR short and system responsive.
Final Answer:
Set a flag in ISR and process data in main loop -> Option A
Quick Check:
Flag in ISR, process later = best practice [OK]
Hint: Use flags in ISR, process outside [OK]
Common Mistakes:
Doing heavy work inside ISR
Using delays or prints in ISR
Ignoring main loop processing
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.