Home > Microcontroller >Microcontroller Production > Bootloader for 16F87x series PIC microcontrollers

Bootloader for 16F87x series PIC microcontrollers

Source: InternetPublisher:saIDcCoBfs Keywords: PIC microcontroller Updated: 2026/03/31

Bootloader for 16F87x PIC microcontroller

The bootloader allows hexadecimal files to be downloaded directly to the flash memory of the PIC or other microcontrollers. The bootloader receives the user program via the PIC's UART and writes it directly to the program memory (self-programming). This feature significantly speeds up the development process because the chip remains within the target circuitry, eliminating the need to move between the target circuitry and the programmer.

When the bootloader is not installed, all memory in the PIC is available for user programs. For the 16F873, that's 4KB (0x000 to 0xFFF). Installing the bootloader means that some memory is used by the bootloader. The user can then download their program to the remaining memory space. In Figure 1, the bootloader occupies 256 words (0xF00 to 0xFFF), which is 6% of the 16F873's memory. The disadvantage of losing 6% of memory is minor compared to the fast program download and more user-friendly development process.

Bootloader for 16F87x series PIC microcontrollers

The first four words (0x000 to 0x003) in the flash memory contain an instruction to jump to the Bootloader. The original four words are identified and reallocated to the Bootloader's memory space during the download process. At startup, the Bootloader waits 200 ms to begin the download. If no download is detected, the Bootloader activates the user program, starting from the first four reallocated words, and then jumps to the remaining user program.

Programming Bootloader

The Bootloader package contains all the necessary software. I will explain how to program Bootloader version 8.23 ​​into the 16F873 using an example.

Choose a hexadecimal file with the correct crystal oscillator frequency and the desired download speed. I chose the file 30419-bootldr-v26-16F873-74-04Mhz-19200bps.hex for my 16F873, with an oscillator of 4 MHz and a download speed of 19200 bps.

Load the hexadecimal file into the PIC programmer and check the fuse settings before programming. I do not want low-voltage programming enabled (which is enabled in the hexadecimal file). If the low-voltage programming fuse is enabled, the PIC may become unstable during operation. My recommended fuse settings are shown in Figure 2.

Figure 2. Fuse settings for programming the bootloader into a 16F873 with a 4 MHz oscillator.

The fuses selected during bootloader programming cannot be changed by the downloaded user program. This is because the bootloader does not modify the PIC's configuration words.

Download user program

The Bootloader package includes a PIC programmer. The PIC programmer is used to transfer hexadecimal files from your PC to the Bootloader in the target chip. After programming the Bootloader to the PIC, test the download process by downloading a suitable program to the PIC. The UART test program for the 16F873 and a description of the 16F873 test board can be found elsewhere on this website. The test program is also included in the Bootloader package.

Figure 3. PIC programmer writing to the target PIC

Load the hexadecimal file into the PIC downloader (click Search or press F2), activate the downloader (click Write or press F4), and reset the PIC. Resetting activates the Bootloader and makes it listen for new downloads on the serial port. The Bootloader and PIC downloader handshake, and the hexadecimal file is downloaded. A few seconds later—voilà—the new user program is running in the PIC.

If the downloaded program will use the PIC's serial port, you must close the PIC downloader and launch a terminal program (such as HyperTerminal). In my case, both the PIC downloader and HyperTerminal use COM2 – but not simultaneously!

Memory reservation

Memory reservation is necessary to preserve the code space used by the bootloader. For the 16F873, in CCS C (PCM 3.060), memory reservation does the following:

#org 0x0F00,0x0FFF {} //16F873 bootloader resides here

In Microchip MPASM, for the 16F873, the memory is reserved to do the following:

ORG 0x0000 ;User program starts here

...

ORG 0x0F00 ;Bootloader starts here

NOP

END

If the user program exceeds 0x0EFF, the last few lines will generate a compiler error.

Hint #1

The bootloader reallocates program words between 0x000 and 0x003. This means that the first four words of the user program must contain a GOTO (long jump) instruction pointing to the rest of the user program. This is typically the case when using a compiler such as CCS's C compiler PCM. If you are using an assembler such as Microchip's MPASM, you must manually insert the GOTO, as follows:

ORG 0x000

GOTO Begin

ORG 0x004

Begin User program starts here

Hint #2

If the PIC's serial port is being used by a downloaded program, and you want to download a new program, the PIC downloader may be unable to access the PIC because of traffic on the port. In this case, you must first press and hold the PIC's reset button while clicking Write (or pressing F4) on the PIC downloader. The downloader will begin searching for the PIC. Releasing the PIC's reset button will start the download.

Bootloader for 16F87x series PIC microcontrollers

Bootloader for 16F87x series PIC microcontrollers

Bootloader for 16F87x series PIC microcontrollers

Bootloader for 16F87x series PIC microcontrollers

Bootloader for 16F87x series PIC microcontrollers

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
community

Robot
development
community

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号