I have this simple code which I wrote to test that my programming circuitry works properly and compiles fine on xc8 pic c compiler:

#include <xc.h>

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.

#pragma config OSC = INTIO1     // Oscillator Selection bits (Internal RC oscillator, CLKO function on RA6 and port function on RA7)
#pragma config FSCM = ON        // Fail-Safe Clock Monitor Enable bit (Fail-Safe Clock Monitor enabled)
#pragma config IESO = ON        // Internal External Switchover bit (Internal External Switchover mode enabled)

#pragma config PWRT = OFF       // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOR = ON         // Brown-out Reset Enable bit (Brown-out Reset   enabled)
// BORV = No Setting

#pragma config WDT = OFF        // Watchdog Timer Enable bit (WDT disabled (control is placed on the SWDTEN bit))
#pragma config WDTPS = 32768    // Watchdog Timer Postscale Select bits (1:32768)

#pragma config MCLRE = ON       // MCLR Pin Enable bit (MCLR pin enabled, RA5 input pin disabled)

#pragma config STVR = ON        // Stack Full/Underflow Reset Enable bit (Stack full/underflow will cause Reset)
#pragma config LVP = OFF        // Low-Voltage ICSP Enable bit (Low-Voltage ICSP disabled)

#pragma config CP0 = OFF        // Code Protection bit (Block 0 (00200-0007FFh) not code-protected)
#pragma config CP1 = OFF        // Code Protection bit (Block 1 (000800-000FFFh) not code-protected)

#pragma config CPB = OFF        // Boot Block Code Protection bit (Boot Block (000000-0001FFh) not code-protected)
#pragma config CPD = OFF        // Data EEPROM Code Protection bit (Data EEPROM not code-protected)

#pragma config WRT0 = OFF       // Write Protection bit (Block 0 (00200-0007FFh) not write-protected)
#pragma config WRT1 = OFF       // Write Protection bit (Block 1 (000800-000FFFh) not write-protected)

#pragma config WRTC = OFF       // Configuration Register Write Protection bit (Configuration registers (300000-3000FFh) not write-protected)
#pragma config WRTB = OFF       // Boot Block Write Protection bit (Boot Block (000000-0001FFh) not write-protected)
#pragma config WRTD = OFF       // Data EEPROM Write Protection bit (Data EEPROM not write-protected)

#pragma config EBTR0 = OFF      // Table Read Protection bit (Block 0 (00200-0007FFh) not protected from table reads executed in other blocks)
#pragma config EBTR1 = OFF      // Table Read Protection bit (Block 1 (000800-000FFFh) not protected from table reads executed in other blocks)

#pragma config EBTRB = OFF      // Boot Block Table Read Protection bit (Boot     Block (000000-0001FFh) not protected from table reads executed in other blocks)

#include <stdbool.h>
#include <stdint.h>
#define _XTAL_FREQ 8000000
inline void delayMs(uint8_t x){
    for(uint8_t i = 0; i<x; i++){
void main(void) {
    OSCCONbits.IRCF = 0b111;//8MHz drives directly

    ADCON1bits.PCFG4 = 1;//Digital rb0 = an4
    TRISBbits.RB0 = 0; //Output for led
    PORTBbits.RB0 = 1;
        PORTBbits.RB0 = !PORTBbits.RB0;


Controller is confiqured to use its internal 8MHz clock source with IO on pin RA7 and clock output on RA6. RB0 is connected to led and series resistor and PIC's Vdd is connected to +5 volts and Vss to ground. This code runs and programs fine when using ICD3 and debug build(led starts blinking every second), but when I click the "RUN" button in the MPLAB X ide the ICD3 tells that programming/verify succeeded, but the led stays off. I have disabled the watchdog timer in configuration bits along with the IESO and FSCM. LVP is also disabled. I also tried to program the chip through MPLAB IPE by taking the production build HEX file and programming it using the IPE with no luck. I'm using MPLAB X IDE v3.30 and ICD3 as the programming device/debugger.

Basically I don't understand why the debug build works and why the production build doesn't and I have not found any reason to the difference after reading the datasheet for the PIC18F1220. I have also checked that the config bits are the same in both debug and production builds. So the question is: Why does the debug build work but the production build doesn't?

EDIT: I have tried to program the chip using PicKit3 and that did not help at all, so it's not ICD3 issue. Also the ICD3 works fine with other pic controllers I have tried it on except the 18F1220 chips. Also it seems like I cannot get anything out of the clkout pin (RA6) when the chip is programmed with the production build, but there is clock signal present on the pin when using the debug build.

EDIT: Also the chips we are using are all of revision 7 or 0b111 silicon according to ICD3. I have checked the errata, but there does not seem to be any major problems (at least that would affect this simple blink code). I have tried to disable the compiler errata workarounds also, but it does not seem to make any difference, as the debug version still works and the production version still fails to work.

  • \$\begingroup\$ You could try comparing the two generated images \$\endgroup\$
    – PlasmaHH
    Commented May 27, 2016 at 9:52
  • \$\begingroup\$ Can you provide some information about how to do that? I haven't done hex comparison before. Anyways the two images are likely to be pretty much different as the debug image will have debug control specific stuff in there. \$\endgroup\$ Commented May 27, 2016 at 9:55
  • \$\begingroup\$ I know the specifics of your platform, but virtually everyone has a useful disassembler \$\endgroup\$
    – PlasmaHH
    Commented May 27, 2016 at 9:58
  • 2
    \$\begingroup\$ This kind of complete failure to start is often a config bits problem... \$\endgroup\$
    – brhans
    Commented May 27, 2016 at 13:50
  • 1
    \$\begingroup\$ Do you have a pull-up on MCLR? I'm sure you do, but I have done this to myself before. Oops didn't notice you already gotr it below. \$\endgroup\$ Commented May 30, 2016 at 22:20

2 Answers 2


Problem here is that the PIC18F1220 and 18F1320 require that the MCLR pin to be connected at least to Vdd through 1-10kOhm resistor. This enables the POR as the datasheet states. If the MCLR pin is left unconnected the POR will not be generated and the pic will stay in reset state. This is why there won't be any clock output (and no program executed). Debug builds work as the programmer takes care of the MCLR pin when connected to the programmer during debugging and chip programming.

The simplest working connection to make the chip work is to connect the MCLR pin to Vdd using resistor (assuming Vdd rise time is fast). Capacitor is required when there is slow rise on the Vdd (see PIC18F1X20 datasheet page 32).

  • \$\begingroup\$ good for you , finding the problem , since without a schemtic we would never have found the solution for you. \$\endgroup\$
    – ElectronS
    Commented May 30, 2016 at 15:39
  • \$\begingroup\$ @ElectronS Yes I agree, but I have previously only worked with pic microcontrollers which do not require the MCLR pin to be connected on production platforms, so I just assumed too much. \$\endgroup\$ Commented May 30, 2016 at 15:45

When programming a device with release code using MPLABX don't use the "Run" button. "Make and Program Device" button is used to program a device with release code.

enter image description here

From what I was told by a Microchip Field Application Engineer the "Run" button is a remnant of NetBeans (The IDE that MPLABX is based on). The "Run" button works sometimes and not other.

  • \$\begingroup\$ Very interesting fact to know. Although in this case the results were identical using the "make and program device" button. \$\endgroup\$ Commented May 30, 2016 at 11:08

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.