Lab 1

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

Introduction to MPLAB and the C 18 Compiler

Name: ID: Date:

Prepared by: Document Version: Date:

Feisal Mohammed 0.1 9/29/2005

1 INTRODUCTION TO MPLAB C 18 Compiler


This section shows how to set up a project using MPLAB C 18, build and step through the source code using MPLAB SIM simulator In order to keep your changes outside of the compiler folder, copy the folder C:\mcc18\example\getting_started\example1 to some location on the desktop. This folder contains a demo program to try out. First select from the menu Project->New. Enter the name of the new project and the example1 folder which you have just placed on the desktop. A project tree is now displayed, with the branch being each type of project file, similar to the one used in VC++.

The targeted processor (processor being used) must be selected before continuing. This can be done be selecting from the main menu Configure>Select Device. Select the appropriate device, in this case, the 18F452, then press OK.

2 SELECTING PROJECT SETTINGS


To select MPLAB C 18 and the MPLINK linker, select Project>select language toolsuite. To use the language tools that include MPLAB C 18 and the MPLINK linker, select Microchip C 18 Toolsuite as the Active Toolsuite. Select MPLINK Object Linker and MPLAB C 18 Compiler and ensure that the paths that lead to mplink.exe and mcc18.exe respectively are where the compiler is located, and then press OK The next step is to set the command-line options for the compiler and linker. Choose Project>Build Options>Project. Enter the paths of the header file and library subdirectories of the MPLAB C 18 installation directory on the General tab. Use the browse button to designate the paths for the include files (*.h), the library path and the linker script path.
Prepared by: Document Version: Date: Feisal Mohammed 0.1 9/29/2005

Output Directory: - This is the final destination for files that result only from a complete build of the project i.e. The *.cod, *.cof and *.hex files. Leave Output Directory blank. This results in the output file (*.cof) being placed in the project directory. Intermediates Directory: - This is where the object files produced by the compiler will be placed. Leave this directory blank as well, resulting in the object file (*.o) being placed in the same directory as the source file. Linker-Script Path: - The MPLINK linker will search the directory specified in this path for the linker scripts. This entry can also be left blank, it will be added later in the project tree

3 SELECT COMPILER AND LINKER SETTINGS


The various command-line options that are passed to the compiler and linker are set in the MPLAB C18 and MPLINK Linker tabs, respectively,

By default, when the MPLINK linker is run from the MPLAB IDE, it will not generate a map (example1.map) file. To change this, select Generate map file on the MPLINK Linker tab then press OK.
Prepared by: Document Version: Date: Feisal Mohammed 0.1 9/29/2005

4 ADDING FILES TO PROJECT


SOURCE FILES: The C source file must be added to the project. Right click on Source Files in the project window. Select Add Files. Browse through the directories and locate example1.c adding it to the project. The source file should appear in the project tree. HEADER FILES: The header file is specified in the C source file; therefore no file needs to be added to Header Files in the project tree. A header file may be added to Header Files for convenient viewing of the file, but it is only required that the header file be included in the C source code to build the project. OBJECT AND LIBRARY FILES:
Prepared by: Document Version: Date: Feisal Mohammed 0.1 9/29/2005

The required startup module, standard library and processor library are specified in the linker script, and so no file needs to be added to Object Files or Library Files in the project tree. If there were other object files or library files to link in the project, they would be added under these branches. LINKER SCRIPTS: The MPLINK linker requires a linker script to be specified. Right click on Linker Scripts in the project window, and select Add Files. Use the linker script 18f452.lkr in the lkr subdirectory of the MPLAB C18 installation directory. This script is for the PIC18F452 device. BUILDING THE PROJECT: Select Project>Build All or ctrl F10 to compile and link the project. If there are any error or warning messages, they will appear in the output window.

5 DEBUGGING USING MPLAB SIM SIMULATOR


First, from the main menu select Debugger>Select Tool>MPLAB SIM. With the MPLAB SIM Simulator, breakpoints can be set in the source code to observe the value of variables with a watch window.

Open the source file by double clicking on it in the project tree. In the source file, place the cursor over the line where the breakpoint is desired to be set, and right click. Select Set Breakpoint. Or double clicking on the line will also place and remove the break point. The red dot in the gutter along the side of the source window indicates that the breakpoint has been set and is enabled. Breakpoints can be set and removed this way.

Prepared by: Document Version: Date:

Feisal Mohammed 0.1 9/29/2005

5.1 WATCH WINDOWS To open a watch window on the variable counter, select View>Watch. Select the data to be observed from the menu next to Add symbol, then press Add symbol. Then press the run button on the toolbar to run the program

The program halts just before the statement at the breakpoint is executed. The green arrow in the gutter of the source window points to the next statement to be executed. What is the value of counter in the watch window? Click run again to continue the program and write down the value of counter after this action.

Different data can be observed by choosing Watch 1-4 on the tab. Different registers can also be viewed by using Add SFR to select. 5.2 STEPPING To step through the source code one statement at a time, use Step Into on the toolbar. As each statement executes, the green arrow in the gutter of the source window moves to the next statement to be executed. The Animate button halted by using the Halt button . runs the program automatically and can be

Prepared by: Document Version: Date:

Feisal Mohammed 0.1 9/29/2005

When first using the step into button a window opens what is the name of the window and what does it contain?

What do you observe after continuous stepping?

The reset button allows the user to start from the beginning of the code, write down the line of code that the arrow points too when this button is used and describe what does this piece of code does.

5.3 MAP AND LISTING FILES The map file (example1.map) and the list file (example1.lst) are present in the project directory and can be view using File>Open, then browse to the project directory allocating the file type as *.map or *.lst, to locate the file. These files provide additional information that may be useful debugging, such as details of allocation of variables and the correspondence between machine code and source code.

Prepared by: Document Version: Date:

Feisal Mohammed 0.1 9/29/2005

Open the listing file for the project *.lst and copy the part of the code that is in the main under the headings Address, value, Disassembly and Source. Open also the *.map file and copy the line which shows the counter, the address and location and also the storage type.

6 DEBUGGING WITH MPLAB ICD2


The MPLAB ICD 2 can be used to actually program the device and step through the application. To do this, the project must be rebuilt with a linker script designed for use with the MPLAB ICD 2. In the project window, right click on the file 18f452.lkr under Linker Scripts, and click Remove. Add the linker script file 18f452i.lkr from the lkr subdirectory of the MPLAB C18 installation directory under Linker Scripts in the project tree. This linker script allocates memory for resources used by the MPLAB ICD 2. The i in the files name indicates this linker script is for use with the MPLAB ICD 2. Rebuild the project by selecting Project>Build All. To use the MPLAB ICD 2, select Debugger>Select Tool>MPLAB ICD 2. In this session, we do not have an ICD connected, but the information is included in this section for completeness.

Prepared by: Document Version: Date:

Feisal Mohammed 0.1 9/29/2005

6.1 PROGRAMMING THE DEVICE To program the device select Debugger>Program. The programming window should show the programming operation succeeded.

7 STIMULUS CONTROLLER
This option is useful for debugging, in that it substitutes the actual circuit with a virtual one. If an input is expected from PIC to carry out an operation it can be done with out programming it. Simply select Debugger>Stimulus Controller> New Scenario, the dialog box as show in the diagram should appear. Choose from the drop down box the desired pin to be simulated, in action choose the value setting for the pin.

Prepared by: Document Version: Date:

Feisal Mohammed 0.1 9/29/2005

If a continuous cyclic action is desired, select pulse, and the width of the pulse as well as the units that it should take. During the running of the program the pin/s can be fired using the fire option. On simulation the output would give an indication in the MPLAB SIM tab of the status of the pin.

Configure RB1 as an input and use stimulus to simulate the high and low setting of a switch that controls the lighting of a light emitting diode on RB0. Show the code for this operation below.

Observe the output window and write down its contents when using stimulus.

Prepared by: Document Version: Date:

Feisal Mohammed 0.1 9/29/2005

Prepared by: Document Version: Date:

Feisal Mohammed 0.1 9/29/2005

You might also like