Wednesday, 1 April 2009

Loading a program onto the PIC

Once I had the Olimex USB development board I wanted to upload a simple program, just to prove everything was working OK. I usually start very simple (i.e. just set an output high so that an LED will light) and then build upon that. I have always found small steps to be the best approach.

First thing to do was to download and install Microchip's MPLAB (I used version 8.10). This is a free download. Just follow the instructions to install it - there should not be any complications.

Secondly, I wanted to program in C, hence I needed a C compiler. I decided to use Microchip's C18 Student edition, as this was free. This was downloaded and installed easily.

Lastly, you need to install the USB bootloader program. This is an additional program which is designed to work with a 'bootloader' so that the PIC can be programmed directly through the USB connection (without any fancy programmer).
I'll take a step back....

What is a bootloader?
A bootloader is a small program which is installed onto the PIC using a PIC programmer. The program has been written to enable the rest of the PIC memory to be programmed via the USB connection. This means that no additional programmer is required and the PIC can be programmed directly from a computer. It is a very small piece of code (around 512B). Microchip have written their own bootloader and an application note on the bootloader. Numerous other people have also written their own bootloader code.

The PIC18F4550 on the Olimex board came with the Microchip bootloader already programmed.

In order to program the PIC an additional program is required (the USB bootloader program). This takes the hex file created by the compiler (MPLAB) and talks to the bootloader code in the PIC in order to re-program the PIC.
The USB bootlader program is called MCHPFS_USB and is also free from Microchip but does not come with so much documentation. This has been written to demonstrate their PICDEM FS USB development board.

The PIC needs to be placed into 'program' mode - so that the bootloader can program the chip, rather than letting the usual program code run. This is done by pressing and holding the reset and input buttons. The reset can then be released and then, after a short delay, the input button.

When this happens, windows will look for a driver. This was found on my computer at: c:/Program Files/Microchip Solutions/USB tools/MCHPUSB Custom Driver/MCHPUSB driver/Release/mchpusb.inf
Once found it should not be required again.

So the basic flow of programming is:
1: Write C code
2: Compile with MPLAB (MPLAB can compile and bring together code from various places and compiles it into the HEX file required to write to the PIC)
3: The HEX file could be written to the PIC with a programmer. OR...
4: Use MCHPFS_USB to program the PIC via the bootloader. This requires the PIC to be connected and in 'bootload' mode and requires a correct HEX file.

So that's what I did. I took the basic PIC18F4550 assembler file (this can be found in the MPLAB file: on my machine c:/Program Files/Microchip/MPASM suite/Template/Code/PIC18F4550TEP.ASM ), loaded this into MPLAB. Compiled it.
Then I praogrammed it into my PIC using MCHPFS_USB. There was a warning that I was about to change some parameters but I did not think too much about it.
It did not work.
Worse still, my bootloader also did not work.
Obviously I had done something very wrong and some of you might have guessed what it was. I'll explain in the next post.

No comments:

Post a Comment