summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorDaniel Friesel <derf@finalrewind.org>2020-03-08 15:49:58 +0100
committerDaniel Friesel <derf@finalrewind.org>2020-03-08 15:49:58 +0100
commit0af72f0d9db44381e15f29e8c72fb7f39603ee86 (patch)
treeb842d812d69d5e5a1cb001c4cc0b9312e6622d50 /src
parent098fd9b55d055204b811317f2e117232637a48ab (diff)
mpu9250_motionlog: measure VCC
Diffstat (limited to 'src')
-rw-r--r--src/app/mpu9250_motionlog/main.cc20
1 files changed, 20 insertions, 0 deletions
diff --git a/src/app/mpu9250_motionlog/main.cc b/src/app/mpu9250_motionlog/main.cc
index 53a8ba6..5165884 100644
--- a/src/app/mpu9250_motionlog/main.cc
+++ b/src/app/mpu9250_motionlog/main.cc
@@ -23,6 +23,15 @@ int main(void)
gpio.setup();
kout.setup();
+ // One ADC conversion per four seconds
+ TCCR1A = 0;
+ TCCR1B = _BV(CS12) | _BV(CS10);
+
+ // Measure internal 1.1V bandgap using VCC as reference on each Timer 1 overflow
+ ADMUX = _BV(REFS0) | 0x0e;
+ ADCSRB = _BV(ADTS2) | _BV(ADTS1);
+ ADCSRA = _BV(ADEN) | _BV(ADATE) | _BV(ADPS2) | _BV(ADPS1);
+
if (i2c.setup() != 0) {
kout << "I2C setup failed" << endl;
return 1;
@@ -50,6 +59,17 @@ int main(void)
min_ax = min_ay = min_az = 30000;
max_ax = max_ay = max_az = -30000;
i = 0;
+ if (ADCSRA & _BV(ADIF)) {
+ uint8_t adcr_l = ADCL;
+ uint8_t adcr_h = ADCH;
+ uint16_t adcr = adcr_l + (adcr_h << 8);
+ uint16_t vcc = 1100L * 1023 / adcr;
+
+ TIFR1 |= _BV(TOV1);
+ ADCSRA |= _BV(ADIF);
+
+ kout << "VCC: " << vcc << endl;
+ }
}
arch.delay_ms(1);
}