Lab Exercise 16: Bit Fields

 Objective

This demo illustrates the use of bit fields. There is no code for you to write. All you need to do is build and run the project and observe the results.

The code itself combines what we learned in Lab 16 about unions, with the concept of bit fields to create a variable that will allow us to access it as a full byte, or as individual bits.

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

Select Open Project OpenProjectButton.png.

2

Debug Project

Click on the Debug Project Main_Debug_Project.png button. This will build and send the program to the simulator.

3

Set a Breakpoint

Lab16Breakpoint.png

Set a breakpoint on line 47 by clicking on the line number in the gray margin. (Alternatively, right-click on the line and select “Toggle Line Breakpoint”). You should then see a small red square replace the line number:

4

Continue

Lab16Continue.png
Click on the Continue Debug_Continue.png button to run until the breakpoint.

5

Set a Breakpoint

Lab16Step5.png

Open the Variables Window ( Alt + shift + 1) and expand bitByte and bitField as shown here.

Click the Step Into button Debug_Step_Into.png to execute line 47. Line 47 writes a value of 0x55 to the fullByte member of the variable bitByte. Note that the bitField members also changed appropriately to reflect the new value of 0x55 = 0b01010101.

bitByte.fullByte = 0x55;

6

Click the Step Into button Debug_Step_Into.png
Line48.png

Line 48 has just been executed, where we wrote a value of 0 to the member bit0 of bitField, which itself is a member of the union bitByte. Therefore, when we changed the individual bit, the fullByte member also changed to reflect the new value.

bitByte.bitField.bit0 = 0;

7

Click the Step Into button Debug_Step_Into.png
Line49.png

Line 49 has just been executed, where we wrote a value of 0 to the member bit2 of bitField, which itself is a member of the union bitByte. Therefore, when we changed the individual bit, the fullByte member also changed to reflect the new value.

bitByte.bitField.bit2 = 0;

8

Click the Step Into button Debug_Step_Into.png
Line50.png

Line 50 has just been executed, where we wrote a value of 1 to the member bit7 of bitField, which itself is a member of the union bitByte. Therefore, when we changed the individual bit, the fullByte member also changed to reflect the new value.

bitByte.bitField.bit7 = 1;

9

End Debug Session

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

Close the Project.

 Conclusions

Bit fields allow us to efficiently use individual bits for Boolean values or as flags/semaphores. On the various PIC architectures, setting and clearing a bit field variable in C will make use of the very efficient bit set and bit clear instructions in assembly language. However, other operations may generate more code than would be necessary if you were working with a full 16-bit integer type variable. So, bit fields can be invaluable in some circumstances, but they should be used with care so that excess code will not be generated.

© 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.