Copy Microcontroller PIC16F627A Binary: The Word at the End of the Array
Most copies fail for interesting reasons. This one fails for a boring mechanical reason that is easy to fix once it is named: the information needed for the copy to work is not all inside the region the word copy suggests.
The device has a small program array, a data EEPROM, a configuration block, and an internal oscillator. The internal oscillator was trimmed at the factory, and the trim value was written into the device. It sits in the last program word, in the upper byte, sharing that word with configuration bits in the lower byte. Any procedure that reads the code and stops at the code loses it, and any procedure that writes configuration without preserving it overwrites it.
Why one word holds two things
The economy is obvious in hindsight. A fourteen-bit instruction word has room, the configuration bits do not occupy all of it, and the trim value needed eight bits. Putting the trim in the unused high byte of the configuration word cost nothing and required no additional memory.
The consequence is that the last word of the array is not code and not configuration but both, at different bit positions, with different lifecycles. Code is written once by the engineer. Configuration is written once by the engineer. The trim value is written once by the test fixture, after the engineer, and it belongs to that specific piece of silicon.
| Part of the word | Written by | Belongs to | Survives a plain config write |
|---|---|---|---|
| lower bits, configuration | firmware engineer | the design | replaced |
| upper byte, oscillator trim | factory test | this individual die | lost unless preserved |
That last column is where copies break. Writing a configuration block from a donor device into a fresh device is the natural thing to do when reproducing a product, and it carries the donor’s trim value with it. The fresh device then runs its internal oscillator trimmed for a different die, which on this family means a frequency error that is small enough to keep the board working and large enough to break every timing-dependent function on it.
What the error looks like on the bench
The internal oscillator on this part is the clock source for the core, and in many designs also for the timers, the serial port and any software delay loops. An error in its frequency is therefore not confined to one function.
Three symptoms show up in combination, and together they identify the cause.
- A serial link that works at one baud rate on the original and produces framing errors on the copy, because the divisor was computed for a frequency the copy does not produce.
- Timekeeping that drifts by a few percent, which is easy to dismiss as normal crystal tolerance until the original is compared side by side.
- A watchdog or brown-out interaction that behaves differently, because the watchdog period is derived from the same clock.
The tell is that the copy works. Nothing is dead, nothing resets, and every measurement is slightly off. That is the signature of a trim value from the wrong die, and it is the reason a copy job on this family should always report the trim byte separately.
Reading order that preserves the byte
The sequence matters because a read is not always the last operation performed on the device.
- Read the last program word on its own, before anything else, and record both bytes separately. This preserves the trim even if a later read of the array is truncated or a subsequent operation alters the device.
- Read the full program array. Note whether the last word is included by the tool’s default length; many programmers stop at the last implemented code address and skip it.
- Read the data EEPROM. On this family it is a couple of hundred bytes and holds the per-unit parameters.
- Decode the configuration byte from the last word, and keep the trim byte alongside it as a separate field rather than as part of the configuration.
Step one has an additional benefit: on a device whose array is protected, the last word is sometimes readable when the rest is not, because the protection scheme acts on the code region. That makes it a cheap early diagnostic.
Writing it back correctly
On the programming side there are two distinct operations and they must not be merged.
The first writes the program array. It includes the last word only if the tool is told to include it, and on a reproduction job the last word of the program array should carry the donor’s code but not the donor’s trim.
The second writes the configuration. Here the trim byte comes from the target device, not the donor. The correct value is whatever the target’s own last word held before the write — which is why reading it first, and keeping it outside the file being programmed, is the whole discipline.
Where a device has been fully erased, its trim value is gone, and there is no way to recover the value that belonged to that specific die. That is a genuine loss and it should be reported. On this family the practical answer is a calibration pass at the intended supply voltage and temperature, writing a measured value rather than an invented one. A value copied from another device is the one option that should never be chosen, because it is wrong in a way that is hard to detect later.
Trim is a curve, not a number
Worth knowing when the application cares: the oscillator’s frequency depends on supply voltage and temperature, and the single trim byte sets the operating point at the factory’s reference conditions. A design that runs at a different voltage, or over a wide temperature range, will see drift around that point regardless of how the trim was set.
A copy that reproduces the trim byte exactly therefore reproduces the original’s behaviour, including its drift. That is the right outcome for a repair, and it is worth saying so in the delivery, because a customer who replaces a unit and finds the same temperature-dependent timing error concludes that the copy was botched, when in fact it was faithful.
When the product does not use the internal oscillator
Not every design on this family runs from the internal clock. Where the board carries a crystal or a resonator, the calibration byte is irrelevant to operation and the whole question above collapses.
It still belongs in the delivery, for two reasons. The first is that it identifies the part’s provenance: a byte in a plausible range says the device was programmed by a fixture that wrote it, while an erased byte says the device has been through an erase that the factory never intended. The second is that a copy is not always made for the same board. A customer who later moves the design to a smaller board without the crystal will need the trim value, and by then the donor device may be gone.
So the rule is: preserve it whether or not it is used, and say plainly in the delivery which case applies. The cost is one line; the alternative is a conversation two years later about why a re-designed board runs slow.
The delivery
For this device the file set is small and the notes are what carry the value.
- the program image, with the last word’s two bytes reported separately
- the data EEPROM image
- the configuration byte decoded into named fields
- the trim byte as a standalone value, labelled as belonging to the donor die
- a statement of whether the target device’s own trim was preserved or lost
- the identifier and revision words
That fifth line is the one that prevents the most expensive mistake in this family. It tells the person programming the replacement whether they need a calibration step or can proceed directly.
The same principle — one location carrying two different kinds of information — appears on other parts in this era, and where the collision involves the protection bits themselves the consequences are covered in PIC16C73A program recovery. For the device where the internal oscillator is absent and the clock question disappears entirely, PIC16F87 code copying is the comparison. And for the eight-pin members of the same generation, where the trim byte occupies the same relative position with far less room around it, PIC12F615 software recovery follows the trail to the smallest case.