📖 Scenario: You are programming a simple embedded system that controls an LED. The LED should toggle its state (on/off) every time a button is pressed. The button press triggers an interrupt, and you need to write an Interrupt Service Routine (ISR) to handle this event.
🎯 Goal: Build a program that sets up an ISR to toggle an LED when a button press interrupt occurs.
📋 What You'll Learn
Create a variable to hold the LED state
Create a flag variable to indicate the button press
Write an ISR function named button_ISR that sets the flag
Write a main loop that checks the flag and toggles the LED state
Print the LED state changes
💡 Why This Matters
🌍 Real World
Embedded systems often use ISRs to respond quickly to hardware events like button presses, sensors, or communication signals.
💼 Career
Understanding ISRs is essential for embedded software engineers working on microcontrollers, IoT devices, and real-time systems.
Progress0 / 4 steps
1
DATA SETUP: Create variables for LED state and button flag
Create an integer variable called led_state and set it to 0. Create another integer variable called button_pressed and set it to 0.
Embedded C
Hint
Use int led_state = 0; and int button_pressed = 0; to create the variables.
2
CONFIGURATION: Write the ISR function to set the button flag
Write a function called button_ISR with no parameters and void return type. Inside it, set button_pressed to 1.
Embedded C
Hint
Define void button_ISR(void) and set button_pressed = 1; inside it.
3
CORE LOGIC: Write the main loop to toggle LED on button press
Write a main function with an infinite loop. Inside the loop, check if button_pressed is 1. If yes, toggle led_state between 0 and 1, reset button_pressed to 0.
Embedded C
Hint
Use while (1) for infinite loop, check button_pressed == 1, toggle led_state with led_state = !led_state;, then reset button_pressed.
4
OUTPUT: Print the LED state when toggled
Inside the if block in main, add a printf statement to print "LED is ON" if led_state is 1, else print "LED is OFF".
Embedded C
Hint
Use printf("LED is ON\n"); when led_state == 1, else printf("LED is OFF\n");.
Practice
(1/5)
1. What is the main purpose of an ISR (Interrupt Service Routine) in embedded C?
easy
A. To allocate memory dynamically
B. To handle hardware events immediately when they occur
C. To run the main program loop continuously
D. To compile the program code
Solution
Step 1: Understand ISR role
An ISR is a special function that runs automatically when a hardware event happens, like a button press or timer overflow.
Step 2: Compare options
Only To handle hardware events immediately when they occur describes this immediate handling of hardware events. Other options describe unrelated tasks.
Final Answer:
To handle hardware events immediately when they occur -> Option B
Quick Check:
ISR = handle hardware events [OK]
Hint: ISRs respond instantly to hardware signals [OK]
Common Mistakes:
Thinking ISR runs like normal functions
Confusing ISR with main program loop
Assuming ISR manages memory
2. Which of the following is the correct way to declare an ISR function in embedded C?
easy
A. void ISR_Handler() __interrupt { /* code */ }
B. void ISR_Handler() interrupt { /* code */ }
C. void ISR_Handler() __interrupt(0) { /* code */ }
D. void ISR_Handler() __isr { /* code */ }
Solution
Step 1: Recall ISR syntax in embedded C
ISRs are declared with the __interrupt keyword followed by the interrupt vector number in parentheses.
Step 2: Check options
void ISR_Handler() __interrupt(0) { /* code */ } uses __interrupt(0), which is the correct syntax. The others either miss the vector number, use the wrong keyword ('interrupt'), or a non-standard keyword ('__isr').
Hint: Use __interrupt(vector_number) to declare ISR [OK]
Common Mistakes:
Omitting interrupt vector number
Using 'interrupt' instead of '__interrupt'
Using non-standard keywords
3. Given the following ISR code snippet, what will be the value of flag after the interrupt occurs?
volatile int flag = 0;
void ISR_Timer() __interrupt(1) {
flag = 1;
// Interrupt flag cleared here
}
int main() {
flag = 0;
// Interrupt occurs here
// ISR_Timer runs
return flag;
}
medium
A. 1
B. 0
C. Undefined behavior
D. Compilation error
Solution
Step 1: Understand ISR effect on flag
The ISR sets the variable 'flag' to 1 when the interrupt occurs.
Step 2: Check main function return
Main sets flag to 0, then interrupt triggers ISR which sets flag to 1, so return value is 1.
Final Answer:
1 -> Option A
Quick Check:
ISR sets flag = 1, so return = 1 [OK]
Hint: ISR changes flag to 1 before main returns [OK]
C. ISR must be declared with 'interrupt' keyword, not '__interrupt'
D. Missing volatile keyword for UART_Interrupt_Flag variable
Solution
Step 1: Check variable usage in ISR
Interrupt flags accessed in ISR should be declared volatile to prevent compiler optimization issues.
Step 2: Analyze other options
Clearing flags inside ISR is correct practice; __interrupt is correct keyword; ISR functions return void, not int.
Final Answer:
Missing volatile keyword for UART_Interrupt_Flag variable -> Option D
Quick Check:
Interrupt flags must be volatile [OK]
Hint: Declare interrupt flags volatile to avoid optimization bugs [OK]
Common Mistakes:
Not declaring flags volatile
Wrong ISR declaration keyword
Expecting ISR to return a value
5. You want to write an ISR for a timer interrupt that increments a counter and clears the interrupt flag. Which of the following is the best practice to ensure correct ISR behavior?
hard
A. Increment counter, then clear interrupt flag inside ISR
B. Clear interrupt flag, then increment counter inside ISR
C. Increment counter outside ISR and clear flag inside ISR
D. Do not clear interrupt flag; let main program clear it
Solution
Step 1: Understand ISR order for flag and counter
Best practice is to handle the event (increment counter) first, then clear the interrupt flag to acknowledge the interrupt.
Step 2: Evaluate options
Increment counter, then clear interrupt flag inside ISR follows this order. Clear interrupt flag, then increment counter inside ISR clears flag before handling event, which may cause missed interrupts. Increment counter outside ISR and clear flag inside ISR splits logic incorrectly. Do not clear interrupt flag; let main program clear it risks repeated interrupts.
Final Answer:
Increment counter, then clear interrupt flag inside ISR -> Option A
Quick Check:
Handle event first, then clear flag [OK]
Hint: Process event before clearing interrupt flag [OK]
Common Mistakes:
Clearing flag before handling event
Leaving flag uncleared causing repeated interrupts
Handling event outside ISR
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.