3.2 Register Initialization
To initialize a register, the user must find the desired configuration by consulting the device data sheet. Then, the register bits must be set or cleared 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 Reset state (default).
The Reset value for all bits and bit fields is ‘0
’ for most AVR
registers. Figure 3-1 shows how to find the Reset
value and all the register’s bits configurations desired for this example.
Read-Modify-Write operations are not needed when working with bit masks or -positions if
the Reset value of the register is 0x00
, and the register can be
configured in a single line.
The desired configuration can be, for example:
- ADC is enabled – the ENABLE bit will
be ‘
1
’ - ADC is operating in Differential mode
– the CONVMODE bit will be ‘
1
’ - ADC will run with 10-bit resolution –
the RESSEL bit field value will be
0x00
(default)
This configuration can be implemented by using either the binary, hexadecimal, or decimal value (as presented below) and writing the resulting value directly to the register.
ADC0.CTRLA = 0b00100001; /* binary */ ADC0.CTRLA = 0x21; /* hexadecimal */ ADC0.CTRLA = 33; /* decimal */
However, to improve the code readability (and potentially the portability), it is recommended to use the device defines shown in the upcoming sections.
0
’, but there are exceptions. For example,
the USART0 Control C register has a couple of bits with the Reset value
‘1
’. In this case, the user must explicitly set the desired
configuration without relying on the fact that the Reset values of the bits are usually
‘0
’.
The USART0 Control C register has a Reset value of 0x03
. In this case,
there is a need for a Read-Modify-Write operation to initialize one of the other bits or
bit fields without changing the value of the CHSIZE bit field. Figure 3-2 shows this register.