HELP, some hex files don't work!


robot imageProblem description

For several times I received emails complaining that one or several hex files from this website won't work. 

On this page I'm publishing the most common issues you may run into when programming your microcontrollers, so if you are having problems continue reading below.

There are some situations where the hex files may fail to work properly:


Proposed solutions

Missing RETLW/OSCCAL value (the most common problem)

All hex files I publish are compiled with the option to load the value at the end of the flash memory into the OSCCAL (internal oscillator calibration) register. The calibration word is written at the last position of the flash memory in the form of a "RETLW 0x2C" and the compiler generates a call to the last address of the flash to get the value and load it to the OSCCAL register.

When using other tools to flash the microcontroller such as ic-prog, it is necessary to take care not to do a full erase of the PIC, otherwise the instruction at the end of the flash may be lost. When this happens, the last position is replaced with a ADDLW 0xFF and the PIC never returns from the call instruction. It will loop to the beginning of the flash, starting the code again... and again... and again... after several times it will fill the PIC hardware stack and stall.


Missing pull-up resistor

Sometimes when testing the microcontroller on a white board one may forget to place a pull-up resistor connected between the MCLR pin and +V. The microcontroller may work fine while connected to the programmer but it will fail after you disconnect it. The most common sympton for this problem is the PIC running fine when you place your hand near or on top of it but stopping when you remove your hand.


Missing 100nF decoupling capacitor

In circuits with Integrated Circuits running at frequencies up to 100MHz there should be a decoupling capacitor per IC, placed as near at possible to its power pins. This capacitor bypasses any high frequency noise produced by the IC to the ground, leaving the power signals clean. Maintaining the power supply signal clean will avoid misbehavior of other ICs in the circuit due to the noise. It is very often to forget about the decoupling capacitor and then the microcontroller fails to run the code, becomes unstable or in extreme situations, as it once happened to me, will corrupt the flash memory.


Interrupt-on-Change glitches

I also discovered a problem with PICs 12F629 and 12F675 causing false triggers when using the Interrupt-on-Change feature. Those can be corrected with a test read of the pin to confirm that it as really changed but will require more memory to remember its last state.


Published on Tuesday 2012/04/17, last modified on Wednesday 2015/05/27