+ All Categories
Home > Documents > 1 December, 2005 University of Ulster 2 - Katzenkatzen.me.uk/Books/Documents/mplab7.pdf · • Step...

1 December, 2005 University of Ulster 2 - Katzenkatzen.me.uk/Books/Documents/mplab7.pdf · • Step...

Date post: 17-Mar-2018
Category:
Upload: phamkiet
View: 213 times
Download: 1 times
Share this document with a friend
35
1 December, 2005 University of Ulster 1 Getting started with MPLAB7 Launch MPLAB • Open a new Source file by choosing [F ILE][N EW] 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 [F ILE][SA VE 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
Transcript
Page 1: 1 December, 2005 University of Ulster 2 - Katzenkatzen.me.uk/Books/Documents/mplab7.pdf · • Step One: Pick the device you are going to use as the target • Hit the

1 December, 2005 University of Ulster 1

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

Page 2: 1 December, 2005 University of Ulster 2 - Katzenkatzen.me.uk/Books/Documents/mplab7.pdf · • Step One: Pick the device you are going to use as the target • Hit the

1 December, 2005 University of Ulster 2

Page 3: 1 December, 2005 University of Ulster 2 - Katzenkatzen.me.uk/Books/Documents/mplab7.pdf · • Step One: Pick the device you are going to use as the target • Hit the

1 December, 2005 University of Ulster 3

• 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)

Page 4: 1 December, 2005 University of Ulster 2 - Katzenkatzen.me.uk/Books/Documents/mplab7.pdf · • Step One: Pick the device you are going to use as the target • Hit the

1 December, 2005 University of Ulster 4

• 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

Page 5: 1 December, 2005 University of Ulster 2 - Katzenkatzen.me.uk/Books/Documents/mplab7.pdf · • Step One: Pick the device you are going to use as the target • Hit the

1 December, 2005 University of Ulster 5

• 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

Page 6: 1 December, 2005 University of Ulster 2 - Katzenkatzen.me.uk/Books/Documents/mplab7.pdf · • Step One: Pick the device you are going to use as the target • Hit the

1 December, 2005 University of Ulster 6

• Assemble your code by picking [PROJECT][MAKE] or hit {F10} • If there are NO syntax errors then you should see something like this:

Page 7: 1 December, 2005 University of Ulster 2 - Katzenkatzen.me.uk/Books/Documents/mplab7.pdf · • Step One: Pick the device you are going to use as the target • Hit the

1 December, 2005 University of Ulster 7

• 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 instructionaddwl 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

Page 8: 1 December, 2005 University of Ulster 2 - Katzenkatzen.me.uk/Books/Documents/mplab7.pdf · • Step One: Pick the device you are going to use as the target • Hit the

1 December, 2005 University of Ulster 8

• Prepare to simulate your program by turning on MPLAB’ssimulator 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

Page 9: 1 December, 2005 University of Ulster 2 - Katzenkatzen.me.uk/Books/Documents/mplab7.pdf · • Step One: Pick the device you are going to use as the target • Hit the

1 December, 2005 University of Ulster 9

• 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]

Page 10: 1 December, 2005 University of Ulster 2 - Katzenkatzen.me.uk/Books/Documents/mplab7.pdf · • Step One: Pick the device you are going to use as the target • Hit the

1 December, 2005 University of Ulster 10

• 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

Page 11: 1 December, 2005 University of Ulster 2 - Katzenkatzen.me.uk/Books/Documents/mplab7.pdf · • Step One: Pick the device you are going to use as the target • Hit the

1 December, 2005 University of Ulster 11

• 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

Page 12: 1 December, 2005 University of Ulster 2 - Katzenkatzen.me.uk/Books/Documents/mplab7.pdf · • Step One: Pick the device you are going to use as the target • Hit the

1 December, 2005 University of Ulster 12

• 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

Page 13: 1 December, 2005 University of Ulster 2 - Katzenkatzen.me.uk/Books/Documents/mplab7.pdf · • Step One: Pick the device you are going to use as the target • Hit the

1 December, 2005 University of Ulster 13

• 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)

Page 14: 1 December, 2005 University of Ulster 2 - Katzenkatzen.me.uk/Books/Documents/mplab7.pdf · • Step One: Pick the device you are going to use as the target • Hit the

1 December, 2005 University of Ulster 14

Page 15: 1 December, 2005 University of Ulster 2 - Katzenkatzen.me.uk/Books/Documents/mplab7.pdf · • Step One: Pick the device you are going to use as the target • Hit the

• 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

Page 16: 1 December, 2005 University of Ulster 2 - Katzenkatzen.me.uk/Books/Documents/mplab7.pdf · • Step One: Pick the device you are going to use as the target • Hit the

1 December, 2005 University of Ulster 16

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

Page 17: 1 December, 2005 University of Ulster 2 - Katzenkatzen.me.uk/Books/Documents/mplab7.pdf · • Step One: Pick the device you are going to use as the target • Hit the

1 December, 2005 University of Ulster 17

• 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 xtalfrequency• 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

Page 18: 1 December, 2005 University of Ulster 2 - Katzenkatzen.me.uk/Books/Documents/mplab7.pdf · • Step One: Pick the device you are going to use as the target • Hit the

1 December, 2005 University of Ulster 18

• For example for a 20MHz crystal:

Page 19: 1 December, 2005 University of Ulster 2 - Katzenkatzen.me.uk/Books/Documents/mplab7.pdf · • Step One: Pick the device you are going to use as the target • Hit the

1 December, 2005 University of Ulster 19

• Using the Trace memory facilityFor 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 symbolsthus:

Page 20: 1 December, 2005 University of Ulster 2 - Katzenkatzen.me.uk/Books/Documents/mplab7.pdf · • Step One: Pick the device you are going to use as the target • Hit the

1 December, 2005 University of Ulster 20

•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

Page 21: 1 December, 2005 University of Ulster 2 - Katzenkatzen.me.uk/Books/Documents/mplab7.pdf · • Step One: Pick the device you are going to use as the target • Hit the

1 December, 2005 University of Ulster 21

Page 22: 1 December, 2005 University of Ulster 2 - Katzenkatzen.me.uk/Books/Documents/mplab7.pdf · • Step One: Pick the device you are going to use as the target • Hit the

• 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 youmust 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][StimulusController][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

Page 23: 1 December, 2005 University of Ulster 2 - Katzenkatzen.me.uk/Books/Documents/mplab7.pdf · • Step One: Pick the device you are going to use as the target • Hit the

1 December, 2005 University of Ulster 23

• 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)

Page 24: 1 December, 2005 University of Ulster 2 - Katzenkatzen.me.uk/Books/Documents/mplab7.pdf · • Step One: Pick the device you are going to use as the target • Hit the

1 December, 2005 University of Ulster 24

Page 25: 1 December, 2005 University of Ulster 2 - Katzenkatzen.me.uk/Books/Documents/mplab7.pdf · • Step One: Pick the device you are going to use as the target • Hit the

1 December, 2005 University of Ulster 25

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

Page 26: 1 December, 2005 University of Ulster 2 - Katzenkatzen.me.uk/Books/Documents/mplab7.pdf · • Step One: Pick the device you are going to use as the target • Hit the

1 December, 2005 University of Ulster 26

• 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

Page 27: 1 December, 2005 University of Ulster 2 - Katzenkatzen.me.uk/Books/Documents/mplab7.pdf · • Step One: Pick the device you are going to use as the target • Hit the

1 December, 2005 University of Ulster 27

• Compile your code by picking [Project][Compile] or hit {F10} • If there are NO syntax errors then you should see something like this:

Page 28: 1 December, 2005 University of Ulster 2 - Katzenkatzen.me.uk/Books/Documents/mplab7.pdf · • Step One: Pick the device you are going to use as the target • Hit the

1 December, 2005 University of Ulster 28

• 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 theappropriate 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 */!!!

Page 29: 1 December, 2005 University of Ulster 2 - Katzenkatzen.me.uk/Books/Documents/mplab7.pdf · • Step One: Pick the device you are going to use as the target • Hit the

1 December, 2005 University of Ulster 29

• 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

Page 30: 1 December, 2005 University of Ulster 2 - Katzenkatzen.me.uk/Books/Documents/mplab7.pdf · • Step One: Pick the device you are going to use as the target • Hit the

1 December, 2005 University of Ulster 30

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]

Page 31: 1 December, 2005 University of Ulster 2 - Katzenkatzen.me.uk/Books/Documents/mplab7.pdf · • Step One: Pick the device you are going to use as the target • Hit the

1 December, 2005 University of Ulster 31

• Again, from the [Programmer] menu pick [Enable Programmer].

Page 32: 1 December, 2005 University of Ulster 2 - Katzenkatzen.me.uk/Books/Documents/mplab7.pdf · • Step One: Pick the device you are going to use as the target • Hit the

32

• 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.

Page 33: 1 December, 2005 University of Ulster 2 - Katzenkatzen.me.uk/Books/Documents/mplab7.pdf · • Step One: Pick the device you are going to use as the target • Hit the

1 December, 2005 University of Ulster 33

• 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.

Page 34: 1 December, 2005 University of Ulster 2 - Katzenkatzen.me.uk/Books/Documents/mplab7.pdf · • Step One: Pick the device you are going to use as the target • Hit the

1 December, 2005 University of Ulster 34

• 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.

Page 35: 1 December, 2005 University of Ulster 2 - Katzenkatzen.me.uk/Books/Documents/mplab7.pdf · • Step One: Pick the device you are going to use as the target • Hit the

• 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].


Recommended