8.1 Initialize the ADC
In the Window Comparator configuration, the device detects if the ADC result is below or above a specific threshold value. This is useful when monitoring a signal that is required to be maintained inside a specific range or for signaling low battery/overcharge. The Window Comparator can be used in both Free-Running mode and Single Conversion mode.
The ADC Window Comparator uses the Config A setup explained in Hardware Configuration.
In this example, the Window Comparator is used in Free-Running mode. A monitored signal
requires continuous sampling, and the Free-Running mode reduces the CPU load by not
requiring a manual start for each conversion. For this example, a 0x100
threshold is set in the ADC0.WINLT register.
In the code snippet below, the WINDOW_CMP_LOW_TH_EXAMPLE macro is defined and set
for 0x100
and provided as a low threshold for the Window
Comparator:
#define WINDOW_CMP_LOW_TH_EXAMPLE (0x100)
ADC0.WINLT = WINDOW_CMP_LOW_TH_EXAMPLE;
The ADC Window Comparator mode is set in the ADC0.CTRLE register:
The following line of code sets the threshold for results lower than WINLT:
ADC0.CTRLE = ADC_WINCM_BELOW_gc;
If the ADC result is below the previously set threshold value, the WCMP bit in the
ADC0.INTFLAGS register is set by the hardware, and it needs to be cleared by writing a
‘1
’ to it or by reading the conversion result from the RES
register.