From b94459ebae13b73757d127afefa8487c22a0e2d6 Mon Sep 17 00:00:00 2001 From: klassents Date: Wed, 11 Sep 2024 16:49:08 +0700 Subject: [PATCH] =?UTF-8?q?=D0=BF=D1=80=D0=B8=20=D0=B8=D0=B7=D0=BC=D0=B5?= =?UTF-8?q?=D1=80=D0=B5=D0=BD=D0=B8=D0=B8=20=D0=B0=D1=86=D0=BF=20=D0=BD?= =?UTF-8?q?=D0=B0=D0=BA=D0=B0=D0=BF=D0=BB=D0=B8=D0=B2=D0=B0=D0=B5=D1=82=20?= =?UTF-8?q?=D0=BD=D0=B5=D0=BA=D0=BE=D1=82=D0=BE=D1=80=D0=BE=D0=B5=20=D0=BA?= =?UTF-8?q?=D0=BE=D0=BB-=D0=B2=D0=BE=20=D0=B8=D0=B7=D0=BC=D0=B5=D1=80?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D0=B9=20=D0=B8=20=D1=83=D1=81=D1=80=D0=B5?= =?UTF-8?q?=D0=B4=D0=BD=D1=8F=D0=B5=D1=82=20=D0=B8=D1=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cores/arduino/wiring_analog.c | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) 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