From 63e718e83f708a5bf3db38fbd1052064e1f9ed35 Mon Sep 17 00:00:00 2001 From: klassents Date: Thu, 12 Sep 2024 06:46:31 +0300 Subject: [PATCH] dev_beta_adcTest (#2) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - добавлена функция изменения разрешения ацп и соответствующие изменения в функции analogRead() - при измерении ацп накапливает некоторое кол-во измерений и усредняет их --- cores/arduino/wiring_analog.c | 43 +++++++++++++++++++++++++++++--- cores/arduino/wiring_analog.h | 7 ++++++ variants/standart/pins_arduino.h | 1 + 3 files changed, 47 insertions(+), 4 deletions(-) diff --git a/cores/arduino/wiring_analog.c b/cores/arduino/wiring_analog.c index cdf1226..8af6dfe 100644 --- a/cores/arduino/wiring_analog.c +++ b/cores/arduino/wiring_analog.c @@ -11,6 +11,11 @@ extern "C" { extern void ErrorMsgHandler(const char * msg); // -------------------------- Analog read -------------------------- // +#define ADC_SAMPLES_QTY 10 // samples quantity for averaging adc results +#define ADC_DEFAULT_RESOLUTION 10 // resolution for arduino compatibility + +uint8_t currentResolution = ADC_DEFAULT_RESOLUTION; // resolution used for output results + // structure for ADC channel initialization. Only the channel number // changes, everything else is the same static ADC_HandleTypeDef hadc = @@ -21,6 +26,16 @@ static ADC_HandleTypeDef hadc = .Init.Sel = 0 }; +void analogReadResolution(uint8_t resolution) +{ + // resolution limits + if (resolution > 32) resolution = 32; + if (resolution < 1) resolution = 1; + + // save new resolution + currentResolution = resolution; +} + // initialize the channel, run a single measurement, wait for the result uint32_t analogRead(uint32_t PinNumber) { @@ -44,11 +59,31 @@ 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 MCU_ADC_RESOLUTION) + { + // extra least significant bits are padded with zeros + value = (value << (currentResolution - MCU_ADC_RESOLUTION)); + } + else + { + // extra least significant bits read from the ADC are discarded + value = (value >> (MCU_ADC_RESOLUTION - currentResolution)); + } } else ErrorMsgHandler("analogRead(): invalid analog pin number"); diff --git a/cores/arduino/wiring_analog.h b/cores/arduino/wiring_analog.h index 2e90d8d..5c3a4b4 100644 --- a/cores/arduino/wiring_analog.h +++ b/cores/arduino/wiring_analog.h @@ -16,6 +16,13 @@ extern "C" { */ uint32_t analogRead(uint32_t PinNumber); +/* + * \brief Set the resolution of adc results. Default is 10 bits (range from 0 to 1023). + * + * \param resolution 1...32 + */ +void analogReadResolution(uint8_t resolution); + /* * \brief Writes an analog value (PWM wave) to a pin. * diff --git a/variants/standart/pins_arduino.h b/variants/standart/pins_arduino.h index 16dc84b..ebfccae 100644 --- a/variants/standart/pins_arduino.h +++ b/variants/standart/pins_arduino.h @@ -71,6 +71,7 @@ volatile uint32_t* portInputRegister(GPIO_TypeDef* GPIO_x); #define SERIAL_PORT_QTY 2 // ADC +#define MCU_ADC_RESOLUTION 12 // bits // determines the ADC channel number by the board pin number uint32_t analogInputToChannelNumber(uint32_t PinNumber);