Skip to content

Commit 86dad5c

Browse files
authored
Merge pull request #12471 from AGlass0fMilk/adc-voltage-api
Extend AnalogIn API: read_voltage
2 parents b1629b7 + 7f19f82 commit 86dad5c

File tree

4 files changed

+67
-6
lines changed

4 files changed

+67
-6
lines changed

drivers/AnalogIn.h

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
#include "platform/SingletonPtr.h"
2626
#include "platform/PlatformMutex.h"
2727

28+
#include <cmath>
29+
2830
namespace mbed {
2931
/** \defgroup mbed-os-public Public API */
3032

@@ -70,15 +72,21 @@ class AnalogIn {
7072
/** Create an AnalogIn, connected to the specified pin
7173
*
7274
* @param pinmap reference to structure which holds static pinmap.
75+
* @param vref (optional) Reference voltage of this AnalogIn instance (defaults to target.default-adc-vref).
76+
*
77+
* @note An input voltage at or above the given vref value will produce a 1.0 result when `read` is called
7378
*/
74-
AnalogIn(const PinMap &pinmap);
75-
AnalogIn(const PinMap &&) = delete; // prevent passing of temporary objects
79+
AnalogIn(const PinMap &pinmap, float vref = MBED_CONF_TARGET_DEFAULT_ADC_VREF);
80+
AnalogIn(const PinMap &&, float vref = MBED_CONF_TARGET_DEFAULT_ADC_VREF) = delete; // prevent passing of temporary objects
7681

7782
/** Create an AnalogIn, connected to the specified pin
7883
*
7984
* @param pin AnalogIn pin to connect to
85+
* @param vref (optional) Reference voltage of this AnalogIn instance (defaults to target.default-adc-vref).
86+
*
87+
* @note An input voltage at or above the given vref value will produce a 1.0 result when `read` is called
8088
*/
81-
AnalogIn(PinName pin);
89+
AnalogIn(PinName pin, float vref = MBED_CONF_TARGET_DEFAULT_ADC_VREF);
8290

8391
/** Read the input voltage, represented as a float in the range [0.0, 1.0]
8492
*
@@ -93,6 +101,35 @@ class AnalogIn {
93101
*/
94102
unsigned short read_u16();
95103

104+
/**
105+
* Read the input voltage in volts. The output depends on the target board's
106+
* ADC reference voltage (typically equal to supply voltage). The ADC reference voltage
107+
* sets the maximum voltage the ADC can quantify (ie: ADC output == ADC_MAX_VALUE when Vin == Vref)
108+
*
109+
* The target's default ADC reference voltage is determined by the configuration
110+
* option target.default-adc_vref. The reference voltage for a particular input
111+
* can be manually specified by either the constructor or `AnalogIn::set_reference_voltage`.
112+
*
113+
* @returns A floating-point value representing the current input voltage, measured in volts.
114+
*/
115+
float read_voltage();
116+
117+
/**
118+
* Sets this AnalogIn instance's reference voltage.
119+
*
120+
* The AnalogIn's reference voltage is used to scale the output when calling AnalogIn::read_volts
121+
*
122+
* @param[in] vref New ADC reference voltage for this AnalogIn instance.
123+
*/
124+
void set_reference_voltage(float vref);
125+
126+
/**
127+
* Gets this AnalogIn instance's reference voltage.
128+
*
129+
* @returns A floating-point value representing this AnalogIn's reference voltage, measured in volts.
130+
*/
131+
float get_reference_voltage() const;
132+
96133
/** An operator shorthand for read()
97134
*
98135
* The float() operator can be used as a shorthand for read() to simplify common code sequences
@@ -131,6 +168,9 @@ class AnalogIn {
131168

132169
analogin_t _adc;
133170
static SingletonPtr<PlatformMutex> _mutex;
171+
172+
float _vref;
173+
134174
#endif //!defined(DOXYGEN_ONLY)
135175

136176
};

drivers/source/AnalogIn.cpp

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

2424
SingletonPtr<PlatformMutex> AnalogIn::_mutex;
2525

26-
AnalogIn::AnalogIn(PinName pin)
26+
27+
AnalogIn::AnalogIn(PinName pin, float vref) : _vref(vref)
2728
{
2829
lock();
2930
analogin_init(&_adc, pin);
3031
unlock();
3132
}
3233

33-
AnalogIn::AnalogIn(const PinMap &pinmap)
34+
AnalogIn::AnalogIn(const PinMap &pinmap, float vref) : _vref(vref)
3435
{
3536
lock();
3637
analogin_init_direct(&_adc, &pinmap);
3738
unlock();
3839
}
3940

40-
4141
float AnalogIn::read()
4242
{
4343
lock();
@@ -54,6 +54,21 @@ unsigned short AnalogIn::read_u16()
5454
return ret;
5555
}
5656

57+
float AnalogIn::read_voltage()
58+
{
59+
return read() * _vref;
60+
}
61+
62+
void AnalogIn::set_reference_voltage(float vref)
63+
{
64+
_vref = vref;
65+
}
66+
67+
float AnalogIn::get_reference_voltage(void) const
68+
{
69+
return _vref;
70+
}
71+
5772
} // namespace mbed
5873

5974
#endif

targets/targets.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@
8383
"xip-enable": {
8484
"help": "Enable Execute In Place (XIP) on this target. Value is only significant if the board has executable external storage such as QSPIF. If this is enabled, customize the linker file to choose what text segments are placed on external storage",
8585
"value": false
86+
},
87+
"default-adc-vref": {
88+
"help": "Default reference voltage for ADC (float)",
89+
"value": "NAN"
8690
}
8791
}
8892
},

tools/test/travis-ci/doxy-spellchecker/ignore.en.pws

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,4 +114,6 @@ api
114114
uart
115115
chrono
116116
Hinnant
117+
Vin
118+
Vref
117119
_doxy_

0 commit comments

Comments
 (0)