Added API to reconfigure ADC reference voltage at runtime.

pull/12471/head
George Beckstein 2020-02-19 13:27:08 -05:00
parent 6ffacaef05
commit 5a543918ca
3 changed files with 23 additions and 5 deletions

View File

@ -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<PlatformMutex> _mutex;
float vref;
#endif //!defined(DOXYGEN_ONLY)
};

View File

@ -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
}
}

View File

@ -23,14 +23,14 @@ namespace mbed {
SingletonPtr<PlatformMutex> 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