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 */
/* incorrect initialization of the T0CON0 register */
T0CON0 = _T0CON0_T0EN_MASK;
T0CON0 = _T0CON0_T0OUTPS0_MASK;
T0CON0 = _T0CON0_T0OUTPS3_MASK;
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 */