P.O. Box 156
Terryville, CT 06786-0156

PIC18F14K50 USB HID Bootloader compatible sample – “LEDFlash”

I’ve received a few requests for a simple ‘flashing led’ example for my PIC18F14K50 USB Breakout Board, specifically one compatible with the bootloader. This code here aims to meet that request. Additionally, it serves as a template of sorts for the beginnings of your own HID USB bootloader compatible code.

To keep things simple, all we’re doing here is alternately flashing the two user LEDs on one of my PIC18F14K50 v3 USB Breakout Boards. Aside from this code, you will also need to add a linker script (.lkr file, available in the project download at the end of this article) to your project in order to get it to run on the bootloader. That file is available as part of the project download towards the bottom of this post.

The code requirements to make this compatible with the USB HID Bootloader is actually pretty simple. All that is necessary for this basic example is the code between the “bootloader support code” comments, and the linker file attached to the project as mentioned above.  If we were using interrupts, the code would require us to remap those as well, but that’s just a few more lines of code. We’ll save that for a future example. you can also check out Microchip’s USB HID – Mouse demo code for an example which uses interrupts (Available in the Microchip Application Libraries download, http://www.microchip.com/mal)

And now, to the code!:

#include <p18f14k50.h>

//Bootloader support code start
extern void _startup (void); // See c018i.c in your C18 compiler dir
#pragma code REMAPPED_RESET_VECTOR = 0×1000
void _reset (void)
_asm goto _startup _endasm

#pragma code
//Bootloader support code end
void main (void)
TRISC = 0;
LATC = 0;
LATCbits.LATC4 = 1;
LATCbits.LATC5 = 0;
while (1) {
Delay10KTCYx(125); // Delay 1,250,000 cycles
LATCbits.LATC4 = !LATCbits.LATC4;
LATCbits.LATC5 = !LATCbits.LATC5;

The code for this example is available via git here: LEDFlash

View all posts by Jayson
Jayson's website
Post Tagged with ,
  • Nax

    Thank you very much, works perfectly in MPLABx with C18 compiler.
    An excellent starting point.

    • Jayson

      Excellent, glad to hear it works in MPLABx too!

  • AL


    New to PIC18F14K50 and looking to invest in board like yours. In fact, I have been working with Atmel MCUs for a long time and love it. :-) Time to branch out a bit.


    1. You use Microchip’s usb bootloader. Your board has two buttons. One is connected to MCLR pin. The other is to what pin?

    2. Did you have to recompile the Microchip USB bootloader? If so, how did you obtain the source?

    3. What about the CONFIG1H oscillator bits? How do you define that to use the 12Mhz crystal on your board? Should the same thing done for the above LED example?

    Thanks! Greatly appreciate if you can also CC me should you respond.

    • http://www.tautic.com Jayson

      The second button is connected to RB4. The schematic is available here: https://s3.amazonaws.com/tautic1/public/Design/PIC18F14K50-PTH/PIC18F14K50+PTH+USB+Dev+Board+Schematic.pdf

      The source for the bootloader is available from microchip in their “Microchip Application Library” http://www.microchip.com/mal look under USB, Device – Bootloaders, HID. I’m actually using the stock .hex file, no changes were made to it.

      As for the oscillator config, most of the demos in the MAL above intended for microchip’s “low pin count” board will compile and run on this board. Basically what you’re doing with the clock, is using an internal pll to bump the 12MHz up to the 48MHz required for USB.

      Glad you like the board!

      • AL

        Thanks for the answers. Now that I see the schematic, which has only one button connected to the MCLR pin. This is the button used to enter the bootloader. I take you use Microchip’s PDFSUSB tool to upload program into the PIC, for example, the LED program example you provided?


        • http://www.tautic.com Jayson

          I use the HIDBootloader.exe file located the MAL under: \USB\Device – Bootloaders\HID

          • AL

            Oh, wow. I have such a problem with the PIC18F14K50 and Microchip bootloader. Here are the steps that I do:

            1. Upload the PIC18F14K50.hex file that I found in the ..\USB\Device – Bootloaders\HID directory. This hex file is the distributed usb bootloader from Microchip. I upload the bootloader using the PICKit2.

            Unplug and plug back the USB cable, WINDOWS says that there is a problem with the USB device, as it does not recognize it.

            2. Then I run the \USB\Device – Bootloaders\HID\HIDBootloader.exe for WINDOWS. It says that device is not connected.

            Please advice. Thanks very much!

  • Richard

    Just in case Jayson is busy and can’t answer right away.

    The stock HIDBootloader stays in the bootloader to download new firmware if RB4 is low (SW2 pressed) when the chip is reset or powered up.

    See line 228 in MAL file USB\Device – Bootloaders\HID\Firmware – PIC18 Non-J\main.c
    It sounds like the chip is trying to run non existent or bad “user” code such that windows isn’t seeing a working USB device even though it sees one plugged in.

    PS the HIDBootloader (Windows).exe in MAL v2011-10-18 didn’t work properly for me I had to build the sources in the “Software – Windows” subdirectory to get one that worked as expected. The GUIs are completely different between the two so I’m not sure whats going on.

    It sounds like I’m only one step ahead of you on the learning curve.

  • Ali

    thank you very much Mr Jayson
    the the post was so clear and simple to understood