Lab Exercise 17: Unions


This lab is designed to help you to understand how to work with unions. The code is very short and performs no practical function, but it will enable you to work with a union variable and observe how it makes use of data memory when values are written to its members.

Note that you will be using the simulator a bit differently this time. Rather than simply running and stopping your code to see the results, you will be setting breakpoints at three points in your code, and stopping at each of them to observe the data in the watch window.

Software Tools

Tool About Installers
Windows Linux Mac OSX
Integrated Development Environment
C Compiler

Exercise Files

File Download
Windows Linux Mac OSX
Project and Source Files



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 Lab17.X folder.

Select Open Project OpenProjectButton.png.


Open C Source File


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

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


Edit Source File

Set breakpoints as instructed on lines 73 and 82 by clicking on the line number in the left column.

Edit the code from lines 96 to 104 according to the comments.


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 which will stop at line 73.

Open the Variables window (Shift + Alt + 1) and expand the variables as below.



Click on the Step Over button Debug_Step_Over.png several times.

Observe the changes in the Variable Window after each time an instruction is executed. You should notice when one item is changed all the items in the union are affected.



End Debug Session

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

Close the Project.


  • Unions allow the same memory location to be viewed and manipulated as different data types.
  • They make it possible to store different variable types in the same memory location(s).
© 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.