Wednesday, 1 April 2009

Starting a PIC project in MPLAB

This post is a work in progress - I'll get back to it later....

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.

Friday, 27 March 2009

Prototyping Test Equipment

I am trying to construct a prototype device that will read in analogue data signals, do some processing on them, display them to an LCD screen, store them to some form of electronic memory and allow the data to be downloaded vis a USB connection to a PC.

In order to do this my starting point was an Olimex PIC development board. I bought the PIC-USB-4550 development board from Cool Components. This was pretty cheap (around £20) and arrived quickly.
You can read the features of the board on the website but the main reasons I used this board and device (PIC 18F4550) were:
* Had a built in USB 2.0 interface
* 13 channels of 10bit analogue inputs
* 32kB program memory
* 2kB RAM
* Numerous additional I/O pins
* Bootloader so I didnt need a programmer (more on this feature in another post)

This was wired up along with a small breadboard prototyping area, an LCD 16 x 2 display and some I/O devices (8 x LEDs, 3 x push switches, 8 x DIP switches, potentiometer, thermistor and buzzer - from an old PIC16F84 delopment board I had). USB and ICSP (In-Circuit Serial Programmer) sockets were already on the development board.

So now I had most of the hardware (I will build up more as this project progressess), I also needed to look at the software options. I had experience with basic assembler programming and knew that it would be a hassle to do all the programming in assembler (I want to have a life as well). So I looked at available C compliers. After looking at a number of options (maybe I'll put that into another post) I am using the free one from Microchip (C18 c compiler). They supply this for a free trial period of 60 days, after which it becomes limited to non-optimised compliation. But its working OK for now - maybe when I have some money and need for it I can upgrade to a good paid-for version.
In order to create the HEX files for programming I used Microchips MPLAB suite, with which the C18 compiler interfaces. This is also available for free from Microchip.

In addition to this another program is required to write programs to the PIC18F4550 using the bootloader (i.e. via a USB connection). This is called theMCHPFSUSB Framework (Catchy title, eh?) and can be downloaded here.

So thats about everything you need. Sounds easy, doesn't it?
I'll start to write up experiences with using this in future posts.

EDIT: It might also be useful to note that I am using computer running Windows Vista.....

First post

This blog is a record of my attempts to program PIC micro controllers from Microchip.
It is mainly meant as a record to myself of what I did and tricks I have learnt, but I hope it might also be useful to others starting out on PIC micro controllers.
I'm not going to make this look fancy nor do full spell checking - its mainly a reference.
Please get in touch if you have any questions/queries/useful information for me.
Lets begin....