Addressing SAM D21 ADC

Hi haresfur,

To increase the ADC's resolution to 16-bits it's necessary to oversample by accumulating 256 samples (4*n samples, where n = 4 extra bits) and decimation with 4 automatic shifts to the right. This requires the SAMPLECTRL and ADJRES bitfields in the ADC's AVGCTRL register to be set to ADC_AVGCTRL_SAMPLENUM_256 and 0 respectively:

ADC->AVGCTRL.reg = ADC_AVGCTRL_SAMPLENUM_256 | ADC_AVGCTRL_ADJRES(0);

This is in addition to setting the resolution to 16-bits in the CTRLB register, to activate averaging mode:

ADC->CTRLB.reg |= ADC_CTRLB_RESSEL_16BIT;         // Set ADC resolution to 16 bits

It might also be beneficial to speed up the sampling time by setting the SAMPLECTRL register to 0 as well, (but does depend on the source resistance seen by your analog input pin: Arduino Zero ADC Sample Time Too Long · Issue #327 · arduino/ArduinoCore-samd · GitHub):

ADC->SAMPCTRL.reg = 0x00;