Lab Exercise 16: Bit Fields

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

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 a previous lab 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.

Reference Materials

Exercise Files

Procedure

Open the Project
Start MPLAB® X IDE, then click on the Open Project MPLAB X IDE Open Project Icon 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  open projects button.

Back to Top


Debug Project
Click on the Debug Project MPLAB X IDE Debug Project Icon button. This will build and send the program to the simulator.

Back to Top


Set a Breakpoint

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:

Lab16 Breakpoint being set

Back to Top


Continue

Click on the Continue MPLAB X IDE Continue Icon button to run until the breakpoint.

use the continue button

Back to Top


Set a Breakpoint

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

examining breakpoint values in the watch window

Back to Top


Execute One Line of Code

Click the Step Into button MPLAB X IDE Step Into Icon 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;

Back to Top


Click the Step Into button MPLAB X IDE Step Into Icon

watch window after single step

Note: 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;

Click the Step Into button MPLAB X IDE Step Into Icon

Variable window output after the step into

Note: 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;

Click the Step Into button MPLAB X IDE Step Into Icon

shows changes in variable window after another single step

Note: 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;

End Debug Session
End the Simulation Session by clicking the Finish Debugger Session MPLAB X IDE Finish Debugger Session Icon button.
Close the Project.

Back to Top


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.

Back to Top