-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
VoltageReference.cpp
63 lines (51 loc) · 2.27 KB
/
VoltageReference.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*
VoltageReference.cpp - VoltageReference library
Copyright (c) 2014 Roberto Lo Giacco.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "Arduino.h"
#include "VoltageReference.h"
void VoltageReference::begin(uint32_t calibration) {
VoltageReference::calibration = calibration;
}
void VoltageReference::begin(uint8_t hi, uint8_t mid, uint8_t low) {
VoltageReference::calibration = mergeBytes(hi, mid, low);
if (calibration == INVALID_REFERENCE_CALIBRATION)
VoltageReference::calibration = DEFAULT_REFERENCE_CALIBRATION;
}
uint16_t VoltageReference::readInternalRef() {
// Read 1.1V reference against Vcc
#if defined(__AVR_ATmega32U4__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) || defined(__AVR_ATmega1284P__)
ADMUX = _BV(REFS0) | _BV(MUX4) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
#elif defined (__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__)
ADMUX = _BV(MUX5) | _BV(MUX0);
#elif defined (__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__)
ADMUX = _BV(MUX3) | _BV(MUX2);
#else
ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
#endif
delay(2); // Wait for Vref to settle
ADCSRA |= _BV(ADSC); // Start conversion
while (bit_is_set(ADCSRA, ADSC)); // measuring
uint8_t low = ADCL; // must read ADCL first - it then locks ADCH
uint8_t high = ADCH; // unlocks both
return (high << 8) | low; // raw bandgap value;
}
uint16_t VoltageReference::readVcc() {
return calibration / readInternalRef();
}
uint16_t VoltageReference::internalValue() {
return (calibration + (ANALOG_MAX_VALUE / 2)) / ANALOG_MAX_VALUE;
}
uint32_t VoltageReference::calibrate(uint16_t milliVolts) {
return ((uint32_t) milliVolts * readInternalRef());
}