Bit Field Masks

Many functions are controlled by a bit field. For example, ADCRS[2:0] and ADMD[2:0] in the ADCON2 register are grouped bits. The value of the bits in a field selects a specific configuration.

When changing bits in a bit field, it is not enough to set the bits for the desired configuration. It is also required to clear the bits from the old configuration before assigning a new value. To facilitate this, a bit field mask is defined.

The field masks are predefined in the device header file. For example, the field mask for the ADMD bit field is defined below.

#define _ADCON2_ADMD_MASK                                   0x7

The naming convention adopted for the predefined bit field masks in the header file is presented in the Figure 2-3 with an example for the ADMD bit field in the ADCON2 register.

Figure 1. Naming Convention of Bit Field Masks

The bits from a bit field can be accessed as individual bits. To differentiate between these bits, a suffix (index of each bit in the bit field) is appended to the bit field name. The masks for the bits in a bit field are defined below.

#define _ADCON2_ADMD0_MASK                                  0x1
#define _ADCON2_ADMD1_MASK                                  0x2
#define _ADCON2_ADMD2_MASK                                  0x4

For further details on bit fields, consult Microchip Developer - Bit Fields.