Added API call to return ADC reading in volts, scaled by configurable system ADC reference voltage.

pull/12471/head
George Beckstein 2020-02-19 13:19:30 -05:00
parent af4c8a94f3
commit 6ffacaef05
3 changed files with 19 additions and 1 deletions

View File

@ -93,6 +93,16 @@ public:
*/ */
unsigned short read_u16(); unsigned short read_u16();
/** Read the input voltage in volts. The output depends on the target board's
* ADC reference voltage (typically equal to supply voltage). The ADC reference voltage
* sets the maximum voltage the ADC can quantify (ie: Vin == Vref when ADC output == ADC_MAX_VALUE)
*
* The target's ADC reference voltage can be configured by overriding "drivers.adc_vref"
*
* @returns A floating-point value representing the current input voltage, measured in volts.
*/
float read_volts();
/** An operator shorthand for read() /** An operator shorthand for read()
* *
* The float() operator can be used as a shorthand for read() to simplify common code sequences * The float() operator can be used as a shorthand for read() to simplify common code sequences

View File

@ -41,6 +41,9 @@
"qspi_csn": { "qspi_csn": {
"help": "QSPI chip select pin", "help": "QSPI chip select pin",
"value": "QSPI_FLASH1_CSN" "value": "QSPI_FLASH1_CSN"
} },
"adc_vref": {
"help": "Reference voltage for ADC (float)",
"value": 3.3f
} }
} }

View File

@ -54,6 +54,11 @@ unsigned short AnalogIn::read_u16()
return ret; return ret;
} }
float AnalogIn::read_volts() {
float ret = this->read();
return (ret*MBED_CONF_DRIVERS_ADC_VREF);
}
} // namespace mbed } // namespace mbed
#endif #endif