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

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

Exercise Files

File Download
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 Main_Open_Project.png 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 OpenProjectButton.png.

2

Open C Source File

Lab14.png

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)

Lab14Flowchart.png

4

Debug Project

Once you finish writing the code:

Click on the Debug Project Main_Debug_Project.png button. This will build and send the program to the simulator.
Click on the Continue Debug_Continue.png button. This begins the simulation.
Click on the Halt Debug_Pause.png 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 Debug_Finish_Debugger_Session.png 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.
© 2024 Microchip Technology, Inc.
Notice: ARM and Cortex are the registered trademarks of ARM Limited in the EU and other countries.
Information contained on this site regarding device applications and the like is provided only for your convenience and may be superseded by updates. It is your responsibility to ensure that your application meets with your specifications. MICROCHIP MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORY OR OTHERWISE, RELATED TO THE INFORMATION, INCLUDING BUT NOT LIMITED TO ITS CONDITION, QUALITY, PERFORMANCE, MERCHANTABILITY OR FITNESS FOR PURPOSE. Microchip disclaims all liability arising from this information and its use. Use of Microchip devices in life support and/or safety applications is entirely at the buyer's risk, and the buyer agrees to defend, indemnify and hold harmless Microchip from any and all damages, claims, suits, or expenses resulting from such use. No licenses are conveyed, implicitly or otherwise, under any Microchip intellectual property rights.