The Basics of Programming and Using a PIC Microcontroller

Authored By: Doug Bowlus

[db283101@ohio.edu]

 

The purpose of this guide is to help those who are familiar with electronics and writing PIC microcontroller software to program and use PIC microcontrollers at home.  The first part of this guide will help select a PIC programmer by comparing the current options versus cost and functionality.  The second part of this guide will demonstrate the use of one of the programmers mentioned in part I.  The third and final part will explain the setup and use of various supporting hardware necessary to use the PIC microcontroller. 

 

Part I: Selecting a Programmer

          What programmer you will buy depends largely on how much you want to spend and how much technical support you will need.  The table below shows the various options I looked at before making my decision.

 

Name

Manufacturer

Where to buy

Comments

Price (USD)

Picstart Plus

Microchip

Digi-key

Expensive, serial, supports a large range of PICs, should have good technical support

$199

MPLAB ICD2

Microchip

Digi-key

Expensive, usb, works with MPLAB to help debug programs (will require two pins), should have good support. Price includes $40 Universal Programming Module

$228

Kit 149, USB / Serial Port PIC Programmer

kitsrus.com

Amazon Electronics

Cheap, serial/usb, programs a wide range of PICs, must be soldered (it’s a kit), free programming software, limited technical support.  Price includes the fact that a 18Vdc wall wart and serial/usb cable must also be purchased. ZIF socket is recommended, add about $10

$45

PICALL PIC Programmer

Boban Dobaj

Amazon Electronics

Relatively cheap, parallel port computer connection, can be bought as a kit or pre-assembled, supports a wide range of PICs and also some other devices, software registration included, limited technical support, Price includes 18Vdc wall wart and parallel cable.  ZIF socket is recommended, add about $10

$85

K128 - USB all Flash PIC Programmer

kitsrus.com

Ozitronics

Cheap, usb connection, no power supply required… runs off usb power, programs a wide variety of PICs, free software, includes ZIF socket, limited technical support, requires 9 components to be soldered. Ozitronics is located in Australia so this price may change with the exchange rate and shipping might take a while.

$65

WARP-13

Newfound Electronics

See Newfound Electronics Distributors

A bit pricey, serial, programs most PICs and a couple other devices, free software, compatible with the PICSTART PLUS (works with MPLAB), comes with ZIF socket.  May or may not come with power supply and cables depending on distributor.

$100

 

This is by no means a complete list of all the PIC programmers out there; this is simply a list of those that I feel are worth mentioning.  Feel free to browse the manufacturer’s sites to learn more about the programmers to help aid your decision.

 I personally have had experience with the PIC16PRO (not mentioned because it would be worth paying slightly more to get the PICALL).  I had no problems with this programmer for a while and then after reformatting my computer, it began to not work with my computer.  I tested it on an older system (found easily at state universities nowadays) and the programmer worked fine.  This means that the problem I was having was software related but instead of spending more time trying to find the problem, I decided to upgrade to a serial programmer.

The second programmer I ended up buying was the Kit 149, USB / Serial Port PIC Programmer.  I have used this programmer through both the serial port and the usb port and both worked just as well.  I have only had minor problems with this device but this was because I bought it when the software was first being developed.  I was able to fix my problems by using a different version of the software.  Kitsrus has now released KIT149B which should eliminate any issues I had.   

In the near future I would like to buy the K128 - USB all Flash PIC Programmer.  This programmer eliminates the need for a power supply and looks “more professional”.  It also uses the same software as the KIT149 which I will demonstrate in Part II of this guide.

I have not personally used any of Microchip’s programmers or the WARP-13 programmer, however I know people that own them and have heard good things.  These programmers have the added bonus of working with MPLAB so you don’t have to open up a separate program to program your PIC.  This may or may not be worth the money for you.

 

Part II:  Programming a PIC with the KIT149 USB/Serial Port Programmer

          Although this section is specific to the KIT149 programmer, the overall process should be similar for all the programmers mentioned above.  Before you program your PIC, you should have already compiled your software in MPLAB creating a file that has the same name as your assembly code file but ends in “.hex”.  This hex file contains the machine code that will be programmed into your PIC.

 

1) With the KIT149 powered and connected to your computer, open up its software, Micropro.  [Note: With the Microchip programmers and WARP-13 there will be no need to open a separate software program to program your PIC; Instead you should do this in MPLAB.] You should now see something similar to this:

 

 

If your com port settings are correct, “Board connected” will be displayed as in the screenshot. 

 

2)  Select the type of PIC you are using from the drop-down box (lower-right hand corner of the screenshot).  In this example, I am using a PIC16F877.  After doing this, a picture of the PIC will be displayed sitting in a ZIF socket above the drop-down box.  Place your PIC in the programmer (hopefully you purchased a ZIF socket to make removal easy) exactly like the picture.  The 16F877 chip is 40 pins so all you have to worry about is installing it in the correct direction, however with some of the smaller chips, you must place them with pin1 in pin2 of the ZIF socket.

 

3)  From the file menu select Load (or click the LOAD button).  It will now ask you for the location of your hex file.  Locate the hex file you compiled in MPLAB and select Open.  The contents of your loaded hex file will now be displayed in the “ROM DATA” window.

 

4)  Now the fuses must be programmed if this has not been done in your assembly file.  To program the fuses, click the Fuses button.  You should now see something similar to this:

 

Use the drop-down boxes beside each fuse to set the desired values.  The types of fuses available will change depending on which PIC you are using, so if you unsure of what a fuse does, check with the datasheet. 

          To eliminate the need to reselect the correct fuses every time you re-program your PIC, you can specify them in your assembly file.  This is done with the __CONFIG directive.   [Note: The __CONFIG directive requires that you must first use the LIST or PROCESSOR directive in your assembly code to declare the type of PIC you are using.]  A nice explanation of the __CONFIG directive can be found in a Microchip document located at: http://www.microchip.com/download/lit/suppdoc/toots/config.pdf

An example of using the __CONFIG directive is:

__CONFIG _CP_OFF&_WDT_OFF&_BODEN_OFF&_PWRTE_ON

This line will disable code protection, turn off the watchdog timer, turn off the brown out detect and turn the power up timer on.

 

5)  Now that you have loaded your hex file and set your fuses, it is time to hit the program button.  This will clear the PIC of its previous code if it is not blank and then fill the PIC’s ROM with your new code.  At this point you can use the Verify button to ensure your code was properly programmed and then remove the PIC from the ZIF socket and use it in your circuit.

 

Part III: Using a PIC Microcontroller Without a Demo Board

          If you’ve taken microcontroller classes at a university, chances are, you used a demo board.  Using a demo board helps you concentrate on learning to program software for the microcontrollers, but ultimately this makes you unaware of the supporting hardware required.  The supporting hardware for a PIC is very minimal but without it, your microcontroller will not run.

          For most PICs there are three main things the PIC needs to run.  The first of these is power.  You should connect the appropriate positive voltage (typically 5 volts) to the Vdd pin and then connect ground to Vss.

The second thing the PIC needs is an oscillator.  Depending on the PIC, you have different options for the type of oscillator you can use; I will show how to hook up a TTL crystal oscillator.  You will note in the schematic to the right that pins 7 and 14 are hooked to ground and positive 5 volts respectfully.  Pin 8 is then connected to the OSC1 pin on the PIC. 

The reset circuit is the last thing that should be built.  If there is no need to have a reset button, simply pull the MCLR_bar pin of the PIC high with a pull-up resistor.  To add a reset switch, keep the pull-up resistor and add a switch connected to ground as shown in the schematic.

 

Helpful Resources:

 

Microchip’s Website (http://www.microchip.com) – Datasheets, Application Notes, Samples

 

Easy Microcontrol'n A Beginner's Guide to Using the PIC Microcontroller, by David Benson

 

Microcontrol'n Apps, PIC Microcontroller Applications Guide by David Benson

 

Jameco – Electronics supplier

 

Digi-key – Electronics supplier

 

 

*Any questions or comments, contact me: [db283101@ohio.edu]                                                   

  © Doug Bowlus  May 25, 2003