2
\$\begingroup\$

I am trying to to program a PIC18F4550 with IPE and PICKit 3.

The steps I am following are as follows:

  1. First, write the code and compile it to obtain an .hex file.

    My code is just a simple Hello World that turns on an LED:

    #include <stdio.h>
    #include <stdlib.h>
    #include "blinking_led.h"
    
    void main(void) {
    
        TRISBbits.RB0 = 0; //Setting PORTB.0 as output
    
        while (1)
        {
            LATBbits.LATB0 = 1; //Switching PORTB.0 on    
        }
    
        return;
    
    }
    

    where "blinking_led.h" is the configuration bits defined as:

    // PIC18F4550 Configuration Bit Settings
    
    // 'C' source line config statements
    
    // CONFIG1L
    #pragma config PLLDIV = 1       // PLL Prescaler Selection bits (No prescale (4 MHz oscillator input drives PLL directly))
    #pragma config CPUDIV = OSC4_PLL6// System Clock Postscaler Selection bits ([Primary Oscillator Src: /4][96 MHz PLL Src: /6])
    #pragma config USBDIV = 1       // USB Clock Selection bit (used in Full-Speed USB mode only; UCFG:FSEN = 1) (USB clock source comes directly from the primary oscillator block with no postscale)
    
    // CONFIG1H
    #pragma config FOSC = EC_EC     // Oscillator Selection bits (EC oscillator, CLKO function on RA6 (EC))
    #pragma config FCMEN = OFF      // Fail-Safe Clock Monitor Enable bit (Fail-Safe Clock Monitor disabled)
    #pragma config IESO = OFF       // Internal/External Oscillator Switchover bit (Oscillator Switchover mode disabled)
    
    // CONFIG2L
    #pragma config PWRT = OFF       // Power-up Timer Enable bit (PWRT disabled)
    #pragma config BOR = ON         // Brown-out Reset Enable bits (Brown-out Reset enabled in hardware only (SBOREN is disabled))
    #pragma config BORV = 3         // Brown-out Reset Voltage bits (Minimum setting 2.05V)
    #pragma config VREGEN = OFF     // USB Voltage Regulator Enable bit (USB voltage regulator disabled)
    
    // CONFIG2H
    #pragma config WDT = ON         // Watchdog Timer Enable bit (WDT enabled)
    #pragma config WDTPS = 32768    // Watchdog Timer Postscale Select bits (1:32768)
    
    // CONFIG3H
    #pragma config CCP2MX = OFF      // CCP2 MUX bit (CCP2 input/output is multiplexed with RC1)
    #pragma config PBADEN = ON      // PORTB A/D Enable bit (PORTB<4:0> pins are configured as analog input channels on Reset)
    #pragma config LPT1OSC = OFF    // Low-Power Timer 1 Oscillator Enable bit (Timer1 configured for higher power operation)
    #pragma config MCLRE = OFF       // MCLR Pin Enable bit (MCLR pin enabled; RE3 input pin disabled)
    
    // CONFIG4L
    #pragma config STVREN = ON      // Stack Full/Underflow Reset Enable bit (Stack full/underflow will cause Reset)
    #pragma config LVP = ON         // Single-Supply ICSP Enable bit (Single-Supply ICSP enabled)
    #pragma config ICPRT = OFF      // Dedicated In-Circuit Debug/Programming Port (ICPORT) Enable bit (ICPORT disabled)
    #pragma config XINST = OFF      // Extended Instruction Set Enable bit (Instruction set extension and Indexed Addressing mode disabled (Legacy mode))
    
    // CONFIG5L
    #pragma config CP0 = OFF        // Code Protection bit (Block 0 (000800-001FFFh) is not code-protected)
    #pragma config CP1 = OFF        // Code Protection bit (Block 1 (002000-003FFFh) is not code-protected)
    #pragma config CP2 = OFF        // Code Protection bit (Block 2 (004000-005FFFh) is not code-protected)
    #pragma config CP3 = OFF        // Code Protection bit (Block 3 (006000-007FFFh) is not code-protected)
    
    // CONFIG5H
    #pragma config CPB = OFF        // Boot Block Code Protection bit (Boot block (000000-0007FFh) is not code-protected)
    #pragma config CPD = OFF        // Data EEPROM Code Protection bit (Data EEPROM is not code-protected)
    
    // CONFIG6L
    #pragma config WRT0 = OFF       // Write Protection bit (Block 0 (000800-001FFFh) is not write-protected)
    #pragma config WRT1 = OFF       // Write Protection bit (Block 1 (002000-003FFFh) is not write-protected)
    #pragma config WRT2 = OFF       // Write Protection bit (Block 2 (004000-005FFFh) is not write-protected)
    #pragma config WRT3 = OFF       // Write Protection bit (Block 3 (006000-007FFFh) is not write-protected)
    
    // CONFIG6H
    #pragma config WRTC = OFF       // Configuration Register Write Protection bit (Configuration registers (300000-3000FFh) are not write-protected)
    #pragma config WRTB = OFF       // Boot Block Write Protection bit (Boot block (000000-0007FFh) is not write-protected)
    #pragma config WRTD = OFF       // Data EEPROM Write Protection bit (Data EEPROM is not write-protected)
    
    // CONFIG7L
    #pragma config EBTR0 = OFF      // Table Read Protection bit (Block 0 (000800-001FFFh) is not protected from table reads executed in other blocks)
    #pragma config EBTR1 = OFF      // Table Read Protection bit (Block 1 (002000-003FFFh) is not protected from table reads executed in other blocks)
    #pragma config EBTR2 = OFF      // Table Read Protection bit (Block 2 (004000-005FFFh) is not protected from table reads executed in other blocks)
    #pragma config EBTR3 = OFF      // Table Read Protection bit (Block 3 (006000-007FFFh) is not protected from table reads executed in other blocks)
    
    // CONFIG7H
    #pragma config EBTRB = OFF      // Boot Block Table Read Protection bit (Boot block (000000-0007FFh) is not protected from table reads executed in other blocks)
    
    // #pragma config statements should precede project file includes.
    // Use project enums instead of #define for ON and OFF.
    
    #include <xc.h>
    

    It works perfectly on Proteus

    Proteus

  2. Next open IPE, select the device and connect the PICKit 3 to one PC USB port

    USB port selection

    and enable "Power Target Circuit from Tool"

    Enable "Power Target Circuit from Tool"

  3. Connect the pins of the PICKit 3 according to the device. In my case:

    PICKit 3 pinout

    PIC18F4550 pinout

    so, I connect:

    • 1 to 1;
    • 2 to 11 and 32;
    • 3 to 12 and 31;
    • 4 to 40;
    • 5 to 39, and;
    • 6 to 38.
  4. Finally, just select the hex file and press "Program"

    Select file and press "Program"

Everything seems OK, but after the message of "Programming Complete", the LED of my project doesn't turn on. Am I missing something important?

Photo of PIC circuit on breadboard

\$\endgroup\$
  • \$\begingroup\$ Test your LED by moving the resistor lead from the PIC to the Vdd rail. Modify your question with the results of that test. \$\endgroup\$ – Dwayne Reid Feb 17 '17 at 2:27
  • \$\begingroup\$ The LED turns on \$\endgroup\$ – Delfin Feb 17 '17 at 2:34
  • \$\begingroup\$ Try to put a pull-down resistor on the PGM pin and see what happens then.. \$\endgroup\$ – m.Alin Feb 17 '17 at 9:43
  • \$\begingroup\$ Were you successful? I would like to try the same or similar hardware setup, and was curious if you were able to successfully program your chip. \$\endgroup\$ – Eraticus Feb 20 '17 at 21:51
  • \$\begingroup\$ Yes. apparently the problem is that I was not supplying enough current. I added a 5 voltage converter, and now it works great. Also, I think the programming code is incorrect. I added an external oscillator instead of the internal one and add: #define _XTAL_FREQ 5000000 to my code. \$\endgroup\$ – Delfin Feb 22 '17 at 5:16
2
\$\begingroup\$

Correct the code on configuring the internal oscillator as I can see you didn't use any external oscillator (crystal) in the photo of your board.

Read here: Internal oscillator for PIC18F4550

The link shows that the internal oscillator of 18F4550 can be accessed through the CONFIG1H register and the OSSCON register. These settings is crucial when you want to use the PIC Micro without using any external oscillators. The link above is from the official Microchip forum, maker of the PIC18F4550 chips.

\$\endgroup\$
1
\$\begingroup\$

Am I missing something important?

A few simple steps:

  1. Read the datasheet;

  2. Turn all fuse bits to OFF, unless / until you know what you are doing;

  3. Use the internal oscillator;

  4. Turn all non-gpio functions off on the pin;

  5. Write the code to switch the pin SLOWLY.

  6. Done.

If none of that works, ask someone to write you the right code and flash it to your chip.

\$\endgroup\$
0
\$\begingroup\$

Apparently1 LVP = OFF could fix it.

Test code:

// test 

#include <p18f4550.h> 

//#pragma config FOSC = HSPLL_HS  //10MHz x 4 gives 40MHz clock 
#pragma config FOSC = INTOSC_EC    //internal oscillator 
#pragma config PLLDIV = 4   //divide by 5 to give 4MHz to PLL 
#pragma config FCMEN = OFF   //no fail safe clock monitor 
#pragma config IESO = OFF   //oscillator switchover disabled 
#pragma config PWRT = ON           //oscillator power up timer enabled (release version only) 
#pragma config BOR = OFF      //hardware brown out reset 
#pragma config WDT = OFF   //watchdog timer disabled 
#pragma config MCLRE = ON    //MCLR pin enabled 
#pragma config LPT1OSC = ON  //timer1 low power operation 
#pragma config PBADEN = OFF   //portB 0to 4 digital - not analogue 
#pragma config LVP = OFF     //low voltage programming disabled 
#pragma config CCP2MX = OFF  //portc1 = CCP2 
#pragma config XINST = OFF         //do not allow PIC18 extended instructions 
#pragma config  STVREN = ON         //stack overflow will cause reset 
#pragma config CP0 = OFF      //code protection block 0 
#pragma config CP1 = OFF 
#pragma config CP2 = OFF 
#pragma config CP3 = OFF 
#pragma config CPB = OFF      //code protection boot block 
#pragma config CPD = OFF      //code protection data EEPROM 
#pragma config WRT0 = OFF      //write protection block 0 
#pragma config WRT1 = OFF 
#pragma config WRT2 = OFF 
#pragma config WRT3 = OFF 
#pragma config WRTB = OFF      //write protection boot block 
#pragma config WRTC = OFF      //write protection configuration register 
#pragma config WRTD = OFF      //write protection data EEPROM 
#pragma config EBTR0 = OFF      //table read protection block 0 
#pragma config EBTR1 = OFF 
#pragma config EBTR2 = OFF 
#pragma config EBTR3 = OFF 
#pragma config EBTRB = OFF      //table read protection boot block 

void delay1s(void);  //prototype 

void main(void) { 
    TRISA = 0;      // all output (except RA7 not implemented) 
    TRISB = 0;      // all output 
    TRISC = 0;      // all output (except RC3 not implmented, RC4,5 only input or USB 
    TRISD = 0;      // all output 
    TRISE = 0;      // all output (only RE0, RE1, RE2 exist) 
    OSCCON = 0xF2;  // internal oscillator, 8MHz 

    while (1) 
    { 
        delay1s(); 
        LATA = 0x55;    // test pattern 
        LATB = 0x55;    // test pattern 
        LATC = 0x55;    // test pattern 
        LATD = 0x55;    // test pattern 
        LATE = 0x55;    // test pattern 
        delay1s(); 
        LATA = 0xaa;    // test pattern 
        LATB = 0xaa;    // test pattern 
        LATC = 0xaa;    // test pattern 
        LATD = 0xaa;    // test pattern 
        LATE = 0xaa;    // test pattern 
    } 
} 

// adjust numbers to give about 1 second delay 
void delay1s(void) 
{ 
int m,n; 

    for (m=0;m<200;m++) 
    { 
        for (n=0;n<200;n++); 
    } 
} 

1 From the link in JRPL's answer.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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