I'm stuck with trying to read multiple ADC channels on a SAMD21 that I have configured using ATMEL.start. I am very new to Microchip Studio (Arduino convert trying to upgrade).
The below code is my basic "read the input voltage" function to test the ADC. My issue is that I have tried changing the channel number (0 below) to both the sequential channels set in Atmel Start ADC confuration (i.e. 0 is PA08/AIN16, 1 is PA09/AIN17, etc.), and the absolute AIN channel, i.e. 16, 17, 18, ...
This gives me exactly the same result for each channel, and not different results for the 4 pins. I have tried enabling the channel before reading each one (no joy) and I have tried using the adc_sync_set_inputs with odd results.
Can somebody give me an example of how to read two ADC inputs, on pins PA08 and PA09, please? I have been banging my head on datasheets and tutorials for days!
Code and atmel.start configuration below:
[![atmel start adc setting][1]][1]
static void read_battery_level()
{
adc_sync_enable_channel(&ADC_BATTERY, 0); // enable ADC, channel 0
uint32_t sum = 0;
for (int i=0; i<NUM_READINGS; i++)
{
// read from ADC channel 0
adc_sync_read_channel(&ADC_BATTERY, 0, battery_reading.c, 2); // 0 is the channel, the .c is the union variable (like a struct)
//add to sum
sum += battery_reading.i;
//short delay
delay_us(20);
}
//calculate battery voltage
battery_voltage = ((sum/NUM_READINGS) * ADC_LSB_MV) / 1000.0f;
}
(sum/NUM_READINGS) * ADC_LSB_MV
should probably besum*ADC_LSB_MV / NUM_READINGS
or you might introduce rounding errors. Also there is no valid reason why you should be using floating point, since this MCU does not have a FPU. \$\endgroup\$