summaryrefslogtreecommitdiff
path: root/src/arch/arduino-nano
diff options
context:
space:
mode:
authorDaniel Friesel <derf@finalrewind.org>2021-12-21 22:01:43 +0100
committerDaniel Friesel <derf@finalrewind.org>2021-12-21 22:01:43 +0100
commitbf974ab871e3be2eabb46be61e1934b8237e16a8 (patch)
treed2f04754358fed8d276b1336b4917bfd392d17f3 /src/arch/arduino-nano
parent56dc720dffcc4639e0bb5492c6101f3e2aa9b5ad (diff)
arduino nano adc: add getPin_mV function
Diffstat (limited to 'src/arch/arduino-nano')
-rw-r--r--src/arch/arduino-nano/driver/adc.cc36
1 files changed, 36 insertions, 0 deletions
diff --git a/src/arch/arduino-nano/driver/adc.cc b/src/arch/arduino-nano/driver/adc.cc
index 17cfc27..8f828a4 100644
--- a/src/arch/arduino-nano/driver/adc.cc
+++ b/src/arch/arduino-nano/driver/adc.cc
@@ -8,6 +8,42 @@
#include "arch.h"
#include "driver/adc.h"
+uint16_t AVRADC::getPin_mV(uint8_t pin, uint16_t avcc)
+{
+ if (avcc) {
+ // measure with AVCC reference
+ ADMUX = _BV(REFS0) | pin;
+ } else {
+ // measure with internal 1.1V bandgap refernce
+ ADMUX = _BV(REFS1) | _BV(REFS0) | pin;
+ }
+
+ // Enable ADC with /64 prescaler
+ ADCSRA = _BV(ADEN) | _BV(ADPS2);
+
+ // Wait for bandgap to stabilise (70us according to datasheet table 28-3)
+ arch.delay_ms(1);
+
+ // Start conversion
+ ADCSRA |= _BV(ADSC);
+
+ // wait until conversion is complete
+ while (ADCSRA & _BV(ADSC)) ;
+
+ uint8_t adcr_l = ADCL;
+ uint8_t adcr_h = ADCH;
+ uint16_t adcr = adcr_l + (adcr_h << 8);
+
+ // Disable ADC
+ ADCSRA &= ~_BV(ADEN);
+
+ if (avcc) {
+ return (uint32_t)avcc * adcr / 1023L;
+ } else {
+ return 1100L * adcr / 1023L;
+ }
+}
+
int16_t AVRADC::getTemp_mdegC(int16_t offset)
{
// Measure temperature probe with 1.1V bandgap reference