Register Initialization

In order to initialize a register, the user must find the desired configuration of the register to achieve the expected functionality by consulting the device data sheet and setting or clearing register bits, so that the value in the register matches the desired configuration.

Register initialization is most often performed as part of device initialization after reset, when the register is in a known state of '0'. This is a special use-case, since:

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.

Note: For most PIC registers, the reset value for all bits and bit fields is ‘0’, but there are exceptions. For example, the Peripheral Interrupt Priority Register 3 has several bits with the reset value ‘1’. In this case, the user has to explicitly set the desired configuration without relying on the fact that usually bits reset values are 0. The reset value for all bits and bit fields in a register are shown in the figure below.
Figure 1. Peripheral Interrupt Priority Register 3 – Reset Value

The following example will apply the various methods of configuring a register (T0CON0 – Timer 0 Control Register 0), shown in the figure below.

Note: For the register in this example (T0CON0), all bits and bit fields are ‘0’ after reset.
Figure 2. Timer 0 Control Register 0 – Configuration
Here is the desired configuration:

The resulting value can be directly written to the register:

T0CON0 = 0b1000 1001;    /* binary */
T0CON0 = 0x89;           /* hexadecimal */
T0CON0 = 137;            /* decimal */

However, to improve the readability (and potentially the portability) of the code, it is recommended to use the device defines, which are shown in the upcoming sections.