2.3.1 Bit Masks and Bit Position Masks
A bit mask is used both when setting and clearing individual bits. A bit group mask is mainly used when clearing multiple bits in a bit field. For example, the bit fields, bit names, bit positions, and bit masks of the CTRLD register of ADC0 are shown in Table 2-1.
Bit Field | INITDLY[2:0] | - | SAMPDLY[3:0] | |||||
Bit Name | INITDLY2 | INITDLY1 | INITDLY0 | ASDV | SAMPDLY3 | SAMPDLY2 | SAMPDLY1 | SAMPDLY0 |
Bit Position | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
Bit Mask | 0x80 | 0x40 | 0x20 | 0x10 | 0x08 | 0x04 | 0x02 | 0x01 |
Since the bit names need to be unique for the compiler to handle them, all bits are prefixed with the module type they belong to. In many cases, the module type name is abbreviated. For all bit definitions related to the Timer/Counter Type A module, the bit names are prefixed by TCA_.
To differentiate between bit masks and bit positions, a suffix is also appended. For a
bit mask, the suffix is _bm
, and for a bit position it is
_bp
. The name of the bit mask for the INITDLY0 bit is thus
ADC_INITDLY0_bm, and the name of the bit position is ADC_INITDLY0_bp. Additionally, the
header file provides definitions for group positions. The suffix for a group position is
_gp
, and the name of the INITDLY group position mask, for example,
is ADC_INITDLY_gp. The code below shows the definitions of the INITDLY bit masks, bit
positions, and group positions, as they are available in the device header file.
#define ADC_INITDLY_gp 5 /* Initial Delay Selection group position */ #define ADC_INITDLY0_bm (1<<5) /* Initial Delay Selection bit 0 mask */ #define ADC_INITDLY0_bp 5 /* Initial Delay Selection bit 0 position */ #define ADC_INITDLY1_bm (1<<6) /* Initial Delay Selection bit 1 mask */ #define ADC_INITDLY1_bp 6 /* Initial Delay Selection bit 1 position */ #define ADC_INITDLY2_bm (1<<7) /* Initial Delay Selection bit 2 mask */ #define ADC_INITDLY2_bp 7 /* Initial Delay Selection bit 2 position */
A naming convention example for the INITDLY0 bit mask is presented in Figure 2-1.