-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAdafruit_INA219.h
235 lines (208 loc) · 9.28 KB
/
Adafruit_INA219.h
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/*!
* @file Adafruit_INA219.h
*
* This is a library for the Adafruit INA219 breakout board
* ----> https://www.adafruit.com/products/904
*
* Adafruit invests time and resources providing this open source code,
* please support Adafruit and open-source hardware by purchasing
* products from Adafruit!
*
* Written by Kevin "KTOWN" Townsend for Adafruit Industries.
*
* BSD license, all text here must be included in any redistribution.
*
*/
#ifndef _LIB_ADAFRUIT_INA219_
#define _LIB_ADAFRUIT_INA219_
#include <inttypes.h>
#include "I2C.h"
/**************************************************************************/
/*!
@brief default I2C address
*/
/**************************************************************************/
#define INA219_ADDRESS (0x40) // 1000000 (A0+A1=GND)
/**************************************************************************/
/*!
@brief read
*/
/**************************************************************************/
#define INA219_READ (0x01)
/*=========================================================================
CONFIG REGISTER (R/W)
-----------------------------------------------------------------------*/
/**************************************************************************/
/*!
@brief config register address
*/
/**************************************************************************/
#define INA219_REG_CONFIG (0x00)
/*---------------------------------------------------------------------*/
/**************************************************************************/
/*!
@brief reset bit
*/
/**************************************************************************/
#define INA219_CONFIG_RESET (0x8000) // Reset Bit
/**************************************************************************/
/*!
@brief mask for bus voltage range
*/
/**************************************************************************/
#define INA219_CONFIG_BVOLTAGERANGE_MASK (0x2000) // Bus Voltage Range Mask
/**************************************************************************/
/*!
@brief bus voltage range values
*/
/**************************************************************************/
enum{
INA219_CONFIG_BVOLTAGERANGE_16V = (0x0000), // 0-16V Range
INA219_CONFIG_BVOLTAGERANGE_32V = (0x2000), // 0-32V Range
};
/**************************************************************************/
/*!
@brief mask for gain bits
*/
/**************************************************************************/
#define INA219_CONFIG_GAIN_MASK (0x1800) // Gain Mask
/**************************************************************************/
/*!
@brief values for gain bits
*/
/**************************************************************************/
enum{
INA219_CONFIG_GAIN_1_40MV = (0x0000), // Gain 1, 40mV Range
INA219_CONFIG_GAIN_2_80MV = (0x0800), // Gain 2, 80mV Range
INA219_CONFIG_GAIN_4_160MV = (0x1000), // Gain 4, 160mV Range
INA219_CONFIG_GAIN_8_320MV = (0x1800), // Gain 8, 320mV Range
};
/**************************************************************************/
/*!
@brief mask for bus ADC resolution bits
*/
/**************************************************************************/
#define INA219_CONFIG_BADCRES_MASK (0x0780) // Bus ADC Resolution Mask
/**************************************************************************/
/*!
@brief values for bus ADC resolution
*/
/**************************************************************************/
enum {
INA219_CONFIG_BADCRES_9BIT = (0x0000), // 9-bit bus res = 0..511
INA219_CONFIG_BADCRES_10BIT = (0x0080), // 10-bit bus res = 0..1023
INA219_CONFIG_BADCRES_11BIT = (0x0100), // 11-bit bus res = 0..2047
INA219_CONFIG_BADCRES_12BIT = (0x0180), // 12-bit bus res = 0..4095
};
/**************************************************************************/
/*!
@brief mask for shunt ADC resolution bits
*/
/**************************************************************************/
#define INA219_CONFIG_SADCRES_MASK (0x0078) // Shunt ADC Resolution and Averaging Mask
/**************************************************************************/
/*!
@brief values for shunt ADC resolution
*/
/**************************************************************************/
enum {
INA219_CONFIG_SADCRES_9BIT_1S_84US = (0x0000), // 1 x 9-bit shunt sample
INA219_CONFIG_SADCRES_10BIT_1S_148US = (0x0008), // 1 x 10-bit shunt sample
INA219_CONFIG_SADCRES_11BIT_1S_276US = (0x0010), // 1 x 11-bit shunt sample
INA219_CONFIG_SADCRES_12BIT_1S_532US = (0x0018), // 1 x 12-bit shunt sample
INA219_CONFIG_SADCRES_12BIT_2S_1060US = (0x0048), // 2 x 12-bit shunt samples averaged together
INA219_CONFIG_SADCRES_12BIT_4S_2130US = (0x0050), // 4 x 12-bit shunt samples averaged together
INA219_CONFIG_SADCRES_12BIT_8S_4260US = (0x0058), // 8 x 12-bit shunt samples averaged together
INA219_CONFIG_SADCRES_12BIT_16S_8510US = (0x0060), // 16 x 12-bit shunt samples averaged together
INA219_CONFIG_SADCRES_12BIT_32S_17MS = (0x0068), // 32 x 12-bit shunt samples averaged together
INA219_CONFIG_SADCRES_12BIT_64S_34MS = (0x0070), // 64 x 12-bit shunt samples averaged together
INA219_CONFIG_SADCRES_12BIT_128S_69MS = (0x0078), // 128 x 12-bit shunt samples averaged together
};
/**************************************************************************/
/*!
@brief mask for operating mode bits
*/
/**************************************************************************/
#define INA219_CONFIG_MODE_MASK (0x0007) // Operating Mode Mask
/**************************************************************************/
/*!
@brief values for operating mode
*/
/**************************************************************************/
enum {
INA219_CONFIG_MODE_POWERDOWN = (0x0000),
INA219_CONFIG_MODE_SVOLT_TRIGGERED = (0x0001),
INA219_CONFIG_MODE_BVOLT_TRIGGERED = (0x0002),
INA219_CONFIG_MODE_SANDBVOLT_TRIGGERED = (0x0003),
INA219_CONFIG_MODE_ADCOFF = (0x0004),
INA219_CONFIG_MODE_SVOLT_CONTINUOUS = (0x0005),
INA219_CONFIG_MODE_BVOLT_CONTINUOUS = (0x0006),
INA219_CONFIG_MODE_SANDBVOLT_CONTINUOUS = (0x0007),
};
/*=========================================================================*/
/**************************************************************************/
/*!
@brief shunt voltage register
*/
/**************************************************************************/
#define INA219_REG_SHUNTVOLTAGE (0x01)
/*=========================================================================*/
/**************************************************************************/
/*!
@brief bus voltage register
*/
/**************************************************************************/
#define INA219_REG_BUSVOLTAGE (0x02)
/*=========================================================================*/
/**************************************************************************/
/*!
@brief power register
*/
/**************************************************************************/
#define INA219_REG_POWER (0x03)
/*=========================================================================*/
/**************************************************************************/
/*!
@brief current register
*/
/**************************************************************************/
#define INA219_REG_CURRENT (0x04)
/*=========================================================================*/
/**************************************************************************/
/*!
@brief calibration register
*/
/**************************************************************************/
#define INA219_REG_CALIBRATION (0x05)
/*=========================================================================*/
/**************************************************************************/
/*!
@brief Class that stores state and functions for interacting with INA219 current/power monitor IC
*/
/**************************************************************************/
class Adafruit_INA219{
public:
Adafruit_INA219(uint8_t bus,uint8_t addr = INA219_ADDRESS);
void setCalibration_32V_2A(void);
void setCalibration_32V_1A(void);
void setCalibration_16V_400mA(void);
float getBusVoltage_V(void);
float getShuntVoltage_mV(void);
float getCurrent_mA(void);
float getPower_mW(void);
private:
I2C *_i2c;
uint8_t ina219_i2caddr;
uint32_t ina219_calValue;
// The following multipliers are used to convert raw current and power
// values to mA and mW, taking into account the current config settings
uint32_t ina219_currentDivider_mA;
float ina219_powerMultiplier_mW;
void wireWriteRegister(uint8_t reg, uint16_t value);
void wireReadRegister(uint8_t reg, uint16_t *value);
int16_t getBusVoltage_raw(void);
int16_t getShuntVoltage_raw(void);
int16_t getCurrent_raw(void);
int16_t getPower_raw(void);
};
#endif