3.1.2 Set, Clear and Read Register Bits Using Bit Positions

An alternative way to set, clear and read register bits is by using bit positions, also provided by the device header file.

Access the registers using the structure declaration

To set a bit using a bit position macro and access the registers using the structure declaration, the following code example can be used.

ADC0.CTRLA |= (1 << ADC_ENABLE_bp); /* Enable the ADC peripheral */

To clear the bit using a bit position macro, the following example is provided.

ADC0.CTRLA &= ~(1 << ADC_ENABLE_bp); /* Disable the ADC peripheral */

To test if a bit is set, the following code can be used.

if(ADC0.INTFLAGS & (1 << ADC_RESRDY_bp)) /* Check if the ADC result is ready */
{
    /* Insert some instructions here */
}

To test if a bit is clear, and execute instructions while it remains clear, the following code can be used. In this case, the instructions inside this loop will be executed until the ADC result is ready.

while(!(ADC0.INTFLAGS & (1 << ADC_RESRDY_bp)))
{
    /* Insert some instructions here */
}

Access the registers using the macro definition

To set a bit using a bit position macro and access the registers using the macro definitions, the following code example can be used.

ADC0_CTRLA |= (1 << ADC_ENABLE_bp); /* Enable the ADC peripheral */

To clear the bit using a bit position macro, the following example is provided.

ADC0_CTRLA &= ~(1 << ADC_ENABLE_bp); /* Disable the ADC peripheral */

To test if a bit is set, the following code can be used.

if(ADC0_INTFLAGS & (1 << ADC_RESRDY_bp)) /* Check if the ADC result is ready */
{
    /* Insert some instructions here */
}

To test if a bit is clear, and execute instructions while it remains clear, the following code can be used. In this case, the instructions inside this loop will be executed until the ADC result is ready.

while(!(ADC0_INTFLAGS & (1 << ADC_RESRDY_bp)))
{
    /* Insert some instructions here */
}