2.3.2 Bit Field Masks (Group Masks)

Many functions such as clock selection for timers, prescaler selection for converters, or filter selection for the configurable custom logic are configured by a field of bits, referred to as a bit field or a bit group. The value of the bits in a group selects a specific configuration.

The masks for configuring a bit field are referred to as a bit group masks, or group configuration masks.

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 make this easy, a bit group mask is defined. The group mask uses the same name as the bits in the bit field and is suffixed _gm.

The code below shows how the ADC prescaler group mask is defined in the header file.

#define ADC_PRESC_gm  0x07  /* Clock the prescaler group mask */

The naming convention is presented in Figure 2-2.

Figure 2-2. Naming Convention of Group Masks

The bit group mask is primarily intended to clear the old configuration of a bit field before writing a new value. The code below shows how this can be done. The code will clear the PRESC bit field in the CTRLC register of the ADC0 module. This construction does not set a configuration. It only sets all the prescaler configuration bits. This is an advantage because there is no need to use all the bit masks to reset a specific configuration; they only need a group mask for this operation. The group mask will typically be used in conjunction with a group configuration mask to clear a particular configuration.

ADC0.CTRLC &= ~(ADC_PRESC_gm); /* Clearing the prescaler bit field using a group mask */