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.