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
Open the Project
Start MPLAB® X IDE, then click on the Open ProjectNavigate to the folder where you saved the exercise files for this class.
Click on the LAB16.X folder.
Select Open Project
.
Debug Project
Click on the Debug ProjectSet a Breakpoint
Open the Variables Window ( Alt + shift + 1) and expand bitByte and bitField as shown here.
Click the Step Into buttonbitByte.fullByte = 0x55;
End Debug Session
End the Simulation Session by clicking the Finish Debugger SessionClose 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.





