The MPLAB system is an integrated development environment for developing programs for the Microchip PIC range of microcontrollers.
It contains the following entities:
The following text illustrates the procedures required to edit, assemble, simulate and program a PIC device within the MPLAB environment. More advanced techniques such as setting breakpoints and the trace facility are also considered.
Before continuing, if you have not already done so, follow the advice of Practical Session 0 (MPLAB Tutorial) and configure MPLAB in the manner described.
After invoking MPLAB a number of pull down menus will be observed across
the top of the screen which can be selected using the left hand mouse button.
Beneath the menu headings are a number of hotkeys which can also
be used to select functions instead of using the pull down menus once
familiarity with the package has been achieved, (no difference here from many
Windows based packages you should already be familiar with). There are several
different hotkey screens which can be selected by clicking on the
left-most hotkey (coloured red, blue and yellow).
Along the bottom of the screen is some additional information, the most important of which (at this stage) is the cell that informs the user which PIC microcontroller is the target device. If any other microcontroller other than PIC16C54 (pin identical to the PIC16C84) is shown here then it will have to be changed to PIC16C54 since this is the microcontroller used in the first section. The PIC16C54 is an older 'simplistic' device (no longer available) but it forms the core of the device that you will be using (PIC16F84), so it is used here as a simple introduction to give familiarity with the simulation.
To change the target device to PIC16C54: Go to the Options menu and select
'Development Mode'.
Select 'MPLAB-Sim Simulator ' and then select PIC16C54 using the arrow key.
When PIC16C54 has been selected, click on reset to accept.
The indicator at the bottom of the screen should now display PIC16C54. Editing
an Assembly Language Program
Programs in the MPLAB environment are assembled and simulated as projects,
but before a project can be constructed the program must first be written
using the MPLAB text editor. To invoke the text editor go to the File menu
and select New Source. The editor screen should appear. Type in the following
program:
| ; First program using MPLAB ; Add two numbers and store the result ; The first value is stored in file register (RAM) 0x09 ; The second value is stored in file register (RAM) 0x0A ; The result is stored in file register (RAM) 0x0B ; |
|||||
| list | p=16c54 | ||||
| ; ****** PROGRAM EQUATES ****** | |||||
| value1 | EQU | 0x09 | |||
| value2 | EQU | 0x0A | |||
| result | EQU | 0x0B | |||
| w | EQU | 0 | |||
| f | EQU | 1 | |||
| ; ; ****** MAIN PROGRAM ****** |
|||||
| ORG | 0x00 | ||||
| start | |||||
| MOVLW | .0 | ;Loads all zeros into w register | |||
| MOVWF | value1 | ;clear 'value1' variable | |||
| MOVWF | value2 | ;clear 'value2' variable | |||
| MOVWF | result | ;clear 'result' variable | |||
| MOVLW | .5 | ;Move the value of 5 (decimal) into w | |||
| MOVWF | value1 | ;Store in value1 | |||
| MOVLW | .3 | ;Move the value of 3 (decimal) into w | |||
| MOVWF | value2 | ;Store in value2 | |||
| ; | |||||
| MOVF | value2,w | ;Move value2 to w | |||
| ADDWF | value1,w | ;Add value1 to w | |||
| MOVWF | result | ;store result | |||
| stop | |||||
| GOTO | stop | ||||
| ; ; ****** RESET VECTOR ****** |
|||||
| ORG | 0x1ff | ;Reset Vector of 16c54 is 0x1ff | |||
| GOTO | start | ||||
| END | |||||
Save the program as firstadd.asm
Once written and saved, the program can now be copied into a project.
Go to the Project menu and select 'New Project'.
Give the Project a name e.g. firstadd.pjt
Go to the Project menu and select 'Edit Project'.
Add firstadd.asm to the project files and click OK.
To assemble the program into machine code go to the Project menu and select
'Make Project'.
MPLAB will attempt to assemble firstadd.asm into machine code. Any errors incurred during assembly will be indicated which means going back into the text editor and correcting the errors before attempting to assemble the program again. The program must obviously be reassembled after every change is made.
When the program assembles correctly - 'Success no Errors' will be displayed.
Display the program memory by clicking on the Program memory window hotkey.
(An explanation of each of the hotkeys is given at the bottom
left hand corner of the screen as you position the mouse there. This particular
hotkey is coloured blue and white) .
The program most recently assembled will be observed in program memory in both assembly language and machine code formats.
Close the program memory window.
Simulation is a very important aspect of software development and should be carried out every time a program is written or edited to ensure correct operation, before programming a device.
To simulate the program, first of all open firstadd.asm if it is not already open.
Open the File Register window by clicking on the relevant hotkey. This allows the file registers to be observed as the program is simulated.
Open the special function register by clicking on the relevant hotkey or click on the SFR icon. This allows changes of the W register, I/O ports etc. to be observed as the program is single stepped.
Now open a WATCH window for the variables you have created, by clicking on the relevant hotkey or click on the Watch icon and select the variables you wish to observe.
The properties of the watched variables can be selected to either view them in binary, decimal or hex format.
Select the desired properties associated with the 'result' variable, then add 'value1' and 'value2' variables.
Randomise all file (RAM) locations prior to simulation so that changes can be observed as the program is single stepped.
From Debug menu select Power on Reset. All file locations should turn red.
Apply a Reset to the program by clicking on the relevant hotkey or the reset icon .
A black bar appears over the 'GOTO start' line of code in the program since, after applying a reset, the program counter contains 0x1ff, the reset vector for the 16C54.
If you then step through the lines of code, you will notice the watched register values change.
Single step through the program by clicking on the Step hotkey (two
footprints) . File registers used in the program should be observed changing
value (turning red) as the program is single stepped. Try to follow and understand
the changes observed in the working register and the created variables as the program is single stepped.
To end, goto Project menu, select close and save this project.
Now you might like to try simulating a similar project using the PIC16C84 device to get a better feel for the microcontroller code simulation. You would need to change the target device for the project and could use the following code:-
| ; First program using MPLAB ; Add two numbers and store the result ; The first value is stored in file register (RAM) 0x0C ; The second value is stored in file register (RAM) 0x0D ; The result is stored in file register (RAM) 0x0E ; |
|||||
| list | p=16C84 | ||||
| ; ****** PROGRAM EQUATES ****** | |||||
| value1 | EQU | 0x0C | |||
| value2 | EQU | 0x0D | |||
| result | EQU | 0x0E | |||
| w | EQU | 0 | |||
| f | EQU | 1 | |||
| ; ; ****** MAIN PROGRAM ****** |
|||||
| ORG | 0x00 | ||||
| start | |||||
| CLRW | ;Clears the w register | ||||
| MOVWF | value1 | ;clear 'value1' variable | |||
| MOVWF | value2 | ;clear 'value2' variable | |||
| MOVWF | result | ;clear 'result' variable | |||
| MOVLW | 0x53 | ;Move 53hex (83 in decimal) into w | |||
| MOVWF | value1 | ;Store in 'value1' | |||
| MOVLW | 0x27 | ;Move 27hex (39 in decimal) into w | |||
| MOVWF | value2 | ;Store in 'value2' | |||
| ; | |||||
| ADDWF | value1,w | ;Add 'value1' to w (still containing value2) | |||
| MOVWF | result | ;store the answer in 'result' | |||
| stop | |||||
| GOTO | stop | ||||
| ; ; ****** RESET VECTOR ****** |
|||||
| ORG | 0x1ff | ;Reset Vector of 16c84 is 0x1ff | |||
| GOTO | start | ||||
| END | |||||
Save the program as 2ndadd.asm
Once written and saved, the program can now be copied into a project.
Go to the Project menu and select 'New Project'.
Give the Project a name e.g. 2ndadd.pjt
Go to the Project menu and select 'Edit Project'.
Add 2ndadd.asm to the project files and click OK.
Did you notice the removal of the redundant line of code from the first example?
You should now have a good appreciation of the benefits of simulation and an awareness of how to start developing your code within MPLAB IDE.
If you would like to repeat/reinforce this information with a video download then go to Microchip's Introduction to MPLAB web-page :-
http://techtrain.microchip.com/webseminars/ArchivedDetail.aspx?Active=46
where, at the bottom of the page you will see the options for:-
Download Recorded Presentation File (slides + audio):
Download Presentation File (slides + text) in .pdf format:
or view a streaming media version.
Or there is the MPLAB IDE getting started guide:- http://ww1.microchip.com/downloads/en/DeviceDoc/MPLAB_Getting_Started_51281g.pdf
Updated 09.11.07 ML
Powered by Google
Site Map