Getting started with MPLAB

Download Report

Transcript Getting started with MPLAB

Getting started with MPLAB

Launch MPLAB • Set Toolbar as in the next slide by clicking the leftmost icon • Open a new Source file by choosing [FILE][NEW] • Type in your program • Rules for the Microchip assembler: • Comments begin with a be UPPERCASE lowercase ; • Labels (names) begin in column 1 (Leftmost) and by style should • Instructions should not begin in column 1 and by style should be • The last directive should be end (comments can come after)

Save your file [FILE][SAVE AS] on your floppy in a subdirectory called myfiles • Rules for file naming:

Names should not be more than 8 alphanumeric characters, not beginning with a number, underscore is acceptable character • The extension should always be .asm

; e.g. example1.asm

• Make a new Project [PROJECT][NEW] • Give your project a name, normally the same as your program name with an extension of .pjt

; e.g. example1.pjt

and ensure that it is targeted to your folder in the floppy!

• If {Development Mode} window does not show MPLAB SIM then use the [Change] button to alter

• Click the [Add Node] button and pick your presaved source file, in the case below example1.asm

on your floppy

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

• 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 11 “ 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}

• Prepare for simulation by setting up a Watch window to monitor the variables, File registers • Pick menu [Window][Watch Windows][New Watch Windows] or click on the icon.

• Add the appropriate variables; NUM and SUM in this example

• To modify a value in the Watch window: • Double-click the variable; e.g. NUM • In the resulting Modify window input the datum byte in hex (or decimal) and click the [Write] button • The altered value is shown in the Watch window in red • When changes are finished click the [Close] button

• To ‘run’ your program by single stepping: • Reset the simulated PIC by either using the [DEBUG][SYSTEM RESET] menu or the Reset icon • You can single-step the system using Single-Step icon or else use the [DEBUG] menu • A coloured bar 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.

• 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 set any time and Watched variable altered for each run. In addition clicking on the top left icon of the Watch window allows variable to be added or properties altered (such as changing from hex to decimal or binary

• To ‘run’ your program by animation • First set a breakpoint by right clicking on the instruction you wish the program to stop (you can have several breakpoints and run down to each one in sequence) • Reset the system • Choose [DEBUG][RUN][ANIMATE] • 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 green icon or the [DEBUG][RUN][RUN] menu • You can use the red icon to stop the simulation at any time

• To monitor the execution time of your program using the Stopwatch • Before running your program go to the [Window] menu and pick [Stopwatch] • 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 [OPTIONS][DEVELOPMENT] menu

• For example for a 20MHz crystal:

• 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 the log to be recorded and then right click; for example:

• The trace instructions will be highlighted in green (the red breakpoint can also be seen below • Single instructions can be right-clicked and added to the trace repertoire.

• More control over the process can be had from the [DEBUG] menu

• Now run the program in the usual way.

• When finished selecting [WINDOW][TRACE MEMORY]. This brings up a text file showing: • Each instruction as it was executed • The time and number of cycles since reset • Each file altered by the instruction; e.g. below line 8 shows File 21h (the loop count) going to 08 and File 3 (STATUS) to 19h • The display below was set to [Source code display] by clicking the top left icon which gives various display options • The file can be saved and printed etc selecting [File][EXPORT TRACE MEMORY] and giving the file a name • Up to around 8,000 trace lines are possible

Using MPLAB 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 in a subdirectory called myfiles • Rules for file naming:

Names should not be more than 8 alphanumeric characters, not beginning with a number, underscore is acceptable character • The extension should always be .c

; e.g. exam16.c

• Make a new Project [PROJECT][NEW] • Give your project a name, normally the same as your program name with an extension of .pjt

; e.g. exam16.pjt

and ensure that it is targeted to your folder in the floppy!

• Change the {Language Tool} window to CCS.

• Select the .hex file in the {Project Files} window and then the [Node Properties] button.

• When the [Node Properties] window comes up (bottom left) click on the [OK] button.

• On return then click on the [Add Node] button and select the source file; for example exam16.c (bottom right).

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

• If you had a syntax error (For this example I changed i++; to j++; ), then something like below will occur: • To correct the errors then read the error description which in this case is in line 9 “ Unidentified identifier j line in the source code.

comments with a start /* ”.

• Double click on error and the cursor will move to the appropriate • Make the appropriate change(s) and reassemble {F10}.

• Many errors are not so easy to locate as this. Look out for and no end comment */

• You can use a Watch window to monitor a C program in the same way as an assembly-level program, but variables defined inside a function will have the form function.variable; e.g. main.number

. Global variables do not have a function name prefix; e.g. TENS .