Skip to content

Commit d094092

Browse files
committed
Added API to reconfigure ADC reference voltage at runtime.
1 parent 09bb6bd commit d094092

File tree

3 files changed

+23
-5
lines changed

3 files changed

+23
-5
lines changed

drivers/AnalogIn.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,17 @@ class AnalogIn {
103103
*/
104104
float read_volts();
105105

106+
/**
107+
* Sets this ADC instance's reference voltage.
108+
*
109+
* Defaults to the configurable MBED_CONF_DRIVERS_DEFAULT_ADC_VREF setting.
110+
*
111+
* The ADC's reference voltage is used to scale the output when calling AnalogIn::read_volts
112+
*
113+
* @param[in] vref New ADC reference voltage for this ADC instance.
114+
*/
115+
void set_reference_voltage(float vref);
116+
106117
/** An operator shorthand for read()
107118
*
108119
* The float() operator can be used as a shorthand for read() to simplify common code sequences
@@ -141,6 +152,9 @@ class AnalogIn {
141152

142153
analogin_t _adc;
143154
static SingletonPtr<PlatformMutex> _mutex;
155+
156+
float vref;
157+
144158
#endif //!defined(DOXYGEN_ONLY)
145159

146160
};

drivers/mbed_lib.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@
4242
"help": "QSPI chip select pin",
4343
"value": "QSPI_FLASH1_CSN"
4444
},
45-
"adc_vref": {
46-
"help": "Reference voltage for ADC (float)",
45+
"default_adc_vref": {
46+
"help": "Default reference voltage for ADC (float)",
4747
"value": 3.3f
4848
}
4949
}

drivers/source/AnalogIn.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ namespace mbed {
2323

2424
SingletonPtr<PlatformMutex> AnalogIn::_mutex;
2525

26-
AnalogIn::AnalogIn(PinName pin)
26+
AnalogIn::AnalogIn(PinName pin) : vref(MBED_CONF_DRIVERS_DEFAULT_ADC_VREF)
2727
{
2828
lock();
2929
analogin_init(&_adc, pin);
3030
unlock();
3131
}
3232

33-
AnalogIn::AnalogIn(const PinMap &pinmap)
33+
AnalogIn::AnalogIn(const PinMap &pinmap) : vref(MBED_CONF_DRIVERS_DEFAULT_ADC_VREF)
3434
{
3535
lock();
3636
analogin_init_direct(&_adc, &pinmap);
@@ -56,7 +56,11 @@ unsigned short AnalogIn::read_u16()
5656

5757
float AnalogIn::read_volts() {
5858
float ret = this->read();
59-
return (ret*MBED_CONF_DRIVERS_ADC_VREF);
59+
return (ret*this->vref);
60+
}
61+
62+
void AnalogIn::set_reference_voltage(float vref) {
63+
this->vref = vref;
6064
}
6165

6266
} // namespace mbed

0 commit comments

Comments
 (0)