2.2.1 Example - ADC
The ADC_t
structure type is defined in the header file, as
presented in the code below. It contains all the module’s registers in the data sheet
specified order, as they are organized in the memory.
/* Analog-to-Digital Converter */ typedef struct ADC_struct { register8_t CTRLA; /* Control A */ register8_t CTRLB; /* Control B */ register8_t CTRLC; /* Control C */ register8_t CTRLD; /* Control D */ register8_t CTRLE; /* Control E */ register8_t SAMPCTRL; /* Sample Control */ register8_t MUXPOS; /* Positive MUX input */ register8_t reserved_1[1]; register8_t COMMAND; /* Command */ register8_t EVCTRL; /* Event Control */ register8_t INTCTRL; /* Interrupt Control */ register8_t INTFLAGS; /* Interrupt Flags */ register8_t DBGCTRL; /* Debug Control */ register8_t TEMP; /* Temporary Data */ register8_t reserved_2[2]; _WORDREGISTER(RES); /* ADC Accumulator Result */ _WORDREGISTER(WINLT); /* Window comparator low threshold */ _WORDREGISTER(WINHT); /* Window comparator high threshold */ register8_t CALIB; /* Calibration */ register8_t reserved_3[1]; } ADC_t;
A macro for an instance of a module is then defined in the header file using that structure type, as presented in the code below.
#define ADC0 (*(ADC_t *) 0x0600) /* Analog-to-Digital Converter */
Therefore, a particular module register, e.g., the CTRLA register, can be addressed as ADC0.CTRLA.