Home

Electronic Projects
  • Micromite Microcontroller
  • Micromite Plus
  • Super Clock
  • Boat Computer MkII
  • Parking Assistant
  • Micromite LCD Backpack
  • ASCII Video Terminal
  • GPS Controlled Clock
  • GPS Tracker
  • Colour Maximite Computer
  • The Original Maximite
  • The mini Maximite
  • Intelligent Fan Controller
  • GPS Synchronised Clock
  • GPS Boat Computer
  • GPS Car Computer
  • Making the GPS Computer
  • Energy Meter Firmware
  • ISM Band Scanner
  • Utility Power Supply
  • Precise Voltage Reference
  • Game of Pong
  • Water Level Meter


  • General Articles
  • Problems in Open Source
  • The Maximite Story
  • Programming PIC Micros
  • MMBasic on the UBW32
  • The TFT Maximite
  • Surface Mount is Easy
  • Measuring Capacitor ESR
  • EM-408 GPS Module
  • SG12232A LCD Driver
  • Custom PC Boards
  • The Gerber Format


  • Reviews
  • Hantek DSO-2250 Scope
  • Rigol DS1000 Scope
  • PIC C Compilers
  • Brickbats


  • PC Software
  • Weather Station
  • Mazing


  • About

     

     

     

    This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Australia (CC BY-NC-SA 3.0)

    Programming PIC Microcontrollers

     

    Most of my projects rely on a Microchip PIC microcontroller which must be programmed with the appropriate firmware to do its job.  But programming these small processors can be a big hurdle for a lot of people.

    As it happens, Microchip try to make it easy for you by providing cheap programmers and free software.  They hope that this will encourage you to buy more of their chips.   The result is a win-win situation.

    So, in this article I intend to show how easy it is to program a PIC microcontroller with an investment of less than $20.






    Note that this page only covers programming the Microchip PIC series of processors.  There are many other microcontrollers out there (Atmel, TI, etc) and these will require different programmers and software.

    The Microcontroller

    Nearly all modern microcontrollers use flash memory for storing the program.  This is why programming a microcontroller is frequently referred to as "flashing" the chip.

    Flash memory can be erased and reprogrammed many times and the stored data is retained even when the power is removed.  In addition to the program there are other memories in the chip that are also programmed in the one operation; these include configuration settings (sometimes called "fuses"), EEPROM (another type of erasable memory) and bootload areas.  The information to program all of these is contained in the one file which generally has an extension of .hex (for example "FirmwareV1.hex").  Often the file is referred to as a "hex file".

    The Programmer

    The best programmer for a PIC microcontroller is undoubtedly the PICkit 3 from Microchip.  It is relatively cheap, it is supported by Microchip's software and will program almost any microcontroller in their range. 

    There are other more fancy programmers offered by Microchip (ie, the ICD3) and there are cheaper and older programmers (ie, PICkit 2) but the PICkit 3 is guaranteed to work and it is all that you need when you just want to load some firmware into a chip.

    You can buy the genuine PICkit 3 from Microchip for US$48 + freight (link) or from their distributors. The part number is PG164130 and if you search Google for this part number you will find many suppliers.

    There are also clones of the PICkit 3 which seem to work just as well but are much cheaper.  As a test I purchased one for US$18.80 with free freight and I cannot fault it.  The Microchip software recognised it as a genuine PICkit 3 and it worked just as well as the genuine article.

    A good place to find cheap clones is eBay.  Just search for PICkit3.

    So, if you just want a cheap method of programming Microchip microcontrollers purchasing a Pickit 3 or a clone would be the way to go.   At the ridiculously low prices for a clone you can afford to buy one just to program a single chip and then add it to your tool box for a possible future project.

    By the way, don't be tempted by the cheaper PICkit 2 that is also available.  It is outdated and will not program the more modern chips.

    In Circuit Serial Programming

    Most Microchip microcontrollers use an In Circuit Serial Programming connection (abbreviated to "ICSP") to transfer the program to the chip.  Normally the printed circuit board (PCB) will have a connector for this and it will look something like the connector highlighted in the photo on the right.

    If you do not have such a connector (perhaps you are programming a bare chip) you will have to figure out the connections yourself.   The following table, which lists the pinouts on the ICSP connector, should help:

    PIN NAME USAGE
    1 MCLR Master reset pin on the microcontroller
    2 Vdd Microcontroller supply voltage
    3 Vss Supply ground or zero volts pin
    4 PGD Programming data signal
    5 PGC Programming clock signal
    6   - Not Used -


    The programmer monitors the voltage on pin 2 (Vdd) and uses that to determine if the chip to be programmed is actually powered up.  To start the programming operation the programmer will drive pin 1 (MCLR) to a voltage higher than Vdd (normally 12 to 15 volts).  It then transfers the data using pin 4 (PGD) and pin 5 (PGC).  Internal logic in the chip will take that data and write it to the chip's flash memory.

    Connecting It Up

    In the above illustration of a PCB you would be able to plug the PICkit 3 directly onto the six pin ISCP header connector.  Pin 1 of the PICKit 3 connector is marked with a white triangle and normally pin 1 of the connector on the PCB is marked with the number 1 or some other symbol.

    If you cannot fit the programmer into the space you will need a six conductor cable with a male header and a female header at the other end as illustrated on the right (my clone came with this which was handy).

    When programming a microcontroller you only need to supply power to the chip (and connect a capacitor to Vcap if the chip has that pin).  You do not need any other components as the chip will startup using its internal oscillator and that is all that the programmer will need.

    The Software

    To drive the PICkit 3 you need to install Microchip's MPLAB X software development system on your personal computer.  This comes in various versions for Windows, Mac OS and Linux.  Unfortunately the full install includes lots of stuff that you do not need (such as a full integrated development environment) but the important part is MPLAB IPE which is the programmer component (IPE stands for Integrated Programming Environment).  This is normally installed as an icon on your desktop.

    When you startup MPLAB IPE you have a fairly straightforward screen which allows you to specify the part number of the chip and the hex file to program into it.  Just go through the numbered steps illustrated below to program your chip.

    Errors

    When you click on the "Program" button the software will instruct the PICkit 3 to erase the chip, program it with the new firmware then read back that program to verify that it has been properly written.  At the end you should get the message "Programming complete".

    Of course it may not be as easy as that. Three of the more common errors that you might encounter are:

    "Target Vdd not detected".  This usually means that you have not correctly connected to the ISCP programming connector or that the device that you are trying to program is not powered.

    "Failed to program device" or "Cannot read device ID".  The programmer could tell that you had connected to something (because Vdd was present) but it could not communicate with the chip.  This usually means that something was interfering with the MCLR, PGD and/or PGC lines (ie, they were not connected or other components were loading down the signals).

    "Target Device ID does not match expected Device ID".  This means that the programmer has detected a device that is different from the device that you specified in step 1 above.

    Power To The Device

    Normally the chip that you are programming will be powered by the circuit that surrounds it but you also have the option of powering the chip from the PICKit 3 itself.  This can come in handy when you are programming a chip which will be later plugged into its eventual home but as the moment is just a free chip.  Note that the PICkit 3 can supply a maximum of 30mA so it is only good for powering a bare chip and nothing else.

    To tell the PICkit 3 to provide the power you need to select Settings --> Advanced Mode.  You are then required to put in a password ("Microchip") to enter Advanced Mode.  Finally select the Power tab and tick "Power Target Circuit from Tool".   The PICkit 3 will then supply power out of pin 2 (Vdd) rather than just measuring the voltage on that pin (which is what it normally does).