Lab Exercise 14: Structures

Objective

This lab helps to illustrate the use of structures in C. The code is a bit more complex than previous programs, but it shows how structures can simplify what might otherwise be very complicated code. In this code, we perform circuit power calculations using two methods. The first uses simple structures while the second uses a structure of structures. You will also see how pointers to structures may be used to copy an entire structure from one variable to another.

Software Tools

Installation
Instructions
Windows Linux Mac OSX
MPLAB® X
Integrated Development Environment
MPLAB® XC16
C Compiler

Exercise Files

Installation
Instructions
Windows Linux Mac OSX
Project and Source Files

Procedure

1

Open the Project

Start MPLAB® X IDE, 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 Lab14.X folder.

Select Open Project .

2

Open C Source File

Open the Lab14.c source file from the project tree by double clicking on its icon.

This will bring up Lab14.c in a window to edit

3

Edit Source File

STEP 1:
Calculate the difference between the minimum and maximum power in circuit 1 using the individual power structures (i.e. the variables PMax1 and PMin1).
Algebraically, we want to compute:
Pdiff = (Vmax * Imax) - (Vmin * Imin)
(HINT: Look at the lines below if you are having trouble)

STEP 2:
Calculate the difference between the minimum and maximum power in circuit 1 using the structure of structures (i.e. the variable PRange1).
Algebraically, we want to compute:
Pdiff = (Vmax * Imax) - (Vmin * Imin)
(HINT: Look at the lines below if you are having trouble)

4

Debug Project

Once you finish writing the code:

Click on the Debug Project button. This will build and send the program to the simulator.
Click on the Continue button. This begins the simulation.
Click on the Halt button. This will stop execution so that we may examine the variables and their values.

Open the Variables Window with either Window -> Debugging -> Variables or ( Alt + Shift + 1)

Results

5

End Debug Session

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

Close the Project.

Conclusions

• Structures make it possible to associate related variables of possibly differing types under the same name.
• Structure members (using the dot notation) may be used anywhere an ordinary variable would be used.
• Pointers to structures make it possible to copy one entire structure to another very easily.