From 2321bfcad7231bf4df26389808800e434173590d Mon Sep 17 00:00:00 2001 From: klassents Date: Thu, 12 Sep 2024 10:43:35 +0700 Subject: [PATCH] =?UTF-8?q?=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B0=20=D1=84=D1=83=D0=BD=D0=BA=D1=86=D0=B8=D1=8F=20?= =?UTF-8?q?=D0=B8=D0=B7=D0=BC=D0=B5=D0=BD=D0=B5=D0=BD=D0=B8=D1=8F=20=D1=80?= =?UTF-8?q?=D0=B0=D0=B7=D1=80=D0=B5=D1=88=D0=B5=D0=BD=D0=B8=D1=8F=20=D0=B0?= =?UTF-8?q?=D1=86=D0=BF=20=D0=B8=20=D1=81=D0=BE=D0=BE=D1=82=D0=B2=D0=B5?= =?UTF-8?q?=D1=82=D1=81=D1=82=D0=B2=D1=83=D1=8E=D1=89=D0=B8=D0=B5=20=D0=B8?= =?UTF-8?q?=D0=B7=D0=BC=D0=B5=D0=BD=D0=B5=D0=BD=D0=B8=D1=8F=20=D0=B2=20?= =?UTF-8?q?=D1=84=D1=83=D0=BD=D0=BA=D1=86=D0=B8=D0=B8=20analogread()?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cores/arduino/wiring_analog.c | 29 +++++++++++++++++++++++++++-- cores/arduino/wiring_analog.h | 7 +++++++ variants/standart/pins_arduino.h | 1 + 3 files changed, 35 insertions(+), 2 deletions(-) diff --git a/cores/arduino/wiring_analog.c b/cores/arduino/wiring_analog.c index 3e5a6c5..8af6dfe 100644 --- a/cores/arduino/wiring_analog.c +++ b/cores/arduino/wiring_analog.c @@ -11,7 +11,10 @@ extern "C" { extern void ErrorMsgHandler(const char * msg); // -------------------------- Analog read -------------------------- // -#define ADC_SAMPLES_QTY 10 // samples quantity for averaging adc results +#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 @@ -23,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) { @@ -57,8 +70,20 @@ uint32_t analogRead(uint32_t PinNumber) HAL_ADC_Single(&hadc); acc += HAL_ADC_WaitAndGetValue(&hadc); } - // get value by averaging + // get value by averaging with MCU_ADC_RESOLUTION value = acc/ADC_SAMPLES_QTY; + + // map value to selected resolution + if (currentResolution > 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);