MPLAB Tutorial (v6.62)
MPLAB Tutorial (v6.62)
If you are new to MPLAB IDE and MPLAB SIM, this tutorial should help you
understand the basics of using the integrated development environment to set up your
application for development and of using the simulator to debug your application
code. You will set up a simple application for the PIC16F84 device in a single project,
single workspace environment.
Note: Selected third party tools are also supported by MPLAB IDE. Check the release notes
or readme files for details.
System Requirements
The following minimum configuration is required to run MPLAB IDE (6.20):
Note: For some Windows OS's, administrative access is required in order to install software on a PC.
• If installing from a CD-ROM, place the disk into a CD drive. Follow the on-
screen menu to install MPLAB IDE. If no on-screen menu appears, use
Windows Explorer to find and execute the CD-ROM menu, menu.exe.
1
• If downloading MPLAB IDE from the Microchip web site
(www.microchip.com), locate the download (.zip) file and click on it to save
it to your PC. Then, unzip it and execute the resulting file to install.
To start the IDE, double click on the icon installed on the desktop after installation or
select Start>Programs>Microchip MPLAB IDE vx.x>MPLAB IDE vx.x. A screen will
display the MPLAB IDE logo followed by the MPLAB IDE desktop.
Tutorial Overview
In order to create code that is executable by the target PICmicro MCU, source files
need to be put into a project and then the code is built into executable code using
selected language tools (assemblers, compilers, linkers, etc.). In MPLAB IDE, the
project manager controls this process.
The Project Wizard will guide us through most of these steps and will make this series
of tasks easy.
Note: Some aspects of the user interface will change in future releases and the screen shots in this
tutorial may not exactly match the appearance of the MPLAB IDE desktop in later releases.
2
Select Device
To show menu selections in this document, the menu item from the top row in
MPLAB IDE will be shown after the menu name like this MenuName>MenuItem. So
to choose the Select Device entry in the Configure menu, it would be written as
Configure>Select Device.
3
In the Device dialog, select the PIC16F84 from the list if it's not already selected.
The "lights" indicate which MPLAB IDE components support this device.
4
Creating Source Code with the Editor
Select File>New to open an empty editor window in which to type your source code.
For more on using the editor, see MPLAB Editor Help.
list p=16f84
include <p16f84.inc>
c1 equ 0x0c ; Set temp variable counter c1 at address 0x0c
This code is a very simple program that increments a counter and resets to a
predetermined value when the counter rolls over to zero.
Once you have completed entering the code, select File>Save and save the file as
tutor84.asm. You may save it to any directory; you will move it, if necessary, into
the project directory in the next step.
Create Project
Next, we'll create a project using the Project Wizard. A project is the way your files
are organized to be compiled and assembled. We will use a single assembly file for
this project. Choose the Project>Project Wizard.
From the Welcome dialog, click on Next> to advance in the Project Wizard.
The next dialog (Step One) allows you to change the device, which we've already
done. Make sure that it says PIC16F84. If it does not, select the PIC16F84 with the
menu.
5
Select Language Tools
Step Two of the Project Wizard sets up the language tools that are used with this
project. Select Microchip MPASM Toolsuite in the top pulldown. Then you should see
MPASM, MPLINK and MPLIB show up in the Toolsuite Contents box. You can click on
each one to see its location. If you installed MPLAB IDE into the default directory,
the MPASM assembler executable will be:
If these do not show up correctly, use the browse button to set them to the proper files
in the MPLAB IDE subfolders.
6
Put Files in Project
Step Three of the wizard allows you to name the project and put it into a folder. This
sample project will be called tutor84, and using the Browse button, the project will
be placed in a folder named My Documents.
Step Four of the Project Wizard allows us to select files for the project. Choose the file
named tutor84.asm. The full path to the file will be:
C:\My Documents\tutor84.asm
7
Press the Add>> button to move the file name to the right panel, and click on the check
box at the start of the line with the file name to enable this file to be copied to our
project directory.
Make sure that your dialog looks like the picture above, with check box checked, then
press the Next> button to finish the Project Wizard.
The final screen of the Project Wizard is a summary showing the selected device, the
toolsuite, and the new project file name.
After pressing the Finish button, look at the Project Window on the MPLAB IDE
desktop. It should look like this. If the Project Window is not open, select
View>Project.
Files can be added and projects saved by using the right mouse button in the project window. In case
of error, files can be manually deleted by selecting them and using the right mouse click menu.
8
Build the Project
Assembling the file can be accomplished in several ways. The method described here uses the
Project>Build All menu item. This will execute the MPASM assembler in the background.
Once the assembly process is complete, the Output Window will appear.
You have intentionally entered at least one error if you entered the code as written in
previously. The last goto in the program references a nonexistent label called bug.
Since this label has not been defined before, the assembler reports an error. You may
have other errors as well.
Using the mouse, double click on the error message. This will bring the cursor to the line in
the source code that contains the error. Change bug to start. Use the Output window to help
find the errors, and repair any other bugs in your source code. Reassemble by executing the
Project>Build All menu function. This process may take a couple of iterations.
• Project>Build All
• Right-click on the project name in the project window and select Build All
• Click the Build All icon on the Project toolbar. Hover the mouse over icons to
see pop-up text of what they represent.
The Output window shows the result of the build process. When you’ve fixed all
errors in the source code, the Output window will display “BUILD SUCCEEDED”.
You now have a complete project that can be executed using the simulator.
9
Upon a successful build, the output file generated by the language tool will be loaded.
This file contains the object code that can be programmed into a PICmicro MCU and
debugging information so that source code can be debugged and source variables can
be viewed symbolically in Watch windows.
Note: The real power of projects is evident when there are many files to be compiled/assembled and
linked to form the final executable application - as in a real application. Projects keep track of all
of this. Build options can be set for each file that access other features of the language tools,
such as report outputs and compiler optimizations.
The simulator is a software program that runs on the PC to simulate the instructions of
the PICmicro MCU. It does not run in "real time," since the simulator program is
dependent upon the speed of the PC, the complexity of the code, overhead from the
operating system and how many other tasks are running. However, the simulator
accurately measures the time it would take to execute the code if it were operating in
real time in an application.
Note: Other debug execution tools include MPLAB ICE 2000, MPLAB ICE 4000 and MPLAB ICD 2.
These are optional hardware tools to test code on the application PC board. Most of the MPLAB
IDE debugging operations are the same as the simulator, but unlike the simulator, these tools
allow the target PICmicro MCU to run at full speed in the actual target application.
Select the simulator as the debug execution tool. This is done from the
Debugger>Select Tool pull down menu. After selecting MPLAB SIM, the following
changes should be seen.
1. The status bar on the bottom of the MPLAB IDE window should change to
MPLAB SIM.
2. Additional menu items should now appear in the Debugger menu.
3. Additional toolbar icons should appear in the Debug Tool Bar.
TIP: Position the mouse cursor over a toolbar button to see a brief description of the
button's function.
10
Figure: MPLAB IDE DESKTOP WITH MPLAB SIM AS DEBUGGER
Next we select Debugger>Reset and a green arrow shows us where our program will
begin. The first instruction in memory jumps to the label called Start, where we put
our code. This instruction jumps over the PIC16F84 vector areas in lower memory.
Figure: Debug>Reset
To single step through the application program, select Debugger>Step Into. This will
execute the currently indicated line of code and move the arrow to the next line of
code to be executed.
There are shortcuts for these commonly used functions in the Debug Tool Bar.
11
TABLE: DEBUG SHORT CUT ICONS
Debugger Menu Toolbar Button Hot Key
Run F9
Halt F5
Animate
Step Into F7
Step Over F8
Step Out Of
Reset F6
Click on the appropriate icon on the toolbar or use the hot key shown next to the menu item. This is
usually the best approach for repeated stepping.
Now press the Step Into icon or select Debugger>Step Into to single step to our code
at Start.
12
Press <F7> (Execute Single-Step) a few times and watch the values update in the file register
window. We put the counter variable at address location 0x0C. As the temporary counter is
incremented, this is reflected in the file register window. File registers change colors when
their value changes so that they can easily be noticed on inspection. However, in very
complex programs, many values may change, making it difficult to focus on one or two
variables. This problem can be solved by using a Watch window.
Select View>Watch to bring up an empty Watch Window. There are two pull downs
on the top of the Watch Window. The one on the left labelled "Add SFR" can be used
to add the Special Function Register, WREG, into the watch. Select WREG from the list
and then click Add SFR to add it to the window.
The pull down on the right, allows us to add symbols from our program. Use that pull
down to add the c1 variable into the Watch Window. Select c1 from the list and then
click Add Symbol to add it to the window.
The watch window should now show the address, value and name of the two registers.
Note: You also may add items to the watch window by either dragging them from the SFR, File
Register or Editor window or directly in the window under symbol name and typing in the item.
13
We could continue single stepping through this code, but instead we'll set a breakpoint
just before the first increment of the temporary counter c1. Set a breakpoint by putting
the cursor on the line and clicking the right mouse button.
Select Set Breakpoint from the context menu. A red "B" will show on the line.
14
Select Debugger>Run to run the application. A text message "Running..." will briefly
appear on the status bar before the application halts at this first breakpoint.
The watch window should now show that the variable c1 was incremented by one.
This would seem to indicate that the program is working as designed. You can single
step through the code, or run more times to verify that things are acting properly.
It would be interesting to calculate time spent in loop. We could use the data book to
determine how long each instruction would take in our loop and come up with a pretty
accurate number. Or we could use the MPLAB StopWatch to measure the time. We're
interested in the time our whole loop takes to execute, so if we set another breakpoint
on the instruction goto Start, we can measure the time between breakpoints.
Use Debugger>StopWatch to bring up the StopWatch dialog. Make sure that a two
breakpoints are set at the incfsz c1,F and at the goto Start instruction, and then
press Debug>Reset and then Debug>Run to halt at the incfsz c1,F instruction. With
the default processor frequency of 4 MHz, the StopWatch should show that it took 4
microseconds to reach the first breakpoint.
Now press Zero button in StopWatch window and disable or remove breakpoint at
incfsz c1,F instruction. Execute Run again to go around the loop, and note that the
StopWatch shows that it took about 740 milliseconds. To change this, you can change
the initial value in the c1.
15
Using Trace
To view which instructions are executed from a Run to a Halt or Break, use the trace
function.
• Select Debugger>Settings and click the Trace/Pins tab. "Trace Enable" should
be checked by default. If it is not checked, click to check it now. Click OK.
• Select View>Simulator Trace to open the Trace window.
• If "Trace Enable" had been checked by default, there will be data in this
window when you open it. These are the instructions that executed when you
ran to the breakpoint in the previous topic.
• If "Trace Enable" had not been checked by default, there will be no data in this
window when you open it. To fill the window with data, repeat the "Using
Breakpoints" procedure from the previous topic. When the breakpoint halts the
program, click on the Trace window to make it active and view the instructions
that executed when you ran to the breakpoint.
For more on the meaning of the data in this window, go to the topic on the Trace
window.
Although this tutorial program does not require pin I/O to function, your own
application may require, for example, the pushing of a button to activate a procedure.
By using pin stimulus, you can simulate a button push to test your code.
• Save your pin stimulus to a file by selecting Save under "Pin Stimulus file".
You may load this pin stimulus file into another project by using Load under "Pin
Stimulus file".
Note: Despite its listing in the drop-down list, you cannot load old clock stimulus files (*.sti).
16
Using File Stimulus
The second type of stimulus is called file stimulus which provides triggered stimulus
on I/O pins or file registers, set up from files. There are three types of files used in file
stimulus. The hierarchy of these files is as follows:
When you are creating file stimulus, you should proceed as follows:
You will next create a Synchronous Stimulus file which will make use of this Register
Stimulus file.
Note: You cannot inject file values into port registers, e.g., PORTB. Use pin values
(explained next.) Also, you cannot inject file values into the W register.
17
o Select "File" under "Value". This will open a dialog to select a register
stimulus file. Select reg1.rsti and click Open to enter this file under
"Value".
• Click Add Row under "Edit Controls". Another row will appear for entering
data.
• Set up the row for pin injection as follows:
o Select "Cycle" under "Trigger On".
You will now create a File Stimulus file including just this Synchronous Stimulus file.
• Click Save Setup under "File Stimulus". This will save all the files in the file
list box as part of a master stimulus file. In this case, there is only one file.
Enter file1 for "File name" and click Save.
You will now run your program again and observe the affects of the stimulus.
Note: Do Not Close the Simulator Stimulus dialog. You may move it almost off the screen if you need
to view other dialogs.
First, you will need to add TRISB to the watch window. Do this by clicking on the
watch window, selecting TRISB from the SFR list, and then clicking Add SFR.
You could add another row to the ssti file that would toggle the RB4 pin, i.e., "Trigger
On" = "Cycles", "Trig Value" = 15, "Pin/Register" = "Pin:RB4", "Value" = 0.
18