Menu
Microchip disclaims all liability arising from this information and its use. Use of Microchip devices in life support and/or safety applications is entirely at the buyer's risk, and the buyer agrees to defend, indemnify and hold harmless Microchip from any and all damages, claims, suits, or expenses resulting from such use.
Bootloader for Microchip PIC Processors
A bootloader is a small piece of software loaded onto the microchip that allows you to download programs to the chip via a simple serial connection. They have become very popular on the Arduino chips as it negates the need for an expensive programmer.
But there is also a freely available bootloader for the Microchip family of processors. As well as the bootloader code, it comes with a Windows graphical front end for the downloading of your programs to the microchip.
If you want to code using Microchips MPLAB front end using any of the freely downloadable compilers (MPASM, C18, HI-TECH C, Basic etc) then you can without the cost of buying an expensive programmer.
ds30Loader
ds30Loader is the bootloader software and can be downloaded from the authors website at
The bootloader code takes up only about 300 bytes of programming space and is stored at the end of the programming space on the processor. For most processors no change to the actual program code needs to be done apart from removing the CONFIG settings as these are already in the bootloader code . Below is a screenshot of the Windows GUI for dsLoader software.
The picture below shows a Microchip 18F14K22 chip on a breadboard along with a Sparkfun FT232RL USB to Serial breakout board. The chip simply needs power (provided here by the USB connection), 0V, a pullup resistor on the MCLR line, and the TX and RX connections wired to RX and TX on the USB to Serial board. The bootloader runs at power up or when the MCLR pin is taken low. If communication with the ds30Loader program is not made within 3 seconds, then the bootloader runs the program that has been downloaded onto the chip. All very similar to the Arduino bootloader.
To download a program
In the ds30Loader main screen
- Select the compiled hex file
- Select the Device
- Select the baud rate that the bootloader is set to (normally 115200)
- Select the Port for your serial connection
- Reset the microchip by either taking the MCLR pin low or removing and re-applying power
- Click the Download button within 3 seconds
Similar to the following will be displayed in the text window
Initiating download...
Searching for bl .
Found PIC18F14K22 fw ver. 2.0.3
Waiting for bootloader to be ready ...ok
Writing flash ...ok
Download finished
Tx 7.1kB / Rx 389 bytes / 7s
Found PIC18F14K22 fw ver. 2.0.3
Waiting for bootloader to be ready ...ok
Writing flash ...ok
Download finished
Tx 7.1kB / Rx 389 bytes / 7s
Auto Resetting
You can setup the PIC processor to be automatically reset when you want to download a new program (which saves having to manually take the MCLR pin low or by removing and re-applying power) by adding a 0.1uF capacitor between the MCLR pin and the DTR pin from the USB to Serial board. This gives the MCLR pin a low pulse (and hence resets the chips) when communication is started.
RS232 Communication with PIC Microcontroller
Description
This article shows how to do a simple communication via a RS232 interface with a PICmicrocontroller. RS232 is a standard for a serial communication interface which allowsto send and receive data via at least three wires. With the RS232 interface it is possibleto setup a connection between a microcontroller and a PC (via PC's COM port) or betweentwo microcontrollers.
The RS232 interface can be used for many purposes like sending commands from a PC to amicrocontroller, send debug information from a micontroller to a terminal, downloadnew firmware to the microcontroller and many other things.
In this tutorial I will show how to link a PIC microcontroller to a standard PC.On the PC we will use a termial program to send and receive data. Data sent bythe microcontroller will be shown in the terminal window and any key pressed insidethe terminal will send the corresponding key code to the microcontroller.We will use this simple configuration to test and understand the RS232 communication.
Note that modern PCs don't have a serial port so you need to get a USB to serial converter.They are available at low cost.
Block Diagram
The following block diagram shows the whole setup:
For serial communication the line used to transmit data is called TX and the line used toreceive data is called RX. The level converter is required to translate the voltage levelof the microntroller to RS232 voltage level. The microntroller operates at TTL level(0V = logic 0, +5V logic 1) whereas RS232 uses around +/-12V. A very famous RS232 levelconverter is the MAX232 chip.
Hardware
In the schematic below a PIC microcontroller is connected to the RS232 level converter chip.A PIC18F2620 micocontroller is used, but it will also work with any other microcontrollerwhich has a built-in UART.
Schematic
The PIC is running at 10MHz. This will be important later when we configure the baudratefor the serial communication.
Ther RS232 level converter uses the famous MAX232 chip, but any other MAX232 compatible chipwill also work. It just requires 4 capacitors to do its job. These external capacitors arerequired for the charge pump inside the chip which generates the required voltage levels.
The connections on the DB9 connector between pins 1,4,6 and 7,8 are required to satisfythe RS232 hardware handshake signals which we will not use here.
I have developed a RS232 modulewhich allows direct connection to the microcontroller.It consists of a DB9 Female connector, a MAX232 compatible RS232 level converter and thecapacitors. You can find the RS232 module here.
RS232 Cable
To connect the above circuit to the PC we need a RS232 cable. The below picture showsthe necessary connections.
Hardware Picture
Below a picture of the hardware setup. As you can see I have used myPIC16F/18F Experiment Boardand myRS232 Module.
Software
Now since the hardware is ready we have to write the software for the PIC microcontroller.The different compiler vendors provide different ways to setup the UART in the PIC. So I willshow how to use the UART for different compilers.
RS232 communication with CCS C compiler
The CCS C compiler provides a very simple way to do serial communication via RS232. It hidesall the register settings for the user. Only the some parameters have to be provided, therest is done by the compiler. By the way, the CCS C compiler also allows to do RS232 communication viageneral I/O pins, i.e. software based RS232 communication instead of using the built-in UART.That is a really great feature of the CCS C compiler.
Here the code lines which are required to setup the UART for RS232 communication.
As you can see, it is very simple!
The #use delay directive provides the compiler with the information about the clockfrequency at which the PIC is running. We run the PIC at 10MHz with the 4X PLL fuseenabled, hence it is running at 40MHz, so we have to set clock=40000000.
The #use rs232 directive provides the compiler the information about the RS232parameters which shall be used for the communication. It is more or less self explaining:
- baud=57600: specifies the baud rate for communication, we will use 57600 baud
- parity=: specifies whether a parity bit shall be used or not, we will not use it, hence we disable it
- xmit=PIN_C6: specifies the pin to be used for transmission, since we want to use the built-in UART we have to use pin RC6
- rcv=PIN_C7: specifies the pin to be used for receiption, since we want to use the built-in UART we have to use pin RC7
- bits=8: specifies the number of bits per transmitted data
To transmit data the following functions can be used:
To receive data the following functions can be used:
Here a simple demo program for the CCS C compiler.Project download link.To run the demo, the HEX file needs to be flashed into the PIC, e.g. withPICPgm Programmer.
RS232 communication with HI-TECH C compiler
To be done ...
PC connection
Now, since we have the required hardware and firmware, we can connect the hardwareto the PC.
To be able to see the data transmitted from the Microcontroller and to send datato the Microcontroller we have to run a terminal program on the PC. There are a lotof terminal programs available. I will show here how to use HyperTerminal whichis included in Microsoft Windows.
HyperTerminal Setup
If you are using Windows XP start HyperTerminal.You can find it in the Communication Folder: Start Menu->All Programs->Accessories->Communication.
On startup it will ask for a connection name. Just enter a text which will identifythe connection, e.g. use 'RS232 Demo'. In the next dialog select the COM port where thePIC is connected to.
In the following dialog we have to set the connection properties. Needless to say, the propertieshave to match the settings we are using in the PIC.
Once this is done, just reset the PIC and the demo output shal be visible.
Download
- Demo project (CCS Compiler): rs232demo_ccs.zip