diff --git a/cores/arduino/wiring_analog.c b/cores/arduino/wiring_analog.c index cdf1226..3e5a6c5 100644 --- a/cores/arduino/wiring_analog.c +++ b/cores/arduino/wiring_analog.c @@ -11,6 +11,8 @@ extern "C" { extern void ErrorMsgHandler(const char * msg); // -------------------------- Analog read -------------------------- // +#define ADC_SAMPLES_QTY 10 // samples quantity for averaging adc results + // structure for ADC channel initialization. Only the channel number // changes, everything else is the same static ADC_HandleTypeDef hadc = @@ -44,11 +46,19 @@ uint32_t analogRead(uint32_t PinNumber) hadc.Init.Sel = adcChannel; HAL_ADC_Init(&hadc); - // start the conversion twice in case another channel was polled before + // start the dummy conversion in case another channel was polled before HAL_ADC_SINGLE_AND_SET_CH(hadc.Instance, adcChannel); - value = HAL_ADC_WaitAndGetValue(&hadc); - HAL_ADC_Single(&hadc); - value = HAL_ADC_WaitAndGetValue(&hadc); + HAL_ADC_WaitAndGetValue(&hadc); + + // accumulate results + uint32_t acc = 0; + for (uint8_t i = 0; i