Consider this embedded C code snippet where an ISR increments a global counter each time an interrupt occurs. What will be the value of counter after the ISR is called 3 times?
volatile int counter = 0; void __attribute__((interrupt)) Timer_ISR(void) { counter++; } int main() { // Simulate 3 interrupts Timer_ISR(); Timer_ISR(); Timer_ISR(); return counter; }
Each call to the ISR increases the counter by one.
The ISR increments the global counter variable by 1 each time it is called. After 3 calls, the value is 3.
In embedded C, which keyword is essential to declare a variable that is shared between the main program and an ISR to prevent compiler optimization issues?
This keyword tells the compiler the variable can change unexpectedly.
The volatile keyword tells the compiler not to optimize access to the variable because it can change at any time, such as inside an ISR.
Examine this ISR declaration. What error will the compiler produce?
void Timer_ISR() {
// ISR code
}
int main() {
// Setup code
return 0;
}ISRs usually need special attributes or linkage to connect to hardware interrupts.
Without the proper interrupt attribute or linkage, the function is not connected to the interrupt vector, causing a linker error.
Choose the correct syntax to declare an ISR for a timer interrupt using GCC compiler extensions.
GCC uses __attribute__((interrupt)) to mark ISRs.
Option D uses the correct GCC syntax with __attribute__((interrupt)) before the function name.
Given this embedded C code where an ISR toggles an LED state on each interrupt, what will be the final value of led_state after 5 interrupts? (1 = ON, 0 = OFF)
volatile int led_state = 0; void __attribute__((interrupt)) Timer_ISR(void) { led_state = !led_state; } int main() { for (int i = 0; i < 5; i++) { Timer_ISR(); } return led_state; }
Each interrupt toggles the LED state from 0 to 1 or 1 to 0.
Starting from 0 (OFF), toggling 5 times results in final state 1 (ON).