Lab Exercise 14: Structures

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

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.

Reference Materials

Exercise Files

Procedure


Open the Project

Start MPLAB X IDE, then click on the Open Project Main Open Project icon on the main toolbarNavigate to the folder where you saved the exercise files for this class.

Click on the Lab14.X folder.

Select Open Project Open Project Button .


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

open the lab source files

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


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)

flow chart for the lab


Debug Project

Once you finish writing the code:

Click on the Debug Project Main Debug Project button. This will build and send the program to the simulator.
Click on the Continue Debug Continue button. This begins the simulation.
Click on the Halt Debug Pause 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),

Back to top

Results

results in the variable window

The program calculates the power in three different circuits, and then determines which circuit has the highest power. After running the program, you should see the following results shown here in the Variables window.
Note that MPLAB® X IDE presents structure variables such that you can expand and collapse them to either show or hide the individual members of the structure.

Code Analysis

(NOTE: Line numbers correspond to those in the provided solution file.)

Lines 13-16
The first of two different structures is defined here. The power structure is used to hold the minimum or maximum voltage and current measurements for a circuit. There will be two variables of type power declared for each circuit—one to hold the maximum values, the other to hold the minimum values.

Lines 18-21
This second structure is actually a structure of structures. It is designed to hold two variables of type power. So, each range structure can hold the maximum and minimum power measurements for a circuit. Since we are creating members of type power within the range structure, the power structure needed to be declared first.

Lines 43-46
These four variables will be used to store the results of the power calculations we will perform later in the program. They are all ordinary long integer type variables.

Lines 52-63
These lines declare variables of the two structure types. The first group of variables are all of type power, and we will use them in the first part to show how the power can be calculated using structures with members of C’s built-in data types. The second group are all of type range, and will be used in the second part of the program to show how power can be calculated using a structure with members that are structures themselves.

Lines 65-66
Here, we declare two pointers. One points to a structure variable of type power, and the other points to a structure variable of type range. These will be used later in the program to first point to the particular circuit’s power structure with the maximum value. It will then be used to copy those structures’ values into the two maximum power structure variables PMax and PMaxRange.

Lines 79-93
At this point of the program, we are inside the main loop and need to initialize the variables that we will be using. In a real application, we probably would obtain these values by sampling the circuits with an analog to digital converter. However, since we are working in the simulator, and have no hardware connected, we will simply make up some values and assign them to the variables.

Line 106
STEP 1 requires that you calculate the difference between minimum and maximum power in circuit 1 using the variables of type power. This can be done just like it is done for circuits 2 and 3 on lines 107-108:

powerDiff1 = (PMax1.v * PMax1.i) - (PMin1.v * PMin1.i);

The variables PMax1 and PMin1 each have two members: v and i, which represent the voltage and current measurements respectively. So, to calculate the maximum power, we simply need to multiply the v and i members of PMax1 together. The minimum power is calculated in the same way. Then, to get the difference, we subtract the minimum calculation from the maximum calculation. The result is then stored in powerDiff1.

Lines 114-129
This block of code determines which of the three circuits have the greatest difference between maximum and minimum power. It first checks to see which of powerDiff1 and powerDiff2 is greater, and assigns the larger one to maxPowerDiff. Next, it checks to see which of maxPowerDiff and powerDiff3 is greater. If maxPowerDiff is greater, nothing further is done. If powerDiff3 is greater, it is assigned to maxPowerDiff.
Also, in each of the above steps, the address of the structure containing the maximum power of the one that has the greatest difference is assigned to the pointer pPower.

Line 135
The pointer pPower that we initialized above is now used to copy the maximum power value into the structure variable PMax. When using a pointer in this fashion, the values of all of the members of the structure it points to are copied into the variable on the left of the assignment operator.

Lines 146-161
Just like lines 79-93 above, we need to initialize the variables we plan on using as if we were actually obtaining these from an analog to digital converter.

Line 173
STEP 2 calculates the same thing as line 106 above, but this time it uses the structure of structures variables. The concept is the same, but the syntax is now a bit different because we need to reference two levels of structure members:
powerDiff1 = (PRange1.max.v * PRange1.max.i) - (PRange1.min.v * PRange1.min.i);

Since the range of power is stored in PRange1 as two power type members, we need to add an extra level of structure member references to our calculation. For example, PRange1.max.v refers to the voltage member of the structure variable max, which itself is a member of the structure PRange1. In total, PRange1 stores four values.

Lines 181-196
This block determines which circuit has the greatest power difference, just as was done on lines 114-129.

Line 203
This line does essentially the same thing as line 135.

End Debug Session

End the Simulation Session by clicking the Finish Debugger Session Debug Finish Debugger Session button. Close the Project.

Back to top

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.

Back to top