forked from Yveaux/Arduino_Vcc
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathVcc.cpp
115 lines (96 loc) · 3.4 KB
/
Vcc.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
Vcc - A supply voltage measuring library for Arduino
Created by Ivo Pullens, Emmission, 2014
Inspired by:
http://provideyourown.com/2012/secret-arduino-voltmeter-measure-battery-voltage/
This library 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 2.1 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "Vcc.h"
Vcc::Vcc( const float correction )
: m_correction(correction)
{
}
#if defined(__AVR_ATmega32U4__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
#define ADMUX_VCCWRT1V1 (_BV(REFS0) | _BV(MUX4) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1))
#define _IVREF 1.1
#define _ADCMAXRES 1024.0
#elif defined (__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__)
#define ADMUX_VCCWRT1V1 (_BV(MUX5) | _BV(MUX0))
#define _IVREF 1.1
#define _ADCMAXRES 1024.0
#elif defined (__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__)
#define ADMUX_VCCWRT1V1 (_BV(MUX3) | _BV(MUX2))
#define _IVREF 1.1
#define _ADCMAXRES 1024.0
#elif defined(__LGT8FX8P__)
#define ADMUX_VCCWRT1V1 (_BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX0))
#define _IVREF 1.024
#define _ADCMAXRES 4096.0
#elif defined(__LGT8FX8E__)
#define ADMUX_VCCWRT1V1 (_BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1))
#define _IVREF 1.25
#define _ADCMAXRES 4096.0
#else // defined(__AVR_ATmega328P__)
#define ADMUX_VCCWRT1V1 (_BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1))
#define _IVREF 1.1
#define _ADCMAXRES 1024.0
#endif
uint16_t adcRead_(void){
ADCSRA |= _BV(ADSC);
while (bit_is_set(ADCSRA, ADSC));
return ADC;
}
float Vcc::Read_Volts(void)
{
analogReference(DEFAULT); // Set AD reference to VCC
#if defined(__LGT8FX8P__)
ADCSRD |= _BV(BGEN); // IVSEL enable
#endif
// Read 1.1V/1.024V/1.25V reference against AVcc
// set the reference to Vcc and the measurement to the internal 1.1V reference
if (ADMUX != ADMUX_VCCWRT1V1)
{
ADMUX = ADMUX_VCCWRT1V1;
// Wait for Vref to settle. Bandgap reference start-up time: max 70us
delayMicroseconds(350);
}
uint16_t pVal;
#if defined(__LGT8FX8P__)
uint16_t nVal;
ADCSRC |= _BV(SPN);
nVal = adcRead_();
ADCSRC &= ~_BV(SPN);
#endif
pVal = adcRead_();
#if defined(__LGT8FX8P__)
pVal = (pVal + nVal) >> 1;
#endif
// Logicgreen gain-error correction
#if defined(__LGT8FX8E__)
pVal -= (pVal >> 5);
#elif defined(__LGT8FX8P__)
pVal -= (pVal >> 7);
#endif
// Calculate Vcc (in V)
float vcc = m_correction * _IVREF * _ADCMAXRES / pVal;
return vcc;
} // end Read_Volts
float Vcc::Read_Perc(const float range_min, const float range_max, const boolean clip)
{
// Read Vcc and convert to percentage
float perc = 100.0 * (Read_Volts()-range_min) / (range_max-range_min);
// Clip to [0..100]% range, when requested.
if (clip)
perc = constrain(perc, 0.0, 100.0);
return perc;
}