Register Initialization using Bit Masks

Read-modify-write operations are not needed, when working with bit masks or bit positions, if the reset value of the register is 0x00 and the register configures in a single line.

The example below shows how to achieve the same configuration using bit masks.

T0CON0 = _T0CON0_T0EN_MASK      /* Enable TMR0 */
       | _T0CON0_T0OUTPS0_MASK  /* Select 1:10 postscaler */
       | _T0CON0_T0OUTPS3_MASK; /* 8-bit operation mode selected by default */
             			
Note: The bit wise OR (‘|’) operator on the register side of the assignment is left out. In most cases, device and peripheral initialization routines are written in this way.
CAUTION: The above initialization of the register must be done in a single line of C code. Writing as follows, the bit mask used in the second and third line would clear the bits set in the previous lines.
/* incorrect initialization of the T0CON0 register */
T0CON0 = _T0CON0_T0EN_MASK;
T0CON0 = _T0CON0_T0OUTPS0_MASK;
T0CON0 = _T0CON0_T0OUTPS3_MASK;
Note: Bit Masks can only set bits in a single line of code, so configurations which require bits to be cleared are left out since they are correctly configured by their reset value.

In this example, no mask is used to explicitly configure the timer in the 8-bit mode. This is possible because the reset value of the T016BIT is '0' which corresponds to the 8-bit mode. To emphasize the configuration of this bit as 0, the user could explicitly select the desired 8-bit mode by using a read-modify-write operation. However, this would need to be a separate line of code and would leave the register unchanged:

T0CON0 = _T0CON0_T0EN_MASK       /* Enable TMR0 */
       | _T0CON0_T0OUTPS0_MASK   /* Select 1:10 postscaler */
       | _T0CON0_T0OUTPS3_MASK;  
T0CON0 &= ~_T0CON0_T016BIT_MASK; /* Select 8-bit operation mode explicitly */