1
\$\begingroup\$

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(){
    if(PRG){
        for (int i = 0; i < 8; i++){
            SIREN = !SIREN;
            __delay_ms(170);
        }
        __delay_ms(2640);
    }
    if(PRG){
        SIREN = 1;
        EECON1bits.WREN = 1;
        for (unsigned char i = 0; i <= 127; i++){
            EEDATA = 0x00;
            EEADR = i;
            EECON1bits.WR = 1;
            while (EECON1bits.WR);
        }
        __delay_ms(1000);
        SIREN = 0;
        LEDZ1 = LEDZ2 = LEDZ3 = LEDZ4 = LEDAC = 1;
        __delay_ms(5000);
    }
}

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:

:020000040000FA
:10420000FF00FF00FF00FF00FF00FF00FF00FF00B6
:10421000FF00FF00FF00FF00FF00FF00FF00FF00A6
:10422000FF00FF00FF00FF00FF00FF00FF00FF0096
:10423000FF00FF00FF00FF00FF00FF00FF00FF0086
:10424000FF00FF00FF00FF00FF00FF00FF00FF0076
:10425000FF00FF00FF00FF00FF00FF00FF00FF0066
:10426000FF00FF00FF00FF00FF00FF00FF00FF0056
:10427000FF00FF00FF00FF00FF00FF00FF00FF0046
:10428000FF00FF00FF00FF00FF00FF00FF00FF0036
:10429000FF00FF00FF00FF00FF00FF00FF00FF0026
:1042A000FF00FF00FF00FF00FF00FF00FF00FF0016
:1042B000FF00FF00FF00FF00FF00FF00FF00FF0006
:1042C000FF00FF00FF00FF00FF00FF00FF00FF00F6
:1042D000FF00FF00FF00FF00FF00FF00FF00FF00E6
:1042E000FF00FF00FF00FF00FF00FF00FF00FF00D6
:1042F000FF00FF00FF00FF00FF00FF00FF00FF00C6
:00000001FF

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.

\$\endgroup\$
0

1 Answer 1

2
\$\begingroup\$

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.

\$\endgroup\$
2
  • 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.