Course contentsShow
← Embedded Systems & Computer Architecture
Lesson 185 of 9363. Microcontroller Fundamentals and Bare-Metal CFree lesson

Mapping the Microcontroller Vector Table

Learners will identify the memory structure of the vector table and explain how the processor uses it to locate critical exception handlers.

In this lesson, you will learn how the processor uses a dedicated memory structure called the Vector Table to automatically locate and execute the correct code when an interrupt or exception occurs.

The Brain’s Reflex System

Imagine the microcontroller is a high-speed worker. When an event happens—like a timer expiring or a button press—the processor needs to stop what it’s doing and jump to a specific function (the "Handler") to deal with that event. It doesn't have time to search through your entire codebase to find where those functions are. Instead, it relies on a Vector Table.

The Vector Table is simply an array of memory addresses stored at a fixed, predefined location in your chip’s flash memory (usually starting at address 0x00000000). Each entry in this table corresponds to a specific "vector," or exception type.

Visualizing the Memory Map

When the processor encounters an exception, it looks at the vector number, calculates the offset into this table, and reads the address stored there. That address is the starting point of your handler code.

In C, we often represent this table using a struct or an array of function pointers. Here is how a simplified table might look in memory:

typedef void (*Handler)(void);

// A simple vector table representation
const Handler vector_table[] __attribute__((section(".vectors"))) = {
    (Handler)0x20001000,  // [0] Initial Stack Pointer
    Reset_Handler,        // [1] Reset Vector
    NMI_Handler,          // [2] Non-Maskable Interrupt
    HardFault_Handler,    // [3] Hard Fault
    // ... additional interrupts follow
};

Why it Matters

The first entry is special: it’s the stack pointer value the CPU loads into the SP register upon power-on. The second entry, the Reset_Handler, tells the CPU exactly where your main() function begins.

If you map this table incorrectly—for instance, if you accidentally place it at the wrong memory address or provide an invalid function pointer—the processor will "jump into the weeds." It will attempt to execute random data as if it were machine code, leading to an immediate system crash or a hard fault. By strictly defining this memory structure, you provide the hardware with a reliable map to navigate its own startup and interrupt-driven behavior.

Key Takeaway: The Vector Table is a fixed-address look-up table of function pointers that acts as the hardware’s bridge between an asynchronous event and the specific C code designed to handle it.