As an Amazon Associate we earn from qualifying purchases.
PIC16F877A EEPROM in HI-TECH C
The PIC16F877A has a small block of data EEPROM inside the chip. Unlike RAM, it keeps its contents when power is removed, so it is a good place for settings, calibration values, counters or a last-known state. The PIC16F877A has 256 bytes of data EEPROM, addressed from 0 to 255.
This article shows how to read and write that memory with the HI-TECH C compiler, using the special function registers directly. It covers the register steps, a complete example and the mistakes that most often cause trouble.
Registers used for data EEPROM
Four registers are involved in reading and writing data EEPROM:
- EEADR holds the address of the byte you want to access.
- EEDATA holds the byte that is read or the byte to be written.
- EECON1 is the control register. The bits used here are EEPGD, WREN, WR and RD.
- EECON2 is not a real register. It exists only so that you can write the two-step unlock sequence before a write.
The EEPGD bit chooses between program memory and data memory. For data EEPROM it must always be cleared (EEPGD=0).
HI-TECH C handles register bank switching for you, so you can use these names like normal variables.
Steps to read from EEPROM
- Specify the memory location in EEADR (EEADR=4;)
- Clear the EEPGD bit (EEPGD=0;)
- Set the RD bit (RD=1;) to start the read
- Read the value from the EEDATA register (unsigned char a=EEDATA;)
The original version of these steps left out the RD bit. Setting RD is what starts the read, so do not skip it. The data is available in EEDATA on the next instruction cycle, so you can read it straight away.
Steps to write to EEPROM
- Specify the memory location in EEADR (EEADR=4;)
- Write the value to the EEDATA register (EEDATA=7;)
- Clear the EEPGD bit (EEPGD=0;)
- Set WREN to enable writes (WREN=1;)
- Disable interrupts (GIE=0;) so the next two steps are not interrupted
- Write 55h to EECON2 (EECON2=0x55;)
- Write AAh to EECON2 (EECON2=0xAA;)
- Set the WR bit (WR=1;) to start the write
- Wait until the write finishes, then clear WREN
The 55h and AAh sequence protects the EEPROM against accidental writes. The two writes must come one right after the other, in this order, with no interrupt between them. If anything gets in the way, the write does not start.
A write takes much longer than a read. The hardware clears the WR bit when the write is done, so you can wait on it before touching the EEPROM again.
Complete HI-TECH C example
The program below writes the character ‘1’ to address 4, reads it back and shows the result on PORTB. The value on PORTB is 0x31, which is the ASCII code of ‘1’.
#include <pic.h>
#define _XTAL_FREQ 16000000
__CONFIG(0x3f7a);
void eewrite(unsigned char address, unsigned char data);
unsigned char eeread(unsigned char address);
void main(void)
{
unsigned char a;
eewrite(4,'1');
a = eeread(4); // data from eeprom
TRISB = 0; // PORTB as output
PORTB = a;
while(1);
}
unsigned char eeread(unsigned char address)
{
EEADR = address;
EEPGD = 0;
RD = 1;
return EEDATA;
}
void eewrite(unsigned char address, unsigned char data)
{
EEADR = address;
EEDATA = data;
EEPGD = 0;
WREN = 1;
GIE = 0;
EECON2 = 0x55;
EECON2 = 0xAA;
WR = 1;
GIE = 1;
while(WR); // wait for the write to finish
WREN = 0;
}
What the code does
- Header and clock: pic.h gives access to the register names. _XTAL_FREQ is set to 16000000 for a 16 MHz crystal. It matters only if you use delay functions, but it should match your hardware.
- Configuration word: __CONFIG(0x3f7a) sets the device configuration bits, including an HS oscillator and the watchdog timer turned off. Check these bits against the datasheet for your own board.
- eewrite(): loads the address and data, then runs the unlock sequence and starts the write.
- eeread(): loads the address, selects data memory, starts the read and returns EEDATA.
The first version of this code declared functions without a return type and used unsigned short int for a single byte. The EEPROM stores 8-bit values, so unsigned char is the better fit. Declaring void for functions with no return value also avoids compiler warnings.
Common mistakes
- Forgetting RD=1: EEDATA keeps its old value and you read stale data.
- Breaking the unlock sequence: if an interrupt fires between the 55h and AAh writes, the write is ignored. Disable interrupts around it.
- Reading too soon after a write: wait for WR to clear before starting another operation.
- Restoring GIE blindly: the example sets GIE=1 afterwards. If your program had interrupts disabled before the call, save the old state and restore that instead.
- Writing in a tight loop: EEPROM cells survive a limited number of write cycles. Write only when a value actually changes, not on every pass of the main loop.
Testing the result
In the example, PORTB shows the byte that was read back. Connect LEDs to PORTB (through resistors) to see the binary value, or check the pins in a simulator. If you are new to port control, see our article on digital output and input first.
For a more useful test, power-cycle the board after the first write and comment out the eewrite() call. If the same value still appears on PORTB, the data really was stored in non-volatile memory. You can also send the value over UART to a serial terminal instead of using LEDs. Our article on UART and EEPROM together shows how to combine the two.
Using the compiler helper functions
The HI-TECH C compiler also ships helper routines for data EEPROM, such as EEPROM_READ and EEPROM_WRITE. Names and behavior can differ between compiler versions, so check your compiler’s manual before using them. Writing the register code yourself, as above, is still worth doing once, because it shows exactly what the hardware requires.
If you are setting up the toolchain, our article on using the PIC16F877A with the HI-TECH C compiler is a good starting point.
FAQ
How much data EEPROM does the PIC16F877A have?
It has 256 bytes of data EEPROM, so valid addresses run from 0 to 255. Each location holds one byte. To store a larger value, such as a 16-bit number, split it across two addresses.
Do I need to set WREN before reading?
No. WREN only enables writes. A read needs the address in EEADR, EEPGD cleared and the RD bit set.
Why is my EEPROM write not working?
The usual causes are a broken 55h/AAh sequence, WREN not being set, EEPGD left at 1, or an interrupt hitting in the middle of the unlock sequence. Also check that the supply voltage is stable during the write.
Does EEPROM data survive a reset or power loss?
Yes. Data EEPROM is non-volatile, so values stay after a reset, power-off or reprogramming of the chip, unless your programmer is set to erase it.