Essential for Industrial Control: EEPROM Data Storage in PIC16F Microcontrollers (Includes Ready-to-Use Code)

In industrial control settings, microcontroller main control boards often need to store critical data such as calibration parameters, device addresses, and operation logs. —— These data must not be lost due to power outages, and the built-in PIC16F series microcontrollers’ EEPROM perfectly meets the core requirement of “long-term power-off data retention”.

I have been engaged in industrial control research and development for over thirty years, and the PIC16F series microcontrollers (such as PIC16F1824, PIC16F1947, PIC16F877A, etc.) are the most frequently used. EEPROM is an essential component in every project.

However, many beginners fall into pitfalls such as “data not being written”, “premature wear-out”, “garbled reading”. Today, I will share practical experience to thoroughly explain EEPROM data storage in PIC16F microcontrollers, along with a code template that beginners can use directly.

1. Understand the Core: Why is EEPROM Suitable for Power-Off Data Storage?

The built-in EEPROM in PIC16F microcontrollers is fundamentally different from RAM and Flash, with its core advantage being designed for “long-term power-off storage”:

  • Data retention during power-off: No additional power supply is needed, and data can be stably retained for over 10 years, making it particularly suitable for storing static critical data such as calibration coefficients and device addresses;
  • Flexible erasing: Supports byte-level erasing, unlike Flash which requires sector operations, making it easier to modify individual parameters and improving efficiency during industrial debugging;
  • Ample lifespan: Typical erase/write cycles can exceed 100,000, and in normal industrial scenarios (e.g., reading and writing 10 times a day), it can be used stably for over 10 years, fully meeting the equipment lifecycle requirements;
  • Low power consumption: The power consumption during read/write operations is extremely low, so even battery-powered IoT sensor nodes will not incur significant additional power burdens.

It is important to note: the capacity of EEPROM is limited (PIC16F1824, PIC16F1947, PIC16F877A all have 256 bytes), so do not use it to store large amounts of dynamic data such as continuously collected sensor data. For such scenarios, it is recommended to use external I2C interface EEPROM (e.g., 24C512) or SD cards.

2. Code Template: Just Copy and Use (Example with PIC16F1947)

The following code has been validated in dozens of industrial projects, ensuring maximum stability. Beginners only need to slightly adjust the header files according to their microcontroller model to use it directly, including core functions for “byte read/write” and “bulk storage”:

1. Core Read/Write Functions for EEPROM (Key Steps Marked)

// EEPROM read function: Read data from the specified address

char eeprom_r(char addr)

{

char temp;

GIE=0; // Disable global interrupts to avoid interruption during read/write

EEADR=addr; // Write the address to read from EEPROM (0-255)

RD=1; // Trigger read operation

while(RD); // Wait for read operation to complete

temp=EEDATA; // Retrieve the read data

GIE=1; // Re-enable global interrupts

return(temp);

}

// EEPROM write function: Write data to the specified address

void eeprom_w(char addr,char data)

{

GIE=0; // Disable global interrupts to avoid interruption during read/write

WREN=1; // Enable write operation

EEADR=addr; // Write the target address

EEDATA=data;// Write the data to be stored

//PIC microcontroller write EEPROM must perform these two steps

EECON2=0x55;

EECON2=0xaa;

WR=1; // Trigger write operation

while(WR); // Wait for write operation to complete

WREN=0; // Disable write operation

GIE=1; // Re-enable global interrupts

}

2. Complete Read/Write Demonstration Program (Including Bulk Storage Logic)

#include<pic16f1947.h> // Example with PIC16F1947, replace with the corresponding header file for other models

unsigned char eeprom_e2[256]; //EEPROM data buffer (corresponding to 256 bytes capacity)

void main()

{

unsigned char i,j;

// Initialization

for(i=0; i<256; i++) // Read EEPROM data into the buffer, 256 represents any number from 1 to 256

{

eeprom_e2[i]=eeprom_r(i);

}

// You can add program logic here to parse the data read from EEPROM.

while(1)

{

if(x) // Condition to write to EEPROM x

{

for(i=0;i<8;i++) // Write 8 bytes (0-7) in total (8 can be any number from 1 to 256)

{

eeprom_w(i,eeprom_e2[i]);

}

}

if(y) // Condition to write to EEPROM y

{

for(i=8;i<j;i++) // Write j-8 bytes in total, j<=256

{

eeprom_w(i,eeprom_e2[i]);

}

}

}

}

3. Pitfall Avoidance Tips: Don’t Make These Mistakes! (Summary of My Mistakes)

  1. Forget to disable global interrupts: If interrupted during read/write, it can lead to data corruption. Always include “GIE=0” and “GIE=1” to wrap operations;
  2. Omit unlock sequence: Writing to PIC microcontroller’s EEPROM requires executing “EECON2=0x55; EECON2=0xaa;”, missing these two steps will result in a direct write failure, which is a common mistake for beginners;
  3. Not waiting for operation completion: If the read operation does not wait for RD to clear to 0, or the write operation does not wait for WR to clear to 0 before proceeding to the next step, it can lead to incomplete data reading or write failure. Always include a while loop to wait;
  4. Frequent erasing of the same address: Even though EEPROM has a lifespan of 100,000 cycles, writing to the same address every second will quickly exhaust it. It is recommended to implement “multi-address balanced writing” (e.g., spreading operation logs across different addresses) to extend lifespan;
  5. Out-of-bounds address operations: The EEPROM address only ranges from 0-255, writing to address 256 or above will overwrite lower address data;
  6. Ignoring power stability: Voltage fluctuations during write operations can lead to write failures. In industrial scenarios, it is recommended to add a 100μF electrolytic capacitor and a 0.1μF ceramic capacitor at the power supply end to stabilize power.

Have you encountered any issues while using PIC microcontroller’s EEPROM? For example, “garbled data reading” or “write operation failure”, feel free to leave a comment, and I will answer each one!

More valuable content is coming, let’s progress together in the world of microcontrollers.

Leave a Comment