Getting started with MPLAB

Download Report

Transcript Getting started with MPLAB

Getting started with MPLAB7
• Launch MPLAB
• Open a new Source file by choosing [FILE][NEW] or clicking on the
icon on the toolbar
• Type in your program
• Rules for the Microchip assembler:
• Comments begin with a ;
• Labels (names) begin in column 1 (Leftmost) and by style should
be UPPERCASE
• Instructions should not begin in column 1 and by style should be
lowercase
• The last directive should be end (comments can come after)
• Save your file [FILE][SAVE AS] on your floppy or memory stick in a
folder called Myfiles
• Rules for file naming:
• Names should be not more than 32 alphanumeric characters,
nor beginning with a number. An underscore is an acceptable
character
• The extension should always be .asm; e.g. example1.asm
17 July, 2015
University of Ulster
1
17 July, 2015
University of Ulster
2
•
•
•
•
Make a new Project [Project][Project Wizard]
Hit the <Next> button (left)
Step One: Pick the device you are going to use as the target
Hit the <Next> button (right)
17 July, 2015
University of Ulster
3
• Step Two: Select your Language Toolsuite. In the diagram below
left, this is the Microchip PIC ASseMbler
• Hit the <Next> button (left)
• Step Three: Give your project a name and where it is going to
reside This is usually in the same place as you saved your source
file; a:/myfiles in our example (right)
• Hit the <Next> button
17 July, 2015
University of Ulster
4
• Step Four: Find your source file, select it and hit the <Add>>>
button (left)
• Hit the <Next> button
• A summary of your project is given to allow you to back up and
make any alterations (right)
•Hit the <Finish> button to complete your project setup file
example1.mcp
17 July, 2015
University of Ulster
5
• Assemble your code by picking [PROJECT][MAKE] or hit {F10}
• If there are NO syntax errors then you should see something like this:
17 July, 2015
University of Ulster
6
• If you had a syntax error (for this example I changed addlw to addwl),
then something like below will occur:
• To correct the errors then read the error description which in
this
case is in line 13 “Illegal character”.
• Double click on error and the cursor will move to the appropriate
line in the source code. The clue is the warning “Found label
after column 1. (addwl)” As it did not recognise the illegal
instruction
addwl it thought it was a label in the wrong place.
• Make the appropriate change(s) and reassemble {F10}
• Note that sometimes the Output window will list Warnings. These
are not necessarily errors, but nevertheless should be closely
inspected
17 July, 2015
University of Ulster
7
• Prepare to simulate your program by turning on MPLAB’s
simulator as shown left
• Pick menu [Debugger]
• Pick submenu [MPLAB SIM]
• Once this is done an extra toolbar appears
• From left to right:
• Run at full simulated speed {F9}
• Stop running {F5}
• Animate at two steps per second.
This can be changed
using the [Debugger][Settings][Animation] menu to alter the
rate
17 July, 2015
University of Ulster
8
• Step Into simulates a single instruction each time this icon
is clicked. This includes subroutines {F7}
• Step Over simulates a single instruction but when this is a
call to a subroutine the latter executes at full simulation
speed until returning to the caller {F8}
•Step out allows the operator to single step through a
subroutine and then when satisfied, to run through to
completion at full simulation rate until returned to the caller
• Reset the software simulator to the instruction at location
h’000’ in the Program store, and various peripheral devices.
{F6}
This does not clear the GPR Files, but this can be done via
the [Debugger][Clear Memory][GPRs]
17 July, 2015
University of Ulster
9
• In order to see what is going on in a simulation you need to set
up a Watch window to monitor the program variables and memory
• Pick menu [View][Watch]
• Add the appropriate variables, NUM and SUM in this example,
using the right-hand drop down menu and clicking the <Add
Symbol> button
17 July, 2015
University of Ulster
10
• To modify a value in the Watch window:
• Double-click the variable’s value; e.g. NUM (left)
• In the resulting Modify window input the datum byte and
left click the mouse
• The altered value is shown in the Watch window in red
(right)
• Special-Purpose Registers, e.g. PORTA, can be monitored in
the same way using the left-hand drop menu and <Add SFR>
select button
17 July, 2015
University of Ulster
11
• To change the display properties of a variable in the Watch
window:
• Single-click anywhere in the variable’s row
• Right-click anywhere inside the Watch window and pick
Properties
• The Watch properties tab allows you to change the
watched variable in various number bases and sizes (e.g. as
a 2-byte decimal).
•More than one copy of a variable can be added to the Watch
window and viewed simultaneously in different number
bases; e.g. binary and decimal
17 July, 2015
University of Ulster
12
• To ‘run’ your program by single stepping:
• Reset the simulated PIC by either using the
[Debugger][Reset][Processor reset] menu entry or the Reset
icon
or {F6}
• You can single-step the system using the Single-Step icon
or else use the [Debugger] menu
• A green arrow will move down the source code as you step
through the program and the variables will alter in the Watch
window (variable values can be changed on the fly in the
Watch window, as described previously)
• The Working register, Program Counter and values of the
three flags are shown lower case for 0 and Upper case for 1
• The program can be reset any time and Watched variables
altered for each run. In addition right clicking in the Watch
window will bring up the Properties menu and allow variables
to be added or properties altered (such as changing from hex
to decimal or binary)
17 July, 2015
University of Ulster
13
17 July, 2015
University of Ulster
14
• To ‘run’ your program by animation
• First set a breakpoint by right clicking on the instruction you
wish the program to stop at
• Click on Set Breakpoint in the resulting menu (bottom left) A
red stop symbol will appear (bottom right)
• Do this as many times as you like to set up several
breakpoints. You can run down to each one in sequence
•You can remove a breakpoint by right clicking and in the
resulting menu clicking on Remove Breakpoint
• Reset the system
• Click on the Animate icon
• The program will execute a few instructions a second and
stop when the breakpoint is reached.
•To ‘run’ at high speed then:
• Set up a breakpoint
• Use the Go icon
• You can use the Stop icon
to halt the simulation at any time
To ‘run’ at high speed then:
• Set up a breakpoint
• Use the Go icon
• You can use the Stop icon
to halt the simulation at any time
• In programs which take longer than about 18ms to execute, the
simulation will halt prematurely, as the Watchdog timer is also
simulated and will time-out unless your program continually clears it
using the clrwdt instruction. In order to avoid this, the Watchdog
timer should be disabled by activating the simulated fuse pattern
• Pick the [Configure][Configuration Bits] menu entry (bottom
left)
• From the Configuration Bits (Fuse) menu (bottom right) click
on the Watchdog Timer Setting and pick Off
17 July, 2015
University of Ulster
16
• To monitor the execution time of your program using the Stopwatch
• Before running your program go to the [Debugger] menu and
pick [StopWatch] (left)
• The Stopwatch window shown below appears.
• Cycles shows the number of internal cycles (¼ crystal
frequency) as the program progresses
• Time is the actual time, which depends on the xtal
frequency
• Normally both the above are zeroed on reset
• If the program is single-stepped or animated then the
Stopwatch is updated on the fly, as is the Watch window
• If the run is at high speed then the final outcome is
displayed when the breakpoint is reached; see next slide
• Note: The simulated crystal frequency may be changed
from the [Debugger][Settings] menu
17 July, 2015
University of Ulster
17
• For example for a 20MHz crystal:
17 July, 2015
University of Ulster
18
• Using the Trace memory facility
For serious debugging it is useful to have a log of the state of
the program as the simulation progresses.
• Highlight all instructions you wish to be logged and then
right click
• In the resulting menu click on [Add Filter-in Trace]
• The instructions to be traced will be indicated with
symbols
thus:
17 July, 2015
University of Ulster
19
•Single instructions can be right-clicked and added to the
trace repertoire
•Now run the program in the usual way
• When finished selecting [View][Simulator Trace]. This brings up
the Trace Memory window showing in each line numbered back
from the breakpoint:
• Each instruction: e.g. in line -8 this is decfsz 0x21,f
• The number of cycles since reset: e.g. h’40’ (decimal 64)
• Address of the Source data (SA): e.g. the File contents being
decremented is in File h’21’
• The value of this data (SD): e.g. 02
• The destination address (DA): e.g. File h’21’
• The value of this destination data (DD): e.g. 01 (it has been
decremented)
• The contents of the Trace Memory can be saved and printed
etc by right clicking in the window picking [Output to File],
and giving the file a name
• Up to around 32,000 trace lines are possible
17 July, 2015
University of Ulster
20
17 July, 2015
University of Ulster
21
• During simulation you may want to monitor stimuli from the
outside world which will drive the port pins when the processor is
executing in a hardware environment. For example, to see how
software responds to a pulse at a Parallel port. To do this you
must use the Asynchronous Stimulus dialog. In the following
example a value of the variable COUNT is to be incremented each
time pin RB0 (that is the Interrupt pin) is pulsed.
•Open the Stimulus Controller window [Debugger][Stimulus
Controller][New Scenario] (bottom left)
•The resulting window (bottom right) allows you to fill in for
any set of pins what action will be taken when the operator
clicks on the appropriate firing button
• Click on the Pin/SFR tab and select the appropriate pin
(bottom left). You can repeat for each row
•Repeat for the Action column and select the appropriate
action (bottom mid)
•Under the width and units columns enter the width or the
event (1 bottom right)
•Under the Units column enter the duration units (cycles, ns,
µs, ms or s)
• Optionally enter a comment for each row
•The next slide shows the situation where the RB0/INT pin has
been fired six times (COUNT = 0x06)
17 July, 2015
University of Ulster
23
17 July, 2015
University of Ulster
24
Using MPLAB7 with the CCS C compiler
• Launch MPLAB
• Open a new Source file by choosing [FILE][NEW]
• Type in your program
• Save your file [FILE][SAVE AS] on your floppy/memory stick in a
subfolder called myfiles
• Rules for file naming:
• Names should not be more than 32 alphanumeric characters,
not beginning with a number, underscore is acceptable
character
• The extension should always be .c; e.g. example16.c
17 July, 2015
University of Ulster
25
• Make a new Project [Project][Project Wizard] in the normal
way
• At step 2 select the CCS compiler as the toolsuite; see below
left
• When complete you should have a summary similar to bottom
right
17 July, 2015
University of Ulster
26
• Compile your code by picking [Project][Compile] or hit {F10}
• If there are NO syntax errors then you should see something like this:
17 July, 2015
University of Ulster
27
• If you had a syntax error (For this example I changed count++; to
cuont++;), then something like below will occur:
• If your Output window does not appear then ensure that
[View][Output] is ticked.
• To correct the errors then read the error description which in this
case is in line 9 “Unidentified identifier cuont”.
• Double click on this error line and the cursor will move to the
appropriate line in the source code.
• Make the appropriate change(s) and compile {F10}.
• Many errors are not so easy to locate as this. Look out for
comments with a start /* and no closing */!!!
17 July, 2015
University of Ulster
28
• You can use a Watch window to monitor a C program in the same
way as an assembly-level program, as shown below
• Variables, such as n, which are outside their scope at the
execution line, will be annotated as such in the Watch window
17 July, 2015
University of Ulster
29
Using the PICSTART+ programmer
•
•
•
Once you have written and assembled/compiled your code and verified its
action using the simulator, you need to blast the code into a real PIC
MCU’s Program store and run the code in real hardware.
The following description assumes that the PICSTART+ is plugged into
serial port COM1 (or COM2) of a PC, not necessarily the one you created
the software on.
If MPLAB7 has not been set up to recognise the PICSTART+ then follow
the procedure below to intialise the setup.
• Ensure that the Power LED on the PICSTART+ is illuminated.
• From the [Programmer] menu pick [Select Programmer][1 PICSTART
Plus]
17 July, 2015
University of Ulster
30
• Again, from the [Programmer] menu pick [Enable
Programmer].
17 July, 2015
University of Ulster
31
• Entering [Programmer][Settings…] allows you to alter some
properties of the PICSTART+. Once the configuration has
been set, it will be remembered for future sessions.
• As shown (bottom left) you can change the serial COM
port.
• If you choose Auto select Memory range (bottom right)
then only locations in your Program store that need
programming will be blasted.
32
• If you have configured the Configuration fuses in your source
program (using __config – see below – or #fuses) then the fuse
options will be automatically set up. If you wish to set these
up manually (not recommended) then use the [Configure]
menu and select [Configuration Bits…] as shown below and
click on the setting(s) you wish to alter.
17 July, 2015
University of Ulster
33
• If your program has been developed on the computer
connected to your PICSTART+ then ignore the next points!
• If your program had no syntax errors then a .hex file will
have been generated; this file contains all the machine
code information needed for the blasting process. A
typical .hex file looks like this:
:020000040000FA
:100000008601051483167E308500860183128518CB
:10001000072820088600051000000514851C0E28FE
:020020000728AF
:02400E00613F10
:00000001FF
• Bring your .hex file over to the
PICSTART+ PC and use [File][Import] to
load your file into the machine.
17 July, 2015
University of Ulster
34
• To blast your code into the PIC MCU use the
[Programmer][Program] option (there are other options, such
as [Verify] to check the contents of the PIC MCU’s Program
store with the code in the .hex file).
•The Output window (bottom right) gives the address range
blasted and whether the blast was successful. If the Output
window does not display, then go to [View][Output].