PostHeaderIcon Recovering PIC16F874 Code: The Configuration Word That Decides Whether It Is Possible

The PIC16F874 is the 40-pin member of Microchip’s original PIC16F87X family. It carries 4K words of Flash program memory, 192 bytes of RAM, 128 bytes of data EEPROM and 33 I/O pins, and it is the part that adds a Parallel Slave Port to the smaller 28-pin devices. It has been designed into industrial controllers, parallel-bus peripherals and instrumentation since the late 1990s, which is exactly why code recovery requests for it still arrive today: the boards keep running, and the source code is long gone.

What most write-ups get wrong is the assumption that code protection on this part is a single on/off switch. It is not. The PIC16F87X generation stored its protect bits twice inside the same configuration word, and the exact value in each copy determines not just whether the chip is protected, but which address range is protected. Understanding that layout is the difference between a recovery that is straightforward and one that is impossible.

What the PIC16F874 actually contains

Item PIC16F874
Package 40-pin PDIP / 44-pin PQFP, TQFP
Program memory 4K × 14-bit Flash (0000h–0FFFh)
Data RAM 192 bytes
Data EEPROM 128 bytes
I/O pins 33
A/D converter 8 channels, 10-bit
CCP modules 2
Serial MSSP (SPI / I²C) + USART
Parallel Slave Port Yes — 8-bit, with external RD, WR and CS
Programming interface 2-wire ICSP

The two features that separate it from its siblings matter for recovery work. The 8K parts (876 and 877) have twice the address space, which changes the meaning of some configuration bits. And the Parallel Slave Port exists only on the 40/44-pin devices — the 28-pin 873 and 876 do not implement it, so a design that uses PSP cannot be rebuilt on a smaller package no matter how clean the recovered image is.

The configuration word at 2007h

Everything about protection on this part lives in one 14-bit word at program memory address 2007h. It sits above the user program space and is only accessible during programming, never by running code. The erased value is 3FFFh.

Bits Field Meaning
13:12 and 5:4 CP1:CP0 Flash code protection — appears twice
11 DEBUG In-circuit debugger; 1 = off, RB6/RB7 are general I/O
9 WRT Flash write enable via EECON; 1 = writable
8 CPD Data EEPROM code protection; 1 = off
7 LVP Low-voltage ICSP; 1 = PGM function on RB3
6 BODEN Brown-out reset; 1 = enabled
3 PWRTE Power-up timer; 0 = enabled (inverted)
2 WDTE Watchdog; 1 = enabled
1:0 FOSC1:FOSC0 11 = RC, 10 = HS, 01 = XT, 00 = LP

The unusual part is CP1:CP0 appearing at bits 13:12 and again at bits 5:4. Microchip’s own documentation carries an explicit note that all CP1:CP0 pairs must be programmed to the same value for the stated protection scheme to take effect. Two copies of the same field in one register is redundant by design — it is there so a single corrupted bit cannot silently leave a device unprotected. But it also means that a device read back as “unprotected at 13:12, protected at 5:4” is in a state that is not covered by the datasheet table at all, and that state has to be treated on its own terms rather than assumed to be one of the four documented rows.

Partial protection is real on this part

For the 4K devices (873 and 874) the four documented CP1:CP0 values are:

CP1:CP0 Range protected
11 none
10 0F00h–0FFFh
01 0800h–0FFFh
00 0000h–0FFFh

Compare that with the 8K devices, where the same bit values map to 1F00h–1FFFh, 1000h–1FFFh and 0000h–1FFFh respectively. The bit field is identical; the address ranges it refers to are not. Anyone reading a generic PIC16F87X note and applying the 8K ranges to a 4K part will misjudge which portion of the image is available.

The practical consequence is that “is this chip protected” is the wrong question on a PIC16F874. The right question is which 256-word block is protected. With CP1:CP0 = 10 only the top 256 words are locked, leaving 0F00h of readable program space. That configuration exists legitimately: it is what a design looks like when the developer wanted to protect a calibration block or a fixed lookup table at the top of memory while leaving application code readable for field diagnostics.

The other bits that change the answer

CPD (bit 8). Data EEPROM protection is independent of Flash protection. A program can store its constants in the 128-byte EEPROM, and CPD = 0 blocks external reads of that block while leaving Flash readable. The reverse is also common. A recovery that returns only the Flash image and ignores the EEPROM returns a program without its data.

LVP (bit 7). This bit decides how the device can be entered into programming mode at all. With LVP = 1, the RB3/PGM pin carries the PGM function and the part can be programmed from a single supply rail. With LVP = 0, RB3 becomes ordinary digital I/O and high voltage must be applied to MCLR. These are physically different fixtures. It also means LVP = 1 costs the design one I/O pin, so many production boards set LVP = 0 — which is precisely the case where a naive bench setup will fail to talk to the chip.

DEBUG (bit 11). If a prototype was left with the debugger enabled, RB6 and RB7 are consumed by the debugger rather than available as I/O. A recovered image from such a device behaves differently on a clean replacement part unless DEBUG is set to match.

FOSC (bits 1:0). The oscillator selection is part of the same word. Copying program memory to a new device without carrying FOSC across can produce a part that runs on the wrong clock source — with an RC setting it may appear to boot but time every peripheral incorrectly.

What a recovered image contains

A complete PIC16F874 image is not just the 4K of program words. It has four distinct regions:

  • 0000h–0FFFh — program Flash, 4K × 14-bit
  • 2000h–2003h — four user ID locations, programmable and often used for serial numbers or build codes
  • 2006h — device ID word, read-only, identifying the die
  • 2007h — the configuration word

The device ID at 2006h is worth checking on any secondhand board. The 873, 874, 876 and 877 each carry a distinct pattern, so the ID confirms which die is physically present rather than which marking is printed on the package. Given how many PIC16F87X parts have been through distribution chains over twenty-five years, a part sold as an 874 that reports a different ID changes the entire address-range analysis above.

Why this recovery is requested

The legitimate cases are consistent. A production line controller whose source was never archived fails, and the only route back is the binary on a working board. A legacy design must be maintained for a customer whose contract outlives the toolchain. A company acquires a product line and inherits hardware documentation but not firmware. In each case the recovered image is the maintenance record — it is what lets a board be repaired instead of scrapped.

Related work on the sibling parts follows the same shape: the 28-pin PIC16F873 code recovery shares the 4K address layout but has no PSP, while the 8K PIC16F877A uses a different configuration scheme entirely. The 40-pin revision of this same part is covered under PIC16F874A.

FAQ

Does a protected PIC16F874 always block everything? No. CP1:CP0 = 10 on a 4K device protects only 0F00h–0FFFh. The lower 3840 words remain readable, which is why a partial image is sometimes available even from a “protected” chip.

Can the configuration word be read back? The configuration word is readable during programming regardless of CP state — it is the user program memory that CP gates. That is why the lock state of an unknown device can be determined before any recovery attempt is made.

Is the data EEPROM included automatically? Only if CPD permits it. CPD = 0 blocks external reads of the 128-byte EEPROM independently of Flash protection, and the EEPROM must be handled as a separate question.

Why does the same hex file fail to boot on a replacement part? Usually because FOSC or DEBUG was not carried across, or because the program depends on EEPROM contents that were not copied. The instruction stream is rarely the problem; the configuration state around it is.

Does the device ID matter? Yes on secondhand stock. Reading 2006h confirms the actual die, which determines which address ranges the CP1:CP0 values refer to.

Conclusion

The PIC16F874 is not a chip with one lock. It has a duplicated protect field that addresses different memory ranges depending on whether the part is 4K or 8K, a separate EEPROM lock, a programming-mode bit that changes the hardware fixture, and an oscillator field that travels with the code. Recovery on this part is an exercise in reading the configuration word correctly before touching anything else — the instruction stream is the easy half.

Related reading

Comments are closed.