Lab Exercise 9: Multi-File Projects

Last modified by Microchip on 2023/11/09 09:21

Objective

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.

Reference Materials

Exercise Files

Procedure

Open the Project

Start MPLAB® X IDE, then click on the Open Project Open Main Project button 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 Open Project Button


Open C Source Files and Header Files

Lab 9 files in project file tree

Open the header files and the source files from the project tree by double-clicking them.

Some of these files are edited in the remainder of this lab.


Edit Source Code

Lab 9 File Tree

This project is a bit different from the others you’ve worked on so far. You are dealing with five different files, all of which will be interacting with each other. As before, Lab09.c contains the main() function and therefore the program begins executing from that point. From within the main() function, you need to call 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 separate files.
The files file1_09.c and file2_09.c don’t require any editing. In those files, several variables and a couple of functions are defined just as they are 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, you will be editing the header files, which provide the connection between Lab09.c and the other two C files.

This lab does the same thing as Lab 8, but the two function definitions and their associated variable definitions have been placed in separate files.

STEP 1a:
Open 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.

STEP 1b:
Add a function prototype to make multiply_function() defined in file1_09.c available to any C source file that includes this header file.

STEP 2a:
Open 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.

STEP 2b:
Add a function prototype to make divide_function() defined in file2_09.c available to any C source file that includes this header file.

Flow diagram for the code used in this lab


Debug Project

Once you finish writing the code:

Click on the Debug Project Debug Main Project button button. This builds and sends the program to the simulator. Set up the Watches window:

Lab 9 uses the same variables as Lab 8; however, in this lab, they have been defined as static variables. MPLAB X IDE refers to these variables as Global Symbols. To view the Lab 9 variables you must use and configure the Watches window.

  • Open the Watches window by going to Windows > Debugging > Watches or pressing Alt + shift + 2.
  • Right-click within 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 Continue Debug Continue button button. This begins the simulation.
Click on the Halt Debug Pause button button. This stops execution so that you can examine the variables and their values.


Results

The results for Lab 9 are very similar to those of Lab 8. In this lab, all the variables were defined outside of a function ( i.e., static variables). The only way they can be displayed by the IDE is to use the Watches window.

Watches Window displaying results of Lab 9


End Debug Session

Erase the Watches window by right-clicking in the Watches window and selecting Delete All.

End the Simulation Session by clicking the Finish Debugger Session Finish Debugger Session button button.

Close the Project.


Analysis

Line numbers correspond to those in the provided solution file.

File1_09.h Lines 7-9
STEP 1a:
Writes external declarations for the variables defined in file1_09.c. In that file, there are three variables:

int intVariable1 = 0;
int intVariable2 = 0;
int product = 0;

In order to make them available to other source files, they must be declared as extern where they are used. The easiest way to provide this is to put the extern declarations into a header file that may be included in any source file that uses these variables. All you need to do is duplicate the variable declarations from file1_09.c and put the extern keyword in front of them (NOTE: you cannot initialize an extern variable declaration — only the actual definition may use an initializer):

extern int intVariable1;
extern int intVariable2;
extern int product;

File1_09.h Line 20
STEP 1b:
Provides a function prototype to make multiply_function() available to other files. This function prototype is identical to the one you created in Lab 8:

int multiply_function(int x, int y);

File2_09.h Lines 7-10
STEP 2a:
Similar to step 1a, but this time makes the variables in file2_09.c available to other files. The variables in file2_09.c are:

float floatVariable1 = 0;
float floatVariable2 = 0;
float quotient = 0;
int intQuotient = 0;

And just like step 1a, you only need to add the extern keyword in front of them when they are added to the header file:

extern float floatVariable1;
extern float floatVariable2;
extern float quotient;
extern int intQuotient;

File2_09.h Line 21
STEP 2b:
Makes divide_function() available to other files by adding a function prototype here. Again, this is just like the one you created in Lab 8.

float divide_function(float x, float y );

Conclusions

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.