The purpose of this lab is to help you understand how to create projects that contain multiple source files. This kind of project structure has numerous advantages, the biggest of which is better organization of programs. It also promotes code reuse because functions that you want to use over and over again may be placed in their own file(s) and then included in any project where you want to use them.
| Windows|| Linux|| Mac OSX|
Integrated Development Environment
| Windows|| Linux|| Mac OSX|
Project and Source Files
Open the ProjectStart MPLAB® X, then click on the Open Project icon on the main toolbar
Navigate to the folder where you saved the exercise files for this class.
Click on the Lab09.X folder.Select Open Project .
Edit Source Code
This project is quite a bit different from the others we’ve worked on so far. We will be dealing with five different files, all of which will be interacting with each other. As before, Lab09.c will contain our main() function, and therefore the program will begin executing from that point. From within the main() function, we will be calling functions that reside in file1_09.c and file2_09.c. The header files associated with file1 and file2 contain the function prototypes that are needed in Lab09.c to be able to call the functions that reside in the separate files.
The files file1_09.c and file2_09.c don’t require any editing. In those files, we defined several variables and a couple functions just as we did in Lab8.c. Essentially these files look like any ordinary main file, but without a main() function (a project can only have one of those). Instead, we will be editing the header files, which provide the connection between Lab09.c and the other two C files.
Note that this lab does the exact same thing as Lab08, but the two function definitions and their associated variable definitions have been placed in separate files.
Open the file file1_09.h. Add external variable declarations to make the variables defined in file1_09.c available to any C source file that includes this header file. The variables you need to create external definitions for are:
intVariable1, intVariable2 and product.
Add a function prototype to make multiply_function() defined in file1_09.c available to any C source file that includes this header file.
Open the file file2_09.h. Add external variable declarations to make the variables defined in file2_09.c available to any C source file that includes this header file. The variables you need to create external definitions for are:
floatVariable1, floatVariable2, quotient and intQuotient.
Add a function prototype to make divide_function() defined in file2_09.c available to any C source file that includes this header file.
Once you finish writing the code:Click on the Debug Project button. This will build and send the program to the simulator.
Set up the Watches window:
Lab 9 uses the same variables as Lab08; however, in this lab they have been defined as Static Variables. MPLAB® X refers to these variables as Global Symbols. To view the Lab09 variables we must use and configure the Watches Window
- Open the Watches Window ( Windows -> Debugging -> Watches ) or (Alt + shift + 2 )
- “Right Click” in the Watches Window and select “New Watch”
- From the global symbol select intVariable1
- Repeat the above 2 steps until intVariable2, floatVarivable1, floatVariable2, quotient, intQuotient, and product have all been added to the Watches Window
Click on the Halt button. This will stop execution so that we may examine the variables and their values.
End Debug SessionErase the Watches Window by right clicking in the Watches Window and selecting Delete All.
End the Simulation Session by clicking the Finish Debugger Session button.
Close the Project.
Multi-file projects take the concept of functions a step further, by allowing further organization and separation of functionality within a program. Separate files allow you to group related functions and variables in such a way that they may be used among several different programs. Using multiple files incurs no additional overhead, so you can feel free to use them whenever they make sense.