ADC Sample Accumulator

In Sample Accumulator mode the ADC can add up to 64 samples in an accumulator register, thus filtering the signal and reducing the noise. It is useful when reading analog sensor data where a smooth signal is required. By using a hardware accumulator instead of adding those readings in software, it reduces the CPU load. To activate this mode, the Sample Accumulation Number in the ADC0.CTRLB register must be set in addition to the normal ADC initialization.
Figure 1. ADC0.CTRLB - set SAMPNUM bit
ADC0.CTRLB = ADC_SAMPNUM_ACC64_gc;
The ADC conversion is started by setting the STCONV bit in the ADC0.COMMAND register.
ADC0.COMMAND = ADC_STCONV_bm;
The samples will be added up in the ADC0.RES register. The ADC_RESRDY flag is set after the number of samples specified in ADC0.CTRLB is acquired.
while (!(ADC0.INTFLAGS & ADC_RESRDY_bm))
{
    ;
}
The user can read that value and divide it by the number of samples, in order to get an average value.
adcVal = ADC0.RES;
adcVal = adcVal >> ADC_SHIFT_DIV64;
The user must clear the RESRDY bit by writing ‘1’ to it before starting another conversion.
ADC0.INTFLAGS = ADC_RESRDY_bm;
Tip: The full code example is also available in the Appendix section.