0
0
Embedded Cprogramming~7 mins

Memory layout on a microcontroller (Flash, SRAM, EEPROM) in Embedded C

Choose your learning style9 modes available
Introduction

Microcontrollers use different memory types to store code and data. Knowing where things go helps your program run correctly and saves space.

When you want to store your program code safely so it doesn't get lost when power is off.
When you need temporary space to keep data while your program runs.
When you want to save small amounts of data that must stay even after power off, like settings.
When debugging memory issues or optimizing your program size.
When writing embedded C programs that interact directly with hardware memory.
Syntax
Embedded C
/* Example memory sections in embedded C */

// Flash memory: stores program code
const char program_code[] = "Hello";

// SRAM: stores variables during program run
int runtime_variable = 10;

// EEPROM: stores data that must persist after power off
#include <avr/eeprom.h>
uint8_t EEMEM saved_value = 5;

Flash is non-volatile memory where your program code lives.

SRAM is fast, temporary memory for variables while the program runs.

Examples
This string is stored in Flash memory and does not change.
Embedded C
/* Flash stores program code */
const char greeting[] = "Hi!";
This variable is stored in SRAM and can change while the program runs.
Embedded C
/* SRAM stores variables during run */
int counter = 0;
This value is saved in EEPROM and keeps its value even if power is lost.
Embedded C
/* EEPROM stores persistent data */
#include <avr/eeprom.h>
uint8_t EEMEM config_value = 42;
EEPROM variable declared but not initialized, contains unknown data until set.
Embedded C
/* Edge case: empty EEPROM variable */
#include <avr/eeprom.h>
uint8_t EEMEM empty_value;
Sample Program

This program shows how Flash stores the program string, SRAM holds a copy for use, and EEPROM keeps a counter that survives power off.

Embedded C
#include <avr/io.h>
#include <avr/eeprom.h>
#include <util/delay.h>
#include <stdio.h>

// Simulated Flash data
const char flash_message[] = "Flash Memory";

// SRAM variable
char sram_buffer[20];

// EEPROM variable
uint8_t EEMEM eeprom_counter = 0;

int main(void) {
    // Copy flash message to SRAM buffer
    for (int i = 0; i < sizeof(flash_message); i++) {
        sram_buffer[i] = flash_message[i];
    }

    // Read EEPROM counter
    uint8_t counter = eeprom_read_byte(&eeprom_counter);

    // Increment counter and save back to EEPROM
    counter++;
    eeprom_write_byte(&eeprom_counter, counter);

    // Print values (simulated)
    printf("Flash message: %s\n", sram_buffer);
    printf("EEPROM counter: %d\n", counter);

    while (1) {
        _delay_ms(1000);
    }

    return 0;
}
OutputSuccess
Important Notes

Time complexity: Accessing Flash and SRAM is fast; EEPROM writes are slower and limited in number.

Space complexity: Flash is large for code; SRAM is smaller for variables; EEPROM is very small for persistent data.

Common mistake: Trying to write to Flash or SRAM to save data permanently. Use EEPROM for that.

Use EEPROM only for data that must survive power loss, because writing to it is slower and wears out memory.

Summary

Flash memory stores your program code and is non-volatile.

SRAM is temporary memory for variables while the program runs.

EEPROM stores small amounts of data that must stay after power off.