An ISR is a special function that runs automatically when a hardware event happens. It helps your program respond quickly without waiting.
Writing an ISR (Interrupt Service Routine) in Embedded C
void ISR_Name(void) __interrupt (interrupt_number) {
// code to handle interrupt
}The function name can be anything but must match the interrupt vector.
The __interrupt keyword tells the compiler this is an ISR.
void Timer0_ISR(void) __interrupt (1) {
// handle Timer0 interrupt
}void External0_ISR(void) __interrupt (0) { // handle external interrupt 0 }
This program uses Timer0 interrupt to increase a counter every time the timer overflows. The ISR runs automatically when the timer interrupt occurs.
#include <reg51.h> volatile unsigned int count = 0; void Timer0_ISR(void) __interrupt (1) { count++; // Increase count every timer interrupt TH0 = 0x3C; // Reload timer high byte TL0 = 0xB0; // Reload timer low byte } void main() { TMOD = 0x01; // Timer0 mode 1 (16-bit) TH0 = 0x3C; // Load timer high byte TL0 = 0xB0; // Load timer low byte ET0 = 1; // Enable Timer0 interrupt EA = 1; // Enable global interrupts TR0 = 1; // Start Timer0 while(1) { // Main loop does nothing, count updates in ISR } }
ISRs should be short and fast to avoid delaying other interrupts.
Do not use functions like printf inside an ISR because they take too long.
Remember to clear interrupt flags if your hardware requires it to avoid repeated interrupts.
ISRs run automatically when hardware events happen.
Use __interrupt keyword to define an ISR in embedded C.
Keep ISR code short and simple for fast response.
Practice
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 BQuick Check:
ISR = handle hardware events [OK]
- Thinking ISR runs like normal functions
- Confusing ISR with main program loop
- Assuming ISR manages memory
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').Final Answer:
void ISR_Handler() __interrupt(0) { /* code */ } -> Option CQuick Check:
ISR syntax = __interrupt(vector) [OK]
- Omitting interrupt vector number
- Using 'interrupt' instead of '__interrupt'
- Using non-standard keywords
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;
}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 AQuick Check:
ISR sets flag = 1, so return = 1 [OK]
- Ignoring ISR effect on flag
- Assuming flag stays 0
- Confusing volatile usage
void ISR_UART() __interrupt(4) {
if (UART_Interrupt_Flag) {
// Handle UART interrupt
UART_Interrupt_Flag = 0;
}
}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 DQuick Check:
Interrupt flags must be volatile [OK]
- Not declaring flags volatile
- Wrong ISR declaration keyword
- Expecting ISR to return a value
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 AQuick Check:
Handle event first, then clear flag [OK]
- Clearing flag before handling event
- Leaving flag uncleared causing repeated interrupts
- Handling event outside ISR
Ready to prove it?
Pass the test and this pattern moves unknown
