From 5a543918ca0e79a41b2d724b7bf60716f8bc1bb0 Mon Sep 17 00:00:00 2001 From: George Beckstein Date: Wed, 19 Feb 2020 13:27:08 -0500 Subject: [PATCH] Added API to reconfigure ADC reference voltage at runtime. --- drivers/AnalogIn.h | 14 ++++++++++++++ drivers/mbed_lib.json | 4 ++-- drivers/source/AnalogIn.cpp | 10 +++++++--- 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/drivers/AnalogIn.h b/drivers/AnalogIn.h index 0b04b5c1ac..ed6c14b69d 100644 --- a/drivers/AnalogIn.h +++ b/drivers/AnalogIn.h @@ -103,6 +103,17 @@ public: */ float read_volts(); + /** + * Sets this ADC instance's reference voltage. + * + * Defaults to the configurable MBED_CONF_DRIVERS_DEFAULT_ADC_VREF setting. + * + * The ADC's reference voltage is used to scale the output when calling AnalogIn::read_volts + * + * @param[in] vref New ADC reference voltage for this ADC instance. + */ + void set_reference_voltage(float vref); + /** An operator shorthand for read() * * The float() operator can be used as a shorthand for read() to simplify common code sequences @@ -141,6 +152,9 @@ protected: analogin_t _adc; static SingletonPtr _mutex; + + float vref; + #endif //!defined(DOXYGEN_ONLY) }; diff --git a/drivers/mbed_lib.json b/drivers/mbed_lib.json index 8caeebad2c..6da6230979 100644 --- a/drivers/mbed_lib.json +++ b/drivers/mbed_lib.json @@ -42,8 +42,8 @@ "help": "QSPI chip select pin", "value": "QSPI_FLASH1_CSN" }, - "adc_vref": { - "help": "Reference voltage for ADC (float)", + "default_adc_vref": { + "help": "Default reference voltage for ADC (float)", "value": 3.3f } } diff --git a/drivers/source/AnalogIn.cpp b/drivers/source/AnalogIn.cpp index 084930f4ec..1370a71863 100644 --- a/drivers/source/AnalogIn.cpp +++ b/drivers/source/AnalogIn.cpp @@ -23,14 +23,14 @@ namespace mbed { SingletonPtr AnalogIn::_mutex; -AnalogIn::AnalogIn(PinName pin) +AnalogIn::AnalogIn(PinName pin) : vref(MBED_CONF_DRIVERS_DEFAULT_ADC_VREF) { lock(); analogin_init(&_adc, pin); unlock(); } -AnalogIn::AnalogIn(const PinMap &pinmap) +AnalogIn::AnalogIn(const PinMap &pinmap) : vref(MBED_CONF_DRIVERS_DEFAULT_ADC_VREF) { lock(); analogin_init_direct(&_adc, &pinmap); @@ -56,7 +56,11 @@ unsigned short AnalogIn::read_u16() float AnalogIn::read_volts() { float ret = this->read(); - return (ret*MBED_CONF_DRIVERS_ADC_VREF); + return (ret*this->vref); +} + +void AnalogIn::set_reference_voltage(float vref) { + this->vref = vref; } } // namespace mbed