Overview
When we write programs in C for a microcontroller, we’re not just working with variables and functions—we’re also interacting directly with the device’s memory. Every microcontroller organizes its memory into a memory map, which is like a blueprint showing where different types of data live.
This map divides memory into regions with specific jobs:
- FLASH memory stores the program instructions so the microcontroller knows what to do.
- SRAM holds temporary data, like variables and calculations, while the program runs.
- Peripheral registers let the program talk to built-in hardware features, such as timers or communication ports.
Next, we’ll look at some C code examples to see how different types of variables end up in different parts of the microcontroller’s memory. Understanding this will help you learn how to access FLASH, SRAM, and peripheral registers in your programs.
Accessing Flash
Accessing Flash memory in a C program usually happens automatically when the microcontroller runs your code. After you write your application in C, the compiler translates it into assembly, and then into machine instructions that are stored in the microcontroller’s Flash memory. When the device powers up, the program counter begins fetching these instructions from Flash and executing the program by starting at the main() function.
The size of your program determines how much Flash is required. Larger, more complex applications need more memory to store all the instructions. This is why efficient coding and optimization matter: they help keep your program within the limits of the microcontroller’s Flash.
In addition to storing program instructions, Flash can also hold read-only global variables. These are values that never change during program execution, such as fixed messages or configuration data. Because most microcontrollers have more Flash than SRAM, placing these constants in Flash helps conserve SRAM for variables that do change.
In C, we do this by declaring the variable with the const keyword. For example, the global variable PROMPT below is stored in Flash because it is marked as const. Although it resides in Flash, it is not part of the program instructions—the compiler keeps read-only data separate from executable code.
