creating compiling simulating a c program step by step...

10
1 Hi-TECH C and XC8 Creating, Compiling, and Simulating a C Program Step by Step 1. Install the MPLAB IDE that was downloaded from www.microchip.com. See detail installation instructions in handout “How to Install MPLAB IDE” 2. Start the MPLAB IDE by double clicking the icon at: 3. Create a project for the lab (refer to the Creating a C Project with the MPLAB IDE.docx file). 4. Choose File from the menu bar, then select New to open the editor window. Alternatively, you can select Open to open any previous .c file or .c template. Begin typing your code, or modifying the existing code, as necessary. Open the LAB#3.C then start the editing Change the Function, Date, & Name to be yours.

Upload: ledien

Post on 24-Mar-2018

217 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Creating Compiling Simulating a C Program Step by Step …ucdistancetraining.org/preworkshpfiles/MPLABIDE_XC8/Creating... · 1 Hi-TECH C and XC8 Creating, Compiling, and Simulating

1

Hi-TECH C and XC8 Creating, Compiling, and Simulating a C Program Step by Step

1. Install the MPLAB IDE that was downloaded from www.microchip.com. See detail

installation instructions in handout “How to Install MPLAB IDE” 2. Start the MPLAB IDE by double

clicking the icon at:

3. Create a project for the lab (refer to the Creating a C Project with the MPLAB

IDE.docx file). 4. Choose File from the menu bar, then select New to open the editor window.

Alternatively, you can select Open to open any previous .c file or .c template. Begin typing your code, or modifying the existing code, as necessary.

Open the LAB#3.C then start the editing

Change the Function, Date, & Name to be

yours.

Page 2: Creating Compiling Simulating a C Program Step by Step …ucdistancetraining.org/preworkshpfiles/MPLABIDE_XC8/Creating... · 1 Hi-TECH C and XC8 Creating, Compiling, and Simulating

Page 2 of 10

5. Save the file soon after creating it. Select File, then Save As, type the desired file name (LAB#3.C in this example), and click Save. Note: Make sure the file is saved in the same folder as the project files.

Type the exact name with extension: LAB#3.C and

make sure the Save as type is C Source Files

Page 3: Creating Compiling Simulating a C Program Step by Step …ucdistancetraining.org/preworkshpfiles/MPLABIDE_XC8/Creating... · 1 Hi-TECH C and XC8 Creating, Compiling, and Simulating

Page 3 of 10

6. Make sure your code begins in the PROGRAM area. Continue editing/creating your

source code.

7. After you have finished editing, save the program file. Select File then Save to save the source code into the folder you specified in step #5.

Page 4: Creating Compiling Simulating a C Program Step by Step …ucdistancetraining.org/preworkshpfiles/MPLABIDE_XC8/Creating... · 1 Hi-TECH C and XC8 Creating, Compiling, and Simulating

Page 4 of 10

Compiling (Building) the C Program

1. From the menu bar, select Project then Build to build the code files. Alternatively, select the Build icon.

2. The Output window will display the compilation results. Note: If there are any errors, double-click on the error message and the cursor will point to the line with the error in the source code. Correct the error(s) then rebuild.

“Build”

Page 5: Creating Compiling Simulating a C Program Step by Step …ucdistancetraining.org/preworkshpfiles/MPLABIDE_XC8/Creating... · 1 Hi-TECH C and XC8 Creating, Compiling, and Simulating

Page 5 of 10

3. The detail of the Output window is shown in the below graphic. If Build Successful is displayed, the program is ready to be simulated.

Page 6: Creating Compiling Simulating a C Program Step by Step …ucdistancetraining.org/preworkshpfiles/MPLABIDE_XC8/Creating... · 1 Hi-TECH C and XC8 Creating, Compiling, and Simulating

Page 6 of 10

Simulating the C Program

1. On the menu bar, select Debugger, then Select Tool, then MPLAB SIM.

2. After the selection of the simulator tool, there will be eight new options available in the menu.

Run: the source code simulation is run all the way through to the end.

Halt: stops program execution.

Page 7: Creating Compiling Simulating a C Program Step by Step …ucdistancetraining.org/preworkshpfiles/MPLABIDE_XC8/Creating... · 1 Hi-TECH C and XC8 Creating, Compiling, and Simulating

Page 7 of 10

Animate: run the source code from the beginning and control the speed of animation using the slider.

Step Into: single step through the code one instruction at a time.

Step Over: allows stepping over CALL routines.

Step Out: step out of a subroutine. Stops execution at the instruction returned to by the subroutine.

Reset: stops execution and returns to the beginning Program Memory.

Breakpoints: set breakpoints to halt program execution during simulation.

3. Select the Step Into icon. The simulator will open the assembler source file (xxx.as)

created during compilation. A green arrow to the left of the source code will show the next step to be executed. Continue to single step (Step Into) through the code until reaching the ljmp _main instruction.

4. On the next single step, the simulator will open the C source code window (xxx.c)

and the green arrow will point to the next instruction to be executed.

Page 8: Creating Compiling Simulating a C Program Step by Step …ucdistancetraining.org/preworkshpfiles/MPLABIDE_XC8/Creating... · 1 Hi-TECH C and XC8 Creating, Compiling, and Simulating

Page 8 of 10

5. On the menu bar, choose View then select File Registers to open a window

displaying the contents of the data memory.

Page 9: Creating Compiling Simulating a C Program Step by Step …ucdistancetraining.org/preworkshpfiles/MPLABIDE_XC8/Creating... · 1 Hi-TECH C and XC8 Creating, Compiling, and Simulating

Page 9 of 10

6. The windows can be resized and dragged to other locations on the screen, as

desired. Alternatively, select Window on the menu bar then select either Tile Vertically or Tile Horizontally to rearrange the windows into equal-size windows. The project window (xxx.mcw) can be minimized to free up space.

Page 10: Creating Compiling Simulating a C Program Step by Step …ucdistancetraining.org/preworkshpfiles/MPLABIDE_XC8/Creating... · 1 Hi-TECH C and XC8 Creating, Compiling, and Simulating

Page 10 of 10

The .c and .as source code windows are where you observe which instructions have been executed.

The File Registers window is where the Special Function Register (SFR) and General Purpose Register (GPR) data are displayed in hexadecimal format. Register).

7. Exam those windows in detail and compare the changes in the registers with the

expected changes. Modify the source code, as required.

Project Window