MPLAB Tutorial
MPLAB Tutorial
18-Jul-2008
Tutorial Overview
In order to create code that is executable by the target PICmicro MCU, source files need to be put into a project and then the code is built into executable code using selected language tools (assemblers, compilers, linkers, etc.). In MPLAB IDE, the project manager controls this process. All projects will have these basic steps: Select Device. The capabilities of MPLAB IDE vary according to which device is selected. Device selection should be done before doing anything else on a project. Create Code. Then source code will be written to the file. Create Project. MPLAB Project Wizard will be used to Create a Project. Select Language Tools. In the Project Wizard the language tools will be selected. For this tutorial, the built-in assembler will be used Put Files in Project. Only one file will be put into the project, a source file. Build Project. The project will be built - causing our source files to be assembled into machine code that can run on the selected PICmicro MCU. Test Code with Simulator. And finally, the code will be tested with the simulator. The Project Wizard will guide us through most of these steps and will make this series of tasks easy.
Select Device
To show menu selections in this document, the menu item from the top row in MPLAB IDE will be shown after the menu name like this MenuName>MenuItem. So to choose the Select Device entry in the Configure menu, it would be written as Configure>Select Device. Choose Configure>Select Device.
Figure: Configure>Select Device
Page 1 of 13
MPLAB TUTORIAL
18-Jul-2008
The "lights" indicate which MPLAB IDE components support this device. A green light indicates full support. A yellow light indicates minimal support for an upcoming part that might not be fully supported in this release by the particular MPLAB IDE component. Usage of components with a yellow light instead of a green light is often intended for early adopters of new parts who need quick support and understand that some operations or functions may not be available. A red light indicates no support for this device. Support may be forthcoming or inappropriate for the tool, e.g., PICkit1 devices cannot be supported on MPLAB ICE 2000.
Page 2 of 13
MPLAB TUTORIAL
18-Jul-2008
Once you have completed entering the code, select File>Save and save the file as Tutor1.asm. You may save it to any directory; you will move it, if necessary, into the project directory in the next step.
Create Project
Next, we'll create a project using the Project Wizard. A project is the way your files are organized to be compiled and assembled. We will use a single assembly file for this project. Choose the Project>Project Wizard. From the Welcome dialog, click on Next> to advance in the Project Wizard.
Lecturer: Ayuniza Ahmad
Page 3 of 13
MPLAB TUTORIAL
18-Jul-2008
If these do not show up correctly, use the browse button to set them to the proper files in the MPLAB IDE subfolders.
Figure: Project Wizard - Select Language Tools
Page 4 of 13
MPLAB TUTORIAL
18-Jul-2008
Step Four of the Project Wizard allows us to select files for the project. Choose the file named tutor1.asm .The full path to the file will be: C:\Program Files\Microchip\project\tutor1.asm
Page 5 of 13
MPLAB TUTORIAL
18-Jul-2008
Press the Add>> button to move the file name to the right panel, and click on the check box at the start of the line with the file name to enable this file to be copied to our project directory. Make sure that your dialog looks like the picture above, with check box checked, then press the Next> button to finish the Project Wizard. The final screen of the Project Wizard is a summary showing the selected device, the toolsuite, and the new project file name. After pressing the Finish button, look at the Project Window on the MPLAB IDE desktop. It should look like this. If the Project Window is not open, select View>Project.
Figure: Project Window
Files can be added and projects saved by using the right mouse button in the project window. In case of error, files can be manually deleted by selecting them and using the right mouse click menu.
Page 6 of 13
MPLAB TUTORIAL
18-Jul-2008
This program has two (2) errors, both at line 18. The first error is symbol not previously defined (F) and second is illegal character (,). You may have other errors as well. Using the mouse, double click on the error message. This will bring the cursor to the line in the source code that contains the error. Change MOVWF 0x60,F,BANKED to MOVWF 0x60,BANKED. (Recall movwf f,a ). Use the Output window to help find the errors, and repair any other bugs in your source code. Reassemble by executing the Project>Build All menu function. This process may take a couple of iterations. To build the project, select either: Project>Build All Right-click on the project name in the project window and select Build All Click the Build All icon on the Project toolbar. However the mouse over icons to see pop-up text of what they represent. The Output window shows the result of the build process. When youve fixed all errors in the source code, the Output window will display BUILD SUCCEEDED.
Figure: output window Build Succeeded
You now have a complete project that can be executed using the simulator.
Lecturer: Ayuniza Ahmad
Page 7 of 13
MPLAB TUTORIAL
18-Jul-2008
Upon a successful build, the output file generated by the language tool will be loaded. This file contains the object code that can be programmed into a PICmicro MCU and debugging information so that source code can be debugged and source variables can be viewed symbolically in Watch windows.
Page 8 of 13
MPLAB TUTORIAL
18-Jul-2008
Next we select Debugger>Reset>Processor Reset and a green arrow shows us where program will begin.
To single step through the application program, select Debugger>Step Into. This will execute the currently indicated line of code and move the arrow to the next line of code to be executed. There are shortcuts for these commonly used functions in the Debug Tool Bar.
Page 9 of 13
MPLAB TUTORIAL
18-Jul-2008
Now press the Step Into icon or select Debugger>Step Into to single step to our code at Start.
Figure: DEbug>Step Into
Page 10 of 13
MPLAB TUTORIAL
18-Jul-2008
Select View>Watch to bring up an empty Watch Window. There are two pull downs on the top of the Watch Window. The one on the left labeled "Add SFR" can be used to add the Special Function Register, WREG, into the watch. Select WREG from the list and then click Add SFR to add it to the window.
Sample data
The pull down on the right, allows us to add symbols from our program. Use that pull down to add the WREG variable into the Watch Window. Select WREG from the list and then click Add Symbol to add it to the window. You also can add any register that you want. Set a breakpoint by putting the cursor on the line and clicking the right mouse button.
Page 11 of 13
MPLAB TUTORIAL
18-Jul-2008
Select Set Breakpoint from the context menu. A red "B" will show on the line.
Figure: Editor Window - Set Breakpoint
Select Debugger>Run to run the application. A text message "Running..." will briefly appear on the status bar before the application halts at this first breakpoint.
Page 12 of 13
MPLAB TUTORIAL
18-Jul-2008
result
Activate Bank #3
Note: if you want to open the existing project, just go to menu project>open.
Page 13 of 13