Getting Started with MPLAB Software

Transcription

Getting Started with MPLAB Software
DsPIC HOW-TO GUIDE
Getting Started with
MPLAB Software
Contents at a Glance
1. Introduction of MPLAB ..............................................4
2. Features of MPLAB IDE ..............................................5
3. Getting Started with MPLAB IDE ................................6
3.1. Install / Uninstall the MPLAB IDE .............................8
4. Operation of Mplab ................................................. 17
4.1. Setting up the Development Mode ........................ 18
4.2. Creating a Project .................................................. 20
4.3. Select Language Tools ............................................ 22
4.4. Naming the Project ................................................ 23
4.5. Adding files to Project ............................................ 24
4.6. Building the Project ............................................... 28
4.7. Testing Code with the Simulator ............................ 31
5. Getting Started with MPLAB SIM ............................. 33
5.1. Starting Up MPLAB SIM.......................................... 38
5.2. Simulator Stopwatch ............................................. 46
Join the Technical Community Today!
http://www.pantechsolutions.net
5.3. Simulator Trace Analyzer ....................................... 48
5.4. Simulator I/O ......................................................... 48
Join the Technical Community Today!
http://www.pantechsolutions.net
1. Introduction of MPLAB
The MPLAB X IDE is the new graphical, integrated
debugging tool set for all of Microchip’s more than 800 8bit, 16-bit and 32-bit MCUs and digital signal controllers,
and memory devices.
It includes a feature-rich editor, source-level debugger,
project manager, software simulator, and supports
Microchip’s popular hardware tools, such as the MPLAB ICD
3 in-circuit debugger, PICkit™ 3, and MPLAB PM3
programmer.
MPLAB IDE is a Windows Operating System (OS)
software program that runs on a PC to develop applications
for Microchip microcontrollers and digital signal controllers.
It is called an Integrated Development Environment, or IDE,
because it provides a single integrated "environment" to
develop code for embedded microcontrollers. Experienced
embedded systems designers may want to skip ahead to
Components of MPLAB IDE.
Join the Technical Community Today!
http://www.pantechsolutions.net
2. Features of MPLAB IDE
MPLAB IDE is a Windows Operating System (OS) based
Integrated Development Environment for the PIC MCU
families and the dsPIC Digital Signal Controllers. The MPLAB
IDE provides the ability to:
Create and edit source code using the built-in
editor.
Assemble, compile and link source code.
Debug the executable logic by watching program
flow with the built-in simulator or in real time with
in-circuit emulators or in-circuit debuggers.
Make timing measurements with the simulator or
emulator.
View variables in Watch windows.
Program firmware into devices with device
programmers.
MPLAB IDE supports the use of numerous assemblers
and compilers for building code in programming languages.
Join the Technical Community Today!
http://www.pantechsolutions.net
Microchip provides free assemblers and linkers for PIC
MCU and dsPIC DSC devices, as well as compilers (free
student/academic/demo editions and for-purchase full
versions). Third parties provide additional coverage with
language tools for assembly, C and BASIC languages
3. Getting Started with MPLAB IDE
The MPLAB IDE combines project management, make
facilities, source code editing, program debugging, and
complete simulation in one powerful environment. The
MPLAB development platform is easy-to-use and helping
you quickly create embedded programs that work.
The MPLAB editor and debugger are integrated in a
single application that provides a seamless embedded
project development environment.
The MPLAB IDE combines project management, a richfeatured editor with interactive error correction, option
setup, make facility, and on-line help.
Join the Technical Community Today!
http://www.pantechsolutions.net
Use MPLAB to create your source files and organize
them into a project that defines your target application.
MPLAB automatically compiles, assembles, and links your
embedded application and provides a single focal point for
your development efforts.
When you use the MPLAB, the project development
cycle is roughly the same as it is for any other software
development project.
 Create a project, select the target chip from the
device database, and configure the tool settings.
 Create source files in C or assembly.
 Build your application with the project manager.
 Correct errors in source files.
 Test the linked application.
 Debug the executable logic by watching program
flow with the built-in simulator or in real time with
in-circuit emulators or in-circuit debuggers.
 Make timing measurements with the simulator or
emulator.
 View variables in Watch windows.
Join the Technical Community Today!
http://www.pantechsolutions.net
 Program firmware into devices with device
programmers.
3.1. Install / Uninstall the MPLAB IDE
To install MPLAB IDE on your system:
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 onscreen menu to install
MPLAB IDE. If no on-screen menu appears, use
Windows Explorer to find and execute the CD-ROM
menu, menu.exe.
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.
First click on the exe file in the specified folder.
Join the Technical Community Today!
http://www.pantechsolutions.net
After clicking on the Install_xxx file then installation
preparation will start, when prompted click on next.
Join the Technical Community Today!
http://www.pantechsolutions.net
Accept License agreement and Click on next.
Select complete and press next.
Join the Technical Community Today!
http://www.pantechsolutions.net
To install this folder click next or otherwise chose
another location and press next.
Click next after accepting the license agreement.
Join the Technical Community Today!
http://www.pantechsolutions.net
Review setting before copying and if you want to
change, you can by using back button.
Here you can the status base for installing
Join the Technical Community Today!
http://www.pantechsolutions.net
Now click on finish button,
After finish you can see and chose Mplab IDE document
select.
Join the Technical Community Today!
http://www.pantechsolutions.net
We choose release note to view. Now installation is
completed. You can now open the MPLAB IDE from Start >
All Programs > Microchip > MPLAB.
Now you have successfully installed MPLAB. Now we can
start to working with MPLAB IDE.
Join the Technical Community Today!
http://www.pantechsolutions.net
Before we can use MPLAB IDE, you must install specific
compiler software.
To uninstall MPLAB IDE:
• Select Start>Settings>Control Panel to open the
Control Panel.
• Double-click on Add/Remove Programs. Find MPLAB
IDE on the list and click on it.
• Click Change/Remove to remove the program from
your system.
First we can Select Start>Settings>Control Panel.
Join the Technical Community Today!
http://www.pantechsolutions.net
Next Select the Add/Remove Programs. Find MPLAB
IDE on the list and click on it.
Click Change/Remove to remove the program from
your system.
Join the Technical Community Today!
http://www.pantechsolutions.net
4. Operation of Mplab
The Mplab screen provides you with a menu bar for
command entry, a tool bar where you can rapidly select
command buttons, and windows for source files, dialog
boxes,
and
information
displays.
Mplab
lets
you
simultaneously open and view multiple source files.
Mplab has two operating modes:
Build Mode: Allows you to translate all the
application files and to generate executable
programs. The features of the Build Mode are
described under Creating Applications.
Debug Mode: Provides you with a powerful
debugger for testing your application. The Debug
Mode is described in Testing Programs.
In both operating modes you may use the source editor
of Mplab to modify your source code. The Debug mode
adds additional windows and stores an own screen layout.
Join the Technical Community Today!
http://www.pantechsolutions.net
4.1. Setting up the Development Mode
The MPLAB IDE desktop contains the following major
elements:
1. A menu across the top line
2. A toolbar below the menu
3. A workspace in which various files, windows, and
dialogs can be displayed
4. A status bar at the bottom
T
h
Join the Technical Community Today!
http://www.pantechsolutions.net
e development mode sets which tool, if any, will execute
code. For this tutorial we will use MPLAB-SIM, the software
simulator. Later you may switch to one of the emulator
operations if you have an emulator. Operation will be
similar. “Editor Only” mode does not allow code execution,
and is mainly useful if you have not installed the simulator,
do not have an emulator, and are just creating code to
program a PIC micro microcontroller (MCU).
Select the Options > Development Mode menu item
and click the Tools tab to select the development tool and
processor for your project.
Join the Technical Community Today!
http://www.pantechsolutions.net
MPLAB IDE is a constantly evolving product, so there
may be subtle differences between what you see and the
picture here. Select MPLAB-SIM Simulator and choose the
dsPIC30F4011 from the pull down list of available
processors supported by the simulator. Click OK. The
simulator will initialize and you should see “dsPIC30F4011”
and “Sim” in the status bar on the bottom of the MPLAB IDE
desktop. You are now in simulator mode for the
dsPIC30F4011 device.
4.2. Creating a Project
The next step is to create a project using the Project
Wizard. A project is the way the files are organized to be
compiled and assembled. We will use a single assembly file
for this project and a linker script. Choose Project>Project
Wizard.
The next dialog (Step One) allows you to select the
device, which we’ve already done.
Join the Technical Community Today!
http://www.pantechsolutions.net
Now we can select Next button then the window shows
as shown in below. Then we can select the device.
1
Join the Technical Community Today!
http://www.pantechsolutions.net
4.3. Select Language Tools
Step two of the Project Wizard sets up the language
tools that are used with this project. Select Microchip C30
Tool suite in the top pull down. Then you should see C30,
MPLINK and MPLIB show up in the Tool suite Contents box.
You can click on each one to see its location. If you installed
MPLAB IDE into the default directory, the C30 compiler
executable will be:
C:\Program Files\Microchip\MPLAB C30\bin\pic30-gcc.exe
The MPLINK linker executable will be:
C:\Program Files\Microchip\MPLAB C30\bin\pic30-ld.exe
And the MPLIB librarian executable will be:
C:\Program Files\Microchip\MPLAB C30\bin\pic30-ar.exe
If these do not show up correctly, use the browse
button to set them to the proper files in the MPLAB IDE
subfolders.
Join the Technical Community Today!
http://www.pantechsolutions.net
When you are finished, click Next >.
4.4. Naming the Project
Step Three of the wizard allows you to name the
project and put it into a folder.
This sample project will be called Mplab. Using the
Browse button, place the project in a folder named Mplab.
Click Next>.
Join the Technical Community Today!
http://www.pantechsolutions.net
4.5. Adding files to Project
Step Four of the Project Wizard allows file selection for
the project. A source file has not yet been selected, so we
will use an MPLAB IDE template file. The template files are
simple files that can be used to start a project. They have
the essential sections for any source file, and contain
information that will help you write and organize your code.
Join the Technical Community Today!
http://www.pantechsolutions.net
These files are in the MPLAB IDE folder, which by
default is in the Program Files folder on the PC.
There is one template file for each Microchip PIC micro
and dsPIC device.
Choose the file named led_delay.c. If MPLAB IDE is
installed in the default location, the full path to the file will
be:
C:\Documents and Settings\Administrator\Desktop\Mplab\led_delay.c
Join the Technical Community Today!
http://www.pantechsolutions.net
Press Add>> to move the file name to the right panel,
and 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 both check boxes checked, then press Next> to finish
the Project Wizard.
The final screen of the Project Wizard is a summary
showing the selected device, the tool suite and the new
project file name.
Join the Technical Community Today!
http://www.pantechsolutions.net
After pressing the Finish button, review the Project
Window on the MPLAB IDE desktop. If the Project Window
is not open, then select View>Project.
Note: Files can be added and projects saved by using the
right mouse button in the project window. In case of error, files
Join the Technical Community Today!
http://www.pantechsolutions.net
can be manually deleted by selecting them and using the right
mouse click menu.
4.6. Building the Project
From the Project menu, we can assemble and link the
current files. They don’t have any of our code in them yet,
but this assures that the project is set up correctly.
To build the project, select either:
Join the Technical Community Today!
http://www.pantechsolutions.net
• 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.
However the mouse over icons to see pop-up text of
what they represent.
The Output window shows the result of the build
process. There should be no errors on any step.
Note:
If these do not assemble and link successfully, check
the following items and then build the project again:
Join the Technical Community Today!
http://www.pantechsolutions.net
• Check the spelling and format of the code entered in
the editor window. Make sure the new variables and the
special function registers, TRISC and PORTC, are in upper
case. If the assembler reported errors in the Output
window, double click on the error and MPLAB IDE will open
the corresponding line in the source code with a green
arrow in the left margin of the source code window.
• Check that the correct compiler (Microchip C30
Compiler) and linker for PIC micro devices are being used.
Select Project>Set Language Tool Locations. Click on the
plus boxes to expand the Microchip C30 tool suite and its
executables. Click Microchip C30 Compiler (pic30-gcc.exe)
and review their location in the display. If the location is
correct, click Cancel. If it is not, change it and then click OK.
The default search paths can be empty.
Upon a successful build, the output file generated by
the language tool will be loaded.
Join the Technical Community Today!
http://www.pantechsolutions.net
This file contains the object code that can be
programmed into a PIC micro MCU and debugging
information so that source code can be debugged and
source variables can be viewed symbolically in Watch
windows.
4.7. Testing Code with the Simulator
In order to test the code, software or hardware is
needed that will execute the PIC micro instructions. A
debug execution tool is a hardware or software tool that is
used to inspect code as it executes a program (in this case
led_delay.c). Hardware tools such as MPLAB ICE or MPLAB
ICD 2 can execute code in real devices. If hardware is not
available, the MPLAB simulator can be used to test the
code. In this tutorial we can use MPLAB SIM simulator.
The simulator is a software program that runs on the
PC to simulate the instructions of the PIC micro MCU.
Join the Technical Community Today!
http://www.pantechsolutions.net
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 PIC micro MCU to run at full speed in the actual
target application.
The status bar on the bottom of the MPLAB IDE
window should change to “MPLAB SIM”.
Additional menu items should now appear in the
Debugger menu. Additional toolbar icons should appear in
the Debug Tool Bar.
Join the Technical Community Today!
http://www.pantechsolutions.net
5. Getting Started with MPLAB SIM
MPLAB SIM is one of the debug engines that can be
used with MPLAB. The other debug engines are hardware
devices, while MPLAB SIM is a software program that runs
on your PC. MPLAB SIM provides many of the same features
as in-circuit emulators and in-circuit debuggers. The
difference is that both in-circuit emulators and in-circuit
debuggers allow the code to be run on actual silicon, and
also allow target application hardware to be functional
while being debugged.
MPLAB SIM has features to simulate hardware
interaction with other signals and devices, and since it is
running as software on the PC, it has complete information
about the internal state of the simulated chip at each
instruction. This is a little different from the hardware
debuggers because, while they are running code at full
speed, they typically cannot monitor all registers and all
memory in real time.
Join the Technical Community Today!
http://www.pantechsolutions.net
Both MPLAB SIM and the hardware debuggers can do
the traditional functions of debuggers, but due to their
differences, they can have unique features of their own.
This presentation will identify the functions and features of
MPLAB SIM.
The debugger is a part of MPLAB IDE, and whether you
are using MPLAB SIM, MPLAB ICE or MPLAB ICD 2, most
operations are exactly the same. This allows you to develop
code using the simulator, and then when your hardware is
ready, you can use a hardware debugger to further test
your code in practice without having to learn how to use a
new tool.
Debugger Menu
Run
Halt
Animate
Step Into
Step Over
Step Out Of
Reset
Toolbar Buttons Hot Key
F9
F5
F7
F8
F6
Join the Technical Community Today!
http://www.pantechsolutions.net
These are the basic debug functions:
• Reset the target, in order to restart the application.
• Execute the code so the program can be tested to
verify it functions as designed.
• Halt the code at breakpoints
• While halted at breakpoints examine and modify
memory and variables to analyze and debug the
application code.
• Single Step through code to closely inspect how it
executes. This allows the engineer to go through code
one instruction (or one C statement) at a time while
monitoring affected variables, registers and flags. Single
stepping essentially “zooms in” on code to ensure that
it operates correctly in complex and critical sections
with ranges of variable values and under various test
conditions.
Most debuggers also have additional features to help
analyze and debug the application.
Join the Technical Community Today!
http://www.pantechsolutions.net
Some of these are listed here:
• Watch points group and monitor selected variables
and memory locations into a convenient, custom
readout.
• Trace buffers capture the streams of instructions
executed and reveal the contents of changing register
values.
• A Stopwatch can time a section of code. Routines can
be optimized, and critical code timing can be accurately
measured and adjusted.
• Complex breakpoints offer a method for establishing
breakpoints or for gathering data in the trace buffer
based upon multiple conditions. Simple breakpoints
allow setting breakpoints in the source code or
anywhere in program memory. Complex breakpoints
allow getting a breakpoint on a condition such as,
– After the main routine called “Refresh Display”
executes then
Join the Technical Community Today!
http://www.pantechsolutions.net
– wait for subroutine “Read Temp” to execute. Then
– break if the variable named “Temperature” is
greater than 20.
Complex events can be constructed to count events, so
that a subroutine would have to be executed, for example,
15 times before it starts looking for a value on a pin or in a
register. This kind of breakpoint allows you to describe the
condition where your code misbehaves, to halt at a
breakpoint or to trace code when that condition occurs.
This is usually a faster way of finding bugs than simply
setting simple breakpoints and stepping through your code.
MPLAB SIM is a simulator, and as a result it has certain
characteristics that make it a unique debug engine. So the
speed of the simulation is determined by,
• How fast your PC executes,
• The complexity of the current simulation, and
• The number of other tasks executing on your PC.
Join the Technical Community Today!
http://www.pantechsolutions.net
Currently the maximum speed of MPLAB SIM is on the
order of 10 MIPS, or 10 million instructions per second. This
will be affected by how many other things are being done
by your PC, by the code the simulator is running, and by the
other tasks that the simulator is performing. The simulator
simulates the operation of,
• The core CPU and its internal registers,
• Memory, and
• Many of it peripherals.
In order to test the application on the simulator,
stimulus signals can be applied to pins and registers.
To evaluate performance, the simulator can log
changing registers to files for further analysis.
5.1. Starting Up MPLAB SIM
Select the simulator as the debug execution tool. This is
done from the Debugger>Select Tool pull down menu.
Join the Technical Community Today!
http://www.pantechsolutions.net
MPLAB SIM is selected as the debug engine from the
Debugger menu. Note the other functions on the debug
menu, such as Run, Step, and Breakpoints.
Once a debug engine is selected, the toolbar is
appended with some new icons for running, halting, single
stepping, and resetting the target.
The Status bar now shows some additional information.
• MPLAB SIM shows as the current debug engine.
Join the Technical Community Today!
http://www.pantechsolutions.net
• The simulated processor is listed, in this case the
dsPIC30F4011,
• Then the program counters,
• The W register,
• The current state of the internal CPU flags and
• The current selected file register bank.
Join the Technical Community Today!
http://www.pantechsolutions.net
The toolbar icons, the menus, or the hot keys listed on
the menus can be used to execute the debug functions.
Note that some are a little more complex, such as Reset,
which actually has four types of reset actions.
Once MPLAB SIM is established as the debug engine,
whenever a project is built, it is automatically loaded into
the simulator’s program memory to be run and tested.
Join the Technical Community Today!
http://www.pantechsolutions.net
One debug window is the source code window. This is
actually the editor window, and breakpoints can be set by
clicking on a line with the right mouse button. Single
stepping with the source code window in focus will single
step through the C source lines. Since you are in the editor,
changes can be done quickly, and the project can be rebuilt.
Join the Technical Community Today!
http://www.pantechsolutions.net
The Program Memory window shows the machine code
that will be executed by the simulator. Single stepping with
this window in focus will allow you to step through each
machine instruction.
Another window, called the Disassembly Listing
window shows high level source code interspersed with
machine code generated by each C statement.
Join the Technical Community Today!
http://www.pantechsolutions.net
You can also open up a watch window and drag the
variables from your program to them to see the contents.
In order to see if the code is operating as intended,
sending incrementing values out PORTC, watch the values
being sent to PORTC.
Below are the menu items in the Disassembly window
right mouse button menu.
Join the Technical Community Today!
http://www.pantechsolutions.net
• Set or remove a breakpoint at the currently-selected
line.
•Enable/Disable a breakpoint at the currentlyselected line.
• Run the program to the current cursor location.
• S e t P C a t C u r s o r Set the program counter (PC) to
the cursor location.
• C opy selected text to clipboard. Select text by (1)
clicking and dragging mouse over text or (2) clicking at
the beginning of text and shift-clicking at the end of
text.
• Select all text in the window.
•Output to File.
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 labeled “Add SFR” can be used
to add the Special Function Register, PORTC, into the watch.
Join the Technical Community Today!
http://www.pantechsolutions.net
Select PORTC from the list and then click Add SFR to add it
to the window.
While debugging, other windows are available to view,
• register memory,
• stack memory, and
• Non-volatile data memory areas.
5.2. Simulator Stopwatch
The Stopwatch dialog can time a section of code as it
Join the Technical Community Today!
http://www.pantechsolutions.net
run in the simulator.
The Stopwatch calculations are based upon the
instructions executed and the setting entered for the
Processor Frequency.
The processor frequency is set to 20 MHz in this
example. From the number of instruction cycles executed,
the total time is calculated.
This is the time it would take to run on a real chip.
The stopwatch has two pairs of readouts, one displays
the total simulated clock cycles and the corresponding
execution time, and the other can be zeroed out, to make a
Join the Technical Community Today!
http://www.pantechsolutions.net
measurement from one breakpoint to the next. The
stopwatch is one way to measure time in the simulator, but
there is another:
5.3. Simulator Trace Analyzer
The trace buffer records instructions when they
execute and puts a time stamp on each instruction. After
events are captured in the trace buffer, the time stamp can
be used to measure time between instructions.
The trace buffer has the advantage that it can capture
large amounts of data selectively and each instruction has a
time stamp. You can capture just the interrupt routine, for
instance, and then easily calculate the time between
interrupts and the total time each interrupt took to execute.
5.4. Simulator I/O
MPLAB SIM simulates the CPU core with the various
microcontroller program, file and data memory areas.
Join the Technical Community Today!
http://www.pantechsolutions.net
Simulation includes pin inputs and outputs as well as
many of the other peripherals.
The peripherals communicate to the application
through special function registers.
A complex Stimulus Generator simulates signals that
can be applied to the device under simulation.
The stimulus generator can send signals to pins or to
registers in the simulator.
The activity of the simulator can be sent to a Log file for
later analysis. This is done by using either the USART as a
Join the Technical Community Today!
http://www.pantechsolutions.net
communication device for inputs and outputs, or by using
the register log feature. Both stimulus and logging activity
can be driven either by execution at a specified program
counter address or by sequencing “on demand.”
“On demand” means that whenever that register is
read by an instruction, a value is read or written to a list,
then the next position in the list is queued up to be used for
the next read or write operation.
Join the Technical Community Today!
http://www.pantechsolutions.net
Did you enjoy the read?
Pantech solutions creates information packed technical
documents like this one every month. And our website is a rich
and trusted resource used by a vibrant online community of
more than 1,00,000 members from organization of all shapes
and sizes.
Join the Technical Community Today!
http://www.pantechsolutions.net
What do we sell?
Our products range from Various Microcontroller
development boards, DSP Boards, FPGA/CPLD boards,
Communication Kits, Power electronics, Basic electronics,
Robotics, Sensors, Electronic components and much more . Our
goal is to make finding the parts and information you need
easier and affordable so you can create awesome projects and
training from Basic to Cutting edge technology.
Join the Technical Community Today!
http://www.pantechsolutions.net