-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathINA219.cpp
201 lines (168 loc) · 5.68 KB
/
INA219.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/**
* @file INA219.cpp
* @version 0.9
*
* @section License
* Copyright (C) 2014-2015, jeditekunum
*
* 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.
*
*/
#include "INA219.hh"
/**
* This driver does not bother with the current or power registers of
* the device (and the subsequent need for calibration and calculation).
*
* Current and power are simply calculated here from the shunt voltage
* and resistance.
*/
const INA219::conversion_time_t INA219::conversion_time[] __PROGMEM = {
// {INA219::BUS_ADC_RESOLUTION_9BIT, 84UL},
// {INA219::BUS_ADC_RESOLUTION_10BIT, 148UL},
// {INA219::BUS_ADC_RESOLUTION_11BIT, 276UL},
{INA219::BUS_ADC_RESOLUTION_12BIT, 532UL},
// {INA219::BUS_ADC_RESOLUTION_12BIT_1S_532US, 532UL},
// {INA219::BUS_ADC_RESOLUTION_12BIT_2S_1060US, 1060UL},
// {INA219::BUS_ADC_RESOLUTION_12BIT_4S_2130US, 2130UL},
// {INA219::BUS_ADC_RESOLUTION_12BIT_8S_4260US, 4260UL},
// {INA219::BUS_ADC_RESOLUTION_12BIT_16S_8510US, 8510UL},
// {INA219::BUS_ADC_RESOLUTION_12BIT_32S_17MS, 17020UL},
// {INA219::BUS_ADC_RESOLUTION_12BIT_64S_34MS, 34050UL},
// {INA219::BUS_ADC_RESOLUTION_12BIT_128S_69MS, 68100UL},
// {INA219::SHUNT_ADC_RESOLUTION_9BIT, 84UL},
// {INA219::SHUNT_ADC_RESOLUTION_10BIT, 148UL},
// {INA219::SHUNT_ADC_RESOLUTION_11BIT, 276UL},
{INA219::SHUNT_ADC_RESOLUTION_12BIT, 532UL}
// {INA219::SHUNT_ADC_RESOLUTION_12BIT_1S_532US, 532UL},
// {INA219::SHUNT_ADC_RESOLUTION_12BIT_2S_1060US, 1060UL},
// {INA219::SHUNT_ADC_RESOLUTION_12BIT_4S_2130US, 2130UL},
// {INA219::SHUNT_ADC_RESOLUTION_12BIT_8S_4260US, 4260UL},
// {INA219::SHUNT_ADC_RESOLUTION_12BIT_16S_8510US, 8510UL},
// {INA219::SHUNT_ADC_RESOLUTION_12BIT_32S_17MS, 17020UL},
// {INA219::SHUNT_ADC_RESOLUTION_12BIT_128S_69MS, 68100UL}
};
uint32_t
INA219::conversion(uint16_t adc_resolution)
{
for (uint8_t i = 0; i<membersof(conversion_time); i++)
if (adc_resolution == (uint16_t)pgm_read_word(&conversion_time[i]))
return ((uint32_t)pgm_read_dword(&conversion_time[i].us));
return (0);
}
void
INA219::begin(range_t range)
{
m_range = range;
write_register(CONFIG, RESET);
DELAY(50); // recover from a possible powerdown (requires 40us)
switch (m_range)
{
case RANGE_32V:
configure(BUS_VOLTAGE_RANGE_32V |
GAIN_8_320MV |
BUS_ADC_RESOLUTION_12BIT |
SHUNT_ADC_RESOLUTION_12BIT |
MODE_SHUNT_AND_BUS_VOLTAGE_CONTINUOUS);
break;
case RANGE_16V:
configure(BUS_VOLTAGE_RANGE_16V |
GAIN_1_40MV |
BUS_ADC_RESOLUTION_12BIT |
SHUNT_ADC_RESOLUTION_12BIT |
MODE_SHUNT_AND_BUS_VOLTAGE_CONTINUOUS);
break;
};
m_active = true;
}
void
INA219::wait(register_t reg)
{
switch (reg)
{
case BUS_VOLTAGE:
if (m_bus_conversion_time > 1000) // us
delay((m_bus_conversion_time / 1000) + 1); // ms
else
DELAY(m_bus_conversion_time + 250); // us
break;
case SHUNT_VOLTAGE:
if (m_shunt_conversion_time > 1000) // us
delay((m_shunt_conversion_time / 1000) + 1); // ms
else
DELAY(m_shunt_conversion_time + 250); // us
break;
default:
break;
};
}
void
INA219::configure(uint16_t config)
{
m_config_value = config;
m_bus_conversion_time = conversion(config & BUS_ADC_RESOLUTION_MASK);
m_shunt_conversion_time = conversion(config & SHUNT_ADC_RESOLUTION_MASK);
write_register(CONFIG, config);
// If mode is continuous then wait for first converion to complete
if (((m_config_value & MODE_MASK) == MODE_SHUNT_VOLTAGE_CONTINUOUS) ||
((m_config_value & MODE_MASK) == MODE_BUS_VOLTAGE_CONTINUOUS) ||
((m_config_value & MODE_MASK) == MODE_SHUNT_AND_BUS_VOLTAGE_CONTINUOUS))
wait((m_bus_conversion_time > m_shunt_conversion_time) ? BUS_VOLTAGE : SHUNT_VOLTAGE);
}
void
INA219::end()
{
m_active = false;
write_register(CONFIG, MODE_POWERDOWN);
}
void
INA219::write_register(register_t reg, uint16_t value)
{
value = swap(value);
twi.acquire(this);
twi.write((uint8_t)reg, &value, sizeof(value));
twi.release();
}
uint16_t
INA219::read_register(register_t reg)
{
uint16_t res;
if (!m_active)
return (0);
twi.acquire(this);
twi.write((uint8_t)reg);
// If mode is triggered then wait for conversion to complete
if ((((reg == SHUNT_VOLTAGE)) &&
(((m_config_value & MODE_MASK) == MODE_SHUNT_VOLTAGE_TRIGGERED) ||
((m_config_value & MODE_MASK) == MODE_SHUNT_AND_BUS_VOLTAGE_TRIGGERED))) ||
((reg == BUS_VOLTAGE) &&
(((m_config_value & MODE_MASK) == MODE_BUS_VOLTAGE_TRIGGERED) ||
((m_config_value & MODE_MASK) == MODE_SHUNT_AND_BUS_VOLTAGE_TRIGGERED))))
wait(reg);
twi.read(&res, sizeof(res));
twi.release();
res = swap(res);
return (res);
}
uint16_t
INA219::current(uint16_t uv)
{
// return 10ths of ma
return ((uv * 10UL) / (uint32_t)m_resistor);
}
uint16_t
INA219::power(uint16_t uv)
{
uint32_t calc;
calc = uv / 10;
calc *= calc; // **2
// return tenths of mW
return (calc / (uint32_t)m_resistor);
}