A Guide to Simulating and Programming PIC Microcontrollers using the MPLAB IDE program
and either the ICD (in circuit debugger) or the PICSTART Plus programmer. 

My thanks to Kwabena Adu, '01, for writing the first draft of this document

 

Welcome to MPLAB IDE, the MPLAB-SIM Simulator, and PICSTART Plus firmware. This program offers the user a complete suite of tools that enable you write your source code, compile, test and then finally program your microcontroller.

Your source code will be written in the C language using MPLAB's in built text editor. The source file is then compiled by invoking the PCB or PCM compiler, still in the MPLAB window.  The code can be tested either using MPLAB's simulator.  Finally you can activate the PICSTART firmware, place the microcontroller in the PICSTART programmer and then program!

Before you begin you should read the Intro to C on the PIC.

All this, is done without leaving the MPLAB window. Below are step by step instructions to accomplish the above.

More information about MPLAB, including a tutorial is available from the microchip web site (link to MPLab page).  (The tutorial is at the bottom of the page).

 

Open Mplab

Open the 'MPLAB' program.  The program is located on three of the computers in Hicks 310.  You will get a window like the one shown below.  (A dialog box will open - just hit "cancel" if you don't wish to open the most recently used project file).  If the menu looks different hit the "Swap Toolbar", , button at the upper left of the MPLab until it looks right.

Create A New Project or Open an Old One

Go to the "Project" menu and choose "New Project" (From now on I will denote this process of choosing an item from a menu as something like: Project->New Project). A dialog box will open up. Give your project a name ending with the extension ".pjt". Save the project in the folder in the MPLab/example folder.

Alternatively you may open an already existing project using "Open Project"

Edit The Project

A dialog box "Edit Project" automatically pops open after you click OK in the "New Project" box. In the box below I have named the project "x.hex".  You should choose something more original.  Note the the "Language Tool Suite" must be set to "CCS".

Select the microcontroller

Select the microcontroller you are working with in the "Development Mode" dialog box by clicking on the "Change..." button.

When you click on "Change...", the "Development Mode" dialog box opens up.  Make selections as shown (MPLAB ICD Debugger and PIC16F873 processor).

If this is the first time you have worked with  this project, you will probably get two error dialogs that you can ignore, as shown.

 

Make sure that "Language Tool Suite" box in the "Edit Project" dialog box has "CCS" selected.  Move the cursor to the "Project Files" box at the bottom of the dialog box, click on your ' .hex' file which is listed there. You will notice that "Node Properties" becomes active.

Click on "Node Properties", or alternately double click on the displayed ' .hex' file in the "Project files" box.

Node Properties

Under "Options", click on "PCM" which is the compiler that you will be using for the PIC 16F873 microcontroller. You need not do anything else. Click on the "OK" button at the bottom of the box. You need not repeat this process so long as you stay within the same project. It is advisable to check if you are using the desired compiler any time you start up Mplab.

"Add Node" will be active when you return to the "Edit Project" box. Leave it alone for now click or OK. You will come back to that after you have began writing your program.

Save your project selecting Project->Save Project.

 

Writing Your Source Code Using the Mplab Text Editor.

Before you begin you should read the Intro to C on the PIC.

Choose File->Open. A dialog box will open up. Select 'blank.c' from the Mplab/example folder.  The contents of blank.c is shown below.

#include <16f873.h>		/*Insert definitions for our processor*/
#use delay (clock=4000000)	/*We are running at 4 MHz*/
/*Insert your functions here*/

main() {
/*Insert initialization code here*/
     while (1) {
     /* Insert the main part of your code here*/
     }

}

The file 'blank.c' is a read only template for your source code.  Save 'blank.c' under a new name and store it in the folder 'MPLab/example'.  It is probably a good idea to save the program with the same name as your project.  Make sure that your program file is saved in the same folder as the project file, otherwise you will not be able to compile your program.  Also all the required header files must be present.

Go to Project->Edit Project and click on the "Add Node" button and select your object file. This object file is the source code you just saved.  Click on "OK"

Write your source code remembering to save often under the "File" menu. Saving under the "Project menu" will save the project not the source code.   When you are sure your program is complete, but prior to debugging, you may then build the project. This involves the compilation of your source code and the creation the .hex file needed to program the microcontroller.

 

Build the Project

To Build the project , select "Make Project" from the "Project menu" or hit the button marked .  Note you may need to hit the "Swap Toolbar", , button at the upper left of the MPLab until the build button appears..

MPLAB will show the results of the building process. Building will be reported as a failure if PCB finds a bug in your source code, along with the line number of the error.    Thankfully the bug is usually clearly displayed in the results report, including the number of the line on which the bug appears. Often the error will be on the line preceding the reported location of the error (particularly with missing semicolons).  Good luck hunting bugs :-)

To help you debug your source code, you can open the '*.lst' file from the "Window menu" (Window->Absolute Listing), or you can just work with your source, '*.c', file.  The list file shows the C-code along with its associated assembly language code for those of you who want to see how things are done at the lowest level.

Repeat until you have a successful (no errors) build.

If you don't have an ICD (In Circuit Debugger) you may want to simulate your code, and then program the code on a PIC.  I will assume you are using the ICD, so continue on.

 

Download the code onto a PIC using the ICD

To download the code to your board you need to connect it to a PC.  Connect an ICD module to a PC (in Hicks 310) via a 9 pin serial cable.  Connect the other end of the ICD to your board with a 6 conductor phone cord.  If you apply power to your board, you should see the LED on the ICD module light up.  If this doesn't happen, see me.

After you get the LED to come on, you need to establish communications with your device.  Go to the ICD dialog box and hit the reconnect button.  It should look like the following to indicate that it has communicated with your board.

Hit the "Options" button and make sure you have all options set as shown.

Close this dialog box and go back to the smaller MPLab ICD dialog box and hit the "Program"  button and your code will be downloaded onto your PIC.  You can now run your program, or use the debugger to single step through the program.  Hit the "Swap Toolbar", , button at the upper left of the MPLab until the general toolbar appears.

The following buttons at the top of the MPLab window are useful:

Start (run) the program.
Stop the program.
Single Step through a program, including called functions.
Single Step through a program, but not through called functions.
Reset the processor
Open the Special Functions Register window.  This lets you observe the IO ports and special memory locations
Watch a variable

You can also open "Watch Windows" to look at the value of different variables as your program executes.  For more thorough instructions go to Help->MPLab IDE Help to get general help on the development environment, or to Help->MPLab ICD Help to get help specific to the In-Circuit Debugger.



Comments or Questions?

Erik Cheever
Engineering Department
Swarthmore College