2
\$\begingroup\$

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.

\$\endgroup\$
15
  • \$\begingroup\$ You could try comparing the two generated images \$\endgroup\$
    – PlasmaHH
    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\$ May 27, 2016 at 9:55
  • \$\begingroup\$ I know the specifics of your platform, but virtually everyone has a useful disassembler \$\endgroup\$
    – PlasmaHH
    May 27, 2016 at 9:58
  • 2
    \$\begingroup\$ This kind of complete failure to start is often a config bits problem... \$\endgroup\$
    – brhans
    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\$ May 30, 2016 at 22:20

2 Answers 2

5
\$\begingroup\$

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

\$\endgroup\$
2
  • \$\begingroup\$ good for you , finding the problem , since without a schemtic we would never have found the solution for you. \$\endgroup\$
    – ElectronS
    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\$ May 30, 2016 at 15:45
3
+25
\$\begingroup\$

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.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.