Electronic – PIC18F1220 production build of C code not working

mplabxpicprogrammer

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.

// CONFIG1H
#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)

// CONFIG2L
#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

// CONFIG2H
#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)

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

// CONFIG4L
#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)

// CONFIG5L
#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)

// CONFIG5H
#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)

// CONFIG6L
#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)

// CONFIG6H
#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)

// CONFIG7L
#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)

// CONFIG7H
#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++){
        __delay_ms(1);
    }
}
void main(void) {
    OSCCONbits.IRCF = 0b111;//8MHz drives directly

    ADCON1bits.PCFG4 = 1;//Digital rb0 = an4
    TRISBbits.RB0 = 0; //Output for led
    PORTBbits.RB0 = 1;
    while(true){
        delayMs(250);
        delayMs(250);
        delayMs(250);
        delayMs(250);
        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.

Best Answer

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).