I'm currently writing code for an alarm system, and I'm having some issues with the EEPROM at the moment of writing/reading (with MPLAB) its contents. I'm not trying to write any variables or configurations yet, but instead I'm trying to wipe it and I constantly fail to write/verify that everything has worked. This is the piece of code that I use to try to wipe the contents of the EEPROM:

void default_check(){
        for (int i = 0; i < 8; i++){
            SIREN = !SIREN;
        SIREN = 1;
        EECON1bits.WREN = 1;
        for (unsigned char i = 0; i <= 127; i++){
            EEDATA = 0x00;
            EEADR = i;
            EECON1bits.WR = 1;
            while (EECON1bits.WR);
        SIREN = 0;
        LEDZ1 = LEDZ2 = LEDZ3 = LEDZ4 = LEDAC = 1;

SIREN is just a macro for a siren, and PRG is the reset button. This function is executed after the basic port init but before the peripherals init.

I've extensively read the datasheet of the PIC16F628A and also took a look at the DS33023 Reference Manual looking for any restriction when trying to write zeros to the EEPROM, but found nothing. I was able to write the whole EEPROM with 0xFF only once, with the same code that now doesn't make any changes.

The way I'm reading the contents of the EEPROM is by using the built-in "EE Data Memory" under "Window>Target Memory Views" and also using the "Read EE/flash data memory to file". Right now under "EE Data Memory" I can see all FFs from 00 to 7F, and with the "Read to file" option I get an HEX file that (when opened with notepad) makes no sense at all:


I have already tried to "blank" the chip with the MPLAB IPE, but the contents of the EEPROM remain untouched (or maybe rewritten to FF). I've also tried to change the 0x00 to a variety of combinations, and nothing got reflected when I tried to read the EEPROM with the IDE.


1 Answer 1


You need to follow the EEPROM unlock sequence with EECON2. Write 0x55, write 0xAA, then set WR bit. Disable interrupts (if used) too.

0xFF is the 'blank' condition of the EEPROM.

  • 1
    \$\begingroup\$ 10.3 in datasheet. \$\endgroup\$
    – user263983
    Commented Jul 13, 2023 at 16:19
  • \$\begingroup\$ Thanks, this solved my issue. I thought that the procediure was the same as for reading \$\endgroup\$
    – fpp
    Commented Jul 13, 2023 at 17:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.