0
0
Power-electronicsHow-ToBeginner · 3 min read

How to Store Data in EEPROM Using Embedded C

To store data in EEPROM using Embedded C, use functions to write bytes or blocks to specific EEPROM addresses and read them back when needed. Typically, you use eeprom_write_byte() and eeprom_read_byte() or similar functions provided by your microcontroller's library to access EEPROM memory safely.
📐

Syntax

EEPROM access usually involves specifying the address and the data to write or read. Common functions are:

  • eeprom_write_byte(address, data): Writes a byte to the EEPROM at the given address.
  • eeprom_read_byte(address): Reads a byte from the EEPROM at the given address.

Here, address is the EEPROM memory location, and data is the byte value to store.

c
void eeprom_write_byte(uint8_t* address, uint8_t data);
uint8_t eeprom_read_byte(const uint8_t* address);
💻

Example

This example shows how to write a byte to EEPROM and then read it back to verify the stored value.

c
#include <avr/io.h>
#include <avr/eeprom.h>
#include <stdio.h>

int main(void) {
    uint16_t eeprom_address = 0x00; // EEPROM address to store data
    uint8_t data_to_store = 123;    // Data byte to store
    uint8_t read_data = 0;

    // Write data to EEPROM
    eeprom_write_byte((uint8_t*)eeprom_address, data_to_store);

    // Read data back from EEPROM
    read_data = eeprom_read_byte((const uint8_t*)eeprom_address);

    // Normally you would use the data here
    // For demonstration, we just loop infinitely
    while(1) {
        // Use read_data as needed
    }

    return 0;
}
Output
No console output; data stored and read from EEPROM successfully.
⚠️

Common Pitfalls

Common mistakes when working with EEPROM include:

  • Writing to EEPROM too frequently, which can wear out the memory.
  • Not waiting for the write operation to complete before reading.
  • Using wrong address types or casting incorrectly.
  • Forgetting to include the proper EEPROM library or headers.

Always check your microcontroller's datasheet for EEPROM size and write cycle limits.

c
/* Wrong way: Writing without casting address properly */
eeprom_write_byte(0x00, 42); // May cause compiler warning or error

/* Right way: Cast address to pointer type */
eeprom_write_byte((uint8_t*)0x00, 42);
📊

Quick Reference

FunctionPurposeParameters
eeprom_write_byteWrite a byte to EEPROMuint8_t* address, uint8_t data
eeprom_read_byteRead a byte from EEPROMconst uint8_t* address
eeprom_update_byteWrite byte only if differentuint8_t* address, uint8_t data
eeprom_write_blockWrite multiple bytesconst void* src, void* dest, size_t size
eeprom_read_blockRead multiple bytesvoid* dest, const void* src, size_t size

Key Takeaways

Use the correct EEPROM functions like eeprom_write_byte and eeprom_read_byte with proper address casting.
Avoid frequent writes to EEPROM to prevent memory wear.
Always check your microcontroller's datasheet for EEPROM size and write cycle limits.
Wait for write operations to complete before reading back data.
Include the proper EEPROM library headers for your platform.