Register Unions

Each register has a union declared in the header file for the individual bits in that register. This allows access to an individual bit/bit field from the register using the union declaration.

typedef union {
    struct {
        unsigned ADGO                   :1;
        unsigned                        :1;
        unsigned ADFM                   :1;
        unsigned                        :1;
        unsigned ADCS                   :1;
        unsigned                        :1;
        unsigned ADCONT                 :1;
        unsigned ADON                   :1;
    };
    struct {
        unsigned GO                     :1;
        unsigned                        :1;
        unsigned ADFM0                  :1;
    };
} ADCON0bits_t;
extern volatile ADCON0bits_t ADCON0bits __at(0xF5B);

The union declaration of the ADCON0 register is shown in the code listing above. This register can be accessed as a whole using the macro declaration or as an individual bit/bit field from the register using the union declaration. Here is an example:

ADCON0 = 0x01;       /* using macro declaration */
ADCON0bits.GO = 1;   /* using bit unions with short bit name convention */
ADCON0bits.ADGO = 1; /* using bit unions with long bit name convention */
The convention used when accessing a bit or a bit field from a register using the union declaration of register is presented in Figure 2-1.
Figure 1. Access Register Unions Convention

For further details on unions, consult Microchip Developer - Unions.