3.2.1 Register Initialization Using Bit Masks and Group Configuration Masks

This subsection provides the recommended way to configure the ADC CTRLA register using bit masks and group configuration masks.

ADC0.CTRLA = ADC_ENABLE_bm         /* Enable the ADC */
           | ADC_CONVMODE_bm       /* Select Differential Conversion mode */
           | ADC_RESSEL_10BIT_gc;  /* 10-bit conversion */

Note the absence of the bitwise OR (‘|’) on the register side of the assignment. In most cases, device and peripheral routines are written in this way.

The ADC_RESSEL_enum enumeration contains the group configuration mask presented below.

ADC_RESSEL_10BIT_gc = (0x01<<2),  /* 10-bit mode */
CAUTION: The above initialization of the register must be done in a single line of C-code. Writing as follows, the group configuration in the second line will clear the bit set in the first line.
ADC0.CTRLA = ADC_ENABLE_bm;
ADC0.CTRLA = ADC_CONVMODE_bm;
ADC0.CTRLA = ADC_RESSEL_10BIT_gc;

The correct way of writing this code using three code lines is presented below.

ADC0.CTRLA = ADC_ENABLE_bm;
ADC0.CTRLA |= ADC_CONVMODE_bm;
ADC0.CTRLA |= ADC_RESSEL_10BIT_gc;
Note: Bit masks can only set bits in a single line of code, so any configurations which require bits to be set to ‘0’ can be left out since they are correctly configured by their Reset value.