An interrupt vector table helps the microcontroller know which code to run when something important happens, like a button press or a timer finishing.
Interrupt vector table in Embedded C
typedef void (*ISR)(void);
ISR interrupt_vector_table[] __attribute__((section(".isr_vector"))) = {
Reset_Handler,
NMI_Handler,
HardFault_Handler,
// other interrupt handlers
};The interrupt vector table is an array of function pointers.
Each entry points to a function called when that interrupt happens.
void Reset_Handler(void) {
// code to run on reset
}
void NMI_Handler(void) {
// code for non-maskable interrupt
}
ISR interrupt_vector_table[] __attribute__((section(".isr_vector"))) = {
Reset_Handler,
NMI_Handler
};void Timer_Handler(void) {
// code to handle timer interrupt
}
ISR interrupt_vector_table[] __attribute__((section(".isr_vector"))) = {
Reset_Handler,
NMI_Handler,
HardFault_Handler,
Timer_Handler
};This program defines three interrupt handlers and an interrupt vector table. It simulates interrupts by calling the handlers directly from the table.
#include <stdio.h> typedef void (*ISR)(void); void Reset_Handler(void) { printf("Reset interrupt triggered\n"); } void NMI_Handler(void) { printf("NMI interrupt triggered\n"); } void HardFault_Handler(void) { printf("HardFault interrupt triggered\n"); } ISR interrupt_vector_table[] __attribute__((section(".isr_vector"))) = { Reset_Handler, NMI_Handler, HardFault_Handler }; int main() { // Simulate interrupts by calling handlers directly interrupt_vector_table[0](); // Reset interrupt_vector_table[1](); // NMI interrupt_vector_table[2](); // HardFault return 0; }
The interrupt vector table must be placed at a specific memory location, usually at the start of the program memory.
Each microcontroller has its own rules about the order and number of interrupts in the table.
In real embedded systems, the hardware automatically jumps to the correct handler when an interrupt occurs.
The interrupt vector table links interrupts to their handler functions.
It is an array of function pointers placed in a special memory section.
Handlers run automatically when their interrupt happens, making your program responsive.
Practice
What is the main purpose of an interrupt vector table in embedded C programming?
Solution
Step 1: Understand the role of interrupt vector table
The interrupt vector table is a special array that holds addresses of interrupt handler functions.Step 2: Identify its purpose in program flow
When an interrupt occurs, the processor uses this table to jump to the correct handler automatically.Final Answer:
To link interrupt events to their handler functions -> Option BQuick Check:
Interrupt vector table = links interrupts to handlers [OK]
- Thinking it stores variables
- Confusing with memory management
- Assuming it performs calculations
Which of the following is the correct way to declare an interrupt vector table as an array of function pointers in embedded C?
void (*interrupt_vector_table[])(void) = {Solution
Step 1: Recognize function pointer array syntax
Function pointers to void functions with no parameters are declared asvoid (*name[])(void).Step 2: Match declaration with initialization
void (*interrupt_vector_table[])(void) = {handler1, handler2}; correctly declares an array of such pointers and initializes it with handler functions.Final Answer:
void (*interrupt_vector_table[])(void) = {handler1, handler2}; -> Option CQuick Check:
Function pointer array syntax = void (*interrupt_vector_table[])(void) = {handler1, handler2}; [OK]
- Using int type instead of function pointers
- Declaring as a single function, not array
- Using void* without function pointer cast
Given the following code snippet, what will be the output when the interrupt at index 1 occurs?
void handler0(void) { printf("Handler 0 called\n"); }
void handler1(void) { printf("Handler 1 called\n"); }
void (*interrupt_vector_table[])(void) = {handler0, handler1};
// Simulate interrupt 1
interrupt_vector_table[1]();Solution
Step 1: Identify the function called by interrupt_vector_table[1]
Index 1 points tohandler1, which prints "Handler 1 called".Step 2: Understand the output of the function call
Callinginterrupt_vector_table[1]()executeshandler1, printing the message.Final Answer:
Handler 1 called -> Option AQuick Check:
interrupt_vector_table[1]() calls handler1 [OK]
- Confusing index 0 and 1
- Expecting no output or error
- Misreading function pointer call syntax
Consider this interrupt vector table declaration:
void handlerA(void) { /* ... */ }
void handlerB(void) { /* ... */ }
void (*interrupt_vector_table[])(void) = {handlerA, handlerB};
// Somewhere else in code
interrupt_vector_table[2]();What is the problem here?
Solution
Step 1: Check the size of interrupt_vector_table
The array has 2 elements at indices 0 and 1 only.Step 2: Analyze the call to interrupt_vector_table[2]()
Index 2 is outside the array bounds, leading to undefined behavior or crash.Final Answer:
Index 2 is out of bounds, causing undefined behavior -> Option AQuick Check:
Accessing beyond array size = out of bounds error [OK]
- Assuming array auto-expands
- Thinking handlerB is invalid
- Ignoring out-of-bounds errors
You want to add a new interrupt handler handler2 to an existing vector table with 2 handlers. Which is the correct way to update the vector table to include handler2 at index 2?
void handler0(void) { /* ... */ }
void handler1(void) { /* ... */ }
void handler2(void) { /* ... */ }
// Existing vector table
void (*interrupt_vector_table[])(void) = {handler0, handler1};
// How to add handler2 correctly?Solution
Step 1: Understand array size fixed at declaration
Arrays in C have fixed size; you cannot add elements dynamically.Step 2: Update vector table by redefining with all handlers
To include handler2, redefine the array with three elements: {handler0, handler1, handler2}.Final Answer:
Redefine the vector table with all three handlers: {handler0, handler1, handler2} -> Option DQuick Check:
Arrays fixed size; redefine to add elements [OK]
- Trying to assign beyond array size
- Using pointer casts unsafely
- Assuming interrupts beyond defined handlers work
Ready to prove it?
Pass the test and this pattern moves unknown
