Home > pic18f bootloader, Uncategorized > Set up required to program Pic18f4620 via Bootloader

Set up required to program Pic18f4620 via Bootloader

STEP 1

Your PIC must first be programmed with this bootloader. If you don’t have a preprogrammed PIC you  can download the hex file from here and program it using a pickit2 programmer by doing the following:

  • Connect the pickit2 to the pic18f4620 and the pickit2 to your computer as shown here.
  • Download, install and open the pickit2 software application
  • Select File->Import Hex. Browse to the location of the hex file you downloaded and select the file.
  • Click on the ‘Write’ button (left middle of the application)
  • Your PIC is now programmed with the bootloader

STEP 2

Build this circuit using the pic with bootloader programmed on to it and connect the usb-to-serial converter to your computers usb socket. (Make sure that the drivers for the usb-to-serial converter are installed on your computer – see prolific’s website for instructions on installing drivers for the PL2303HX)

circuit

STEP 3
Enter bootloader mode by holding down button 2 while clicking button 1 (The reset button). The LED will light up when the pic is in bootloader mode and your pic is ready to be programmed via the bootloader.

STEP 4

Download serialpicprog.exe (here is sourceode) and build.bat and save them in the same location as the code you wish to program on to the pic. Note: in order to use these files the xc8 compiler must be installed on your computer. Here’s some example code from Ted Burke that you can use for testing:

//
// PIC18F4620 example program
// Written by Ted Burke (ted.burke@dit.ie)
// Last update 19-9-2013 -by david.dorran@dit.ie
//          MCLRE set ON
//          RB6 used as output rather than RD0

#include <xc.h>

#pragma config OSC=INTIO67,MCLRE=ON,WDT=OFF,LVP=OFF,BOREN=OFF

int main(void)
{
    // Make RB6 a digital output
    TRISB = 0b10111111;

    while(1)
    {
        LATBbits.LATB6 = 1; // Set pin RB6 high
        _delay(125000);     // 0.5 second delay
        LATBbits.LATB6 = 0; // Set pin RB6 low
        _delay(125000);     // 0.5 second delay
    }
}

STEP 5

Using the DOS prompt, change into the directory where you saved the files in STEP 4 and run the following command “build mycode.c” (replace ‘mycode.c’ with the name of the file you wish to program on to the pic using the bootloader).

If you used the example given above the LED in the circuit will flash at 1Hz.

  1. Tushar Semwal
    February 12, 2014 at 6:10 pm

    Awesome work David..
    Hey david can u tell me if this code will work on pic18f4550..
    Means if by tweaking it ..or i have to completely make a new code..
    Please Help
    Thanx..

    Tushar Semwal
    Computer Science & Engineering
    IIT Guwahati

    • February 12, 2014 at 7:55 pm

      Hi Tushar,

      I’d imagine that there would very few changes required. Let me know how you get on.

      Best of luck,
      David.

  2. March 17, 2014 at 7:22 pm

    Hey David,
    Just started work on boot loader for pic18f4550.So let me first tell you the steps I followed :
    1)I copied your code and compiled using xc8.first on target as 4620 and then 4550.
    2)with pic18f4620 it build successfully as it should but the last line was something like this–? The program file could not be loaded: null(after build successful).
    don’t know why it came or it poses any problem or not..??
    3)then with pic4550 the “first” error was for OCCtunebits.PLLEN line. I think this register bit is not there in 4550.If you are using it for just enabling PLL then i think i should just change to the corresponding register required.
    4the “second” error was for INTIO67 pragma declaration.

    • March 18, 2014 at 8:53 pm

      Yes – I’m just enabling PLL.

      INTIO67 configures internal oscillator and allows RA6 and RA7 be used as inputs. Not sure of settings for 4550.

      Havn’t come across the “The program file could not be loaded:” error before. Is this during the process of programming the pic?

  3. March 18, 2014 at 6:40 am

    Hope my explanation was up to the point.
    Thanx David.
    Your help will be really appreciated.

  4. March 20, 2014 at 1:55 pm

    Thanks For the reply sir.
    ya that “loaded error” is not appearing anymore.
    And I have done few changes for 4550. I will work on them today.
    Will report you for my updated status.

  5. March 20, 2014 at 3:41 pm

    Boot Loader update:
    1)actually 4550 does not support PLL in “Internal OSC” mode.Only with HS,Ec and XT mode for USb applications.
    So I want to ask if you enabling PLL to get 16 MHZ clock?..Means is some specific clock speed required for your boot loader?
    2)and PIC 4550 has option of Internal osc with port as RA6.there is no RA7 option. rest is similar.So will this work:
    Here are some pragma config.:
    INTOSCIO_EC = Internal oscillator, port function on RA6, EC used by USB (INTIO).
    INTOSC_EC = Internal oscillator, CLKO function on RA6, EC used by USB (INTCKO).

    and I think start prog adress woulb be same as ) 0x08(code offset)…?

    • March 20, 2014 at 3:55 pm

      I used 32MHz clock just so I could send and download data quickly. If you have adjust the clock you’ll have to make sure that the UART module is set up to work at the desired baud rate.

  6. March 20, 2014 at 3:46 pm

    Actually Sir, the main thing I am worried about is that PIC 4550 also has a USB host feature.So I am not sure if boot loader would be posing any problem with that.
    Please clear my doubt on this.
    Thank You.

    • March 20, 2014 at 3:57 pm

      I wouldn’t expect this to be a problem if you’re using the UART. It sounds like you’d be able to get away without using a serial-to-USB converter though if there is a USB module on the 4550. If you get this working via the UART why not try to use the USB module to communicate with your PC.

  7. March 20, 2014 at 6:36 pm

    Sir in the comments you have written that you used 115200 as baud rate.
    So in the function openusart the secong arguments is 16i.e openUSART(—, 16)..
    but using the formula given in datasheet the argument is coming out to be 3.34 = 3.
    So did you actually used some other rate or I am miscalculating X.

    • March 20, 2014 at 7:31 pm

      I used a baud rate of 115200. Can you show your workings to come up with 3.34?

  8. March 21, 2014 at 1:45 pm

    The formula given in datasheet is:
    X=((Fosc/desired baud rate)/64)-1
    for you fosc=32000000 baud=115200
    so X=3.34 where X=SPBRGH:SPBRG
    and in C file of openusart function the second argument is passed to SPBRG..SPBRGH is not required as X <8 bit..

    • March 22, 2014 at 12:54 pm

      I think if BRGH=1 (High speed enabled) then the formula is ((Fosc/desired baud rate)/16)-1. Table 8-1 pg205.

  9. March 22, 2014 at 2:38 pm

    Oh.Thanks a lot sir.
    Finally I will try the modifications on pic 4550.I will let you know the results.
    Sorry Sir for troubling you.
    🙂

  10. March 22, 2014 at 4:49 pm

    Since 4550 doesnt have a PLL enable option with internal clock so i am using internal 8MHZ clock.But with baud 115200 it gives me error of 8.5% which is way too much.
    So i decided to lower down my baudrate to 38400.
    Now this requires changes to be made to your serial prog file for macros you have defined.
    So i changed the baud_rate_value to CBR_38400.
    and Baud to 38400.It is compiling well but not sure if it will work according to my changes.
    Please comment.

  11. April 3, 2014 at 1:00 pm

    I’m trying to write a bootloader for a pic18f14k50 and I’m referring to your code as reference. I’m having some problems with it so maybe if you could verify that I am at least doing the right steps it would be much appreciated. I have a python script that parses the .hex file, it gets the number of bytes on a line and then it gets the data from that same line. These are then sent over serial to the pic. I don’t ever send the first line of a hex file because its address is 0000 so I presumed its a jump instruction telling the pic to jump to where the program is in flash memory, however since I want the bootloader to run first I omitted this line. Was that the right thing to do? here is an example of the first line of a hex file:
    :04000000BCEF05F05C

    On the pic side, the program is fairly simple. Firstly the number of bytes in received over serial. The pic then waits for that number of bytes to arrive ( its normally 16 bytes apart from the last line of the hex file). This data is then written in a 16 byte block ( as outlined in the datasheet). This process repeats for the entire hex file. After all the data has been received and written to flash memory there is a goto statement that jumps to the start address of the program. From looking at your bootloader and your serialprog.c file I think that is the basic steps of a bootloader . At this point I’m not sure if my code is wrong or if something has been left out entirely. I verified that the data was written to flash correctly and it appeared to be fine. So my question is, have I left something out or is it more likely a code error?

    • April 3, 2014 at 7:50 pm

      It sounds like you haven’t compiled the code properly since the application code shouldn’t occupy address 0x0000 – this location will house the bootloader code.

      Can you check how you compiled the application code? With my bootloader the application code is housed after address 0xC00 (the bootloader occupies from 0x000 to 0xbff). To compile I used “xc8 –chip=18F4620 –CODEOFFSET=c00h application_code.c” – this forces the application code to occupy the desired memory space.

  12. April 4, 2014 at 5:24 am

    I used “xc8 –chip=18F14K50 –ROM=0-7D0 main.c” to compile the bootloader code and “xc8 –chip=18F14K50 –ROM=7D1-BB8 main.c ” to compile a test program (just blinks an led) . I changed the test code to compile CODEOFFSET and the 0000 address is gone, the first line now has the address of the offset. This explains a lot! Thanks for your help, hopefully I will be able to get this working now.

  13. Vladimir Arias
    April 27, 2020 at 7:04 am

    Hello, good job!!, Could you send a link of serialpicprog.exe, the link is down in 2020. Best regards.

    • April 27, 2020 at 12:32 pm

      I have updated the link in the post Vladimir. Thanks for letting me know.
      David

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: