Вы находитесь на странице: 1из 30

APPENDIX F: TUTORIAL

FOR ASSEMBLING
AND DEBUGGING
A PIC18F ASSEMBLY
LANGUAGE PROGRAM
USING THE MPLAB
Assembling PIC18F assembly language program using MPLAB

First, download the latest versions of the MPLAB assembler and C18 compiler from the
Microchip website www.microchip.com. After installing and downloading the program,
you will see the following icon on your desktop:

MPLAB IDE v8.50.lnk

Double click (right) on the MPLAB icon and wait until you see the following screen:

407
408 Microcontroller Theory and Applications with the PIC18F

Next, click on ‘Project’ and then ‘Project Wizard’; the following screen will appear:
Appendix F: Tutorial 409

Click Next; the following screen shot will be displayed:

Select the device PIC18F4321, hit Next, and wait; the following will be displayed:
410 Microcontroller Theory and Applications with the PIC18F

In the ‘Active Toolsuite’, select ‘Microchip MPASM Toolsuite’, and click Next; the
following will be displayed:
Appendix F: Tutorial 411

Select a location where all project contents will be placed. For this example, the folder
will be placed on the desktop (arbitrarily chosen). Go to the desktop directory, make a new
folder, and name the folder. In order to do this, click on ‘Browse’, and select desktop:
412 Microcontroller Theory and Applications with the PIC18F

Next, create a new folder by clicking on the icon (second yellow icon from right on top
row) or by right clicking on the mouse on the above window; see the following screen:

Next, click on ‘New’ to see the following:


Appendix F: Tutorial 413

Click on folder, name it ‘sum’ (arbitrarily chosen name), and see the following :

Next, type in a file name such as addition (arbitrarily chosen name), and see the following
on the screen:
414 Microcontroller Theory and Applications with the PIC18F

Next, click on Save; the following screen will appear:

Click on Next, and see the following:


Appendix F: Tutorial 415

Click on Next, and see the following:


416 Microcontroller Theory and Applications with the PIC18F

Click on Finish, and see the following:


Appendix F: Tutorial 417

Click on File, and then New to see the following:


418 Microcontroller Theory and Applications with the PIC18F
Type in the program you want to assemble. The following addition program is entered:

INCLUDE <P18F4321.INC>
SUM EQU 0x50
ORG 0x100
MOVLW 0x02
ADDLW 0x05
MOVWF SUM
HERE BRA HERE
END

After entering the program, see the following:

Next, click on File, and Save as, and see the following:
Appendix F: Tutorial 419

Make sure you scroll up to desktop, and then click on sum (the folder that was created
before), and see the following:
420 Microcontroller Theory and Applications with the PIC18F
Next, double click (left) on sum to see the following:

Delete Untitled, and enter the same file name ‘addition’ with .asm extension as File name.
Click on save, and see the following screen shot (notice the display changes color):
Appendix F: Tutorial 421

Next, highlight by clicking on the top (blue) section of addition.asm.mcp, and see the
following:
422 Microcontroller Theory and Applications with the PIC18F
Right click on Source Files to see the following:

Click on Add files to see the following:


Appendix F: Tutorial 423

Click on addition.asm to see the following screen shot:


424 Microcontroller Theory and Applications with the PIC18F

Click Open to see the following:


Appendix F: Tutorial 425

Note that addition.asm is listed under Source Files. Next, click on Project and then build
all (or only the ‘Build All’ icon, second icon on top right of the Debug toolbar), and see
the following:

Next, click on Absolute to see the following:


426 Microcontroller Theory and Applications with the PIC18F

This means that the assembling the program is successful. Next the result will be verified
using the debugger.
Click on Debugger, Select Tool, and then MPLAB SIM to see the following display:
Appendix F: Tutorial 427

Click on MPLAB SIM to see the following:


428 Microcontroller Theory and Applications with the PIC18F
Click on View, toolbars, and Debug to see the following display with Debug toolbar:

Next, click on View, and then watch to see the following:


Appendix F: Tutorial 429

On the Watch list, you can now include WREG and SUM to monitor their contents. For
example, to add WREG, scroll down to WREG by using the arrow beside ADCON0, and
then click on Add SFR to see the following display:
430 Microcontroller Theory and Applications with the PIC18F

Next, scroll down using arrow beside Add Symbol, select SUM, and then click on Add
Symbol to see the following display:
Appendix F: Tutorial 431
Note that SUM with address 0x50 along with contents is displayed.

In order to enter breakpoint for MOVLW 0x02, right click beside MOVLW to see:

Click on set breakpoint to see the following:


432 Microcontroller Theory and Applications with the PIC18F

B in red means breakpoint is inserted for MOVLW0x02. Similarly, breakpoints for


ADDLW 0x05 and MOVWF SUM can be inserted, and the following will be displayed:
Appendix F: Tutorial 433
Next, locate the Debug Toolbar. If, for some reason, Debug toolbar is missing, go to view,
select Toolbars, click on Debug, and see the following:

Click on the reset (first icon from right on the Debug toolbar), move the cursor to left of
MOVLW 0x02, click on run (green arrow on left on the Debug toolbar) on Debug toolbar,
and see that WREG is loaded with 0x02:
434 Microcontroller Theory and Applications with the PIC18F

Next, click on run to execute ADDLW 0x05, and see that the result of addition0x07 is
loaded into WREG as follows:
Appendix F: Tutorial 435
Finally, click on Animate (double green arrow on the Debug tool) to execute MOVWF
SUM to see that the result 0x07 is stored in SUM (address 0x50) as follows:

The debugging is now complete.


436 Microcontroller Theory and Applications with the PIC18F

Вам также может понравиться