-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathINA219.h
125 lines (103 loc) · 3.21 KB
/
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
/******************************************************************************
* TI INA219 hi-side i2c current/power monitor Library
*
* http://www.ti.com/product/ina219
*
* 6 May 2012 by John De Cristofaro
*
*
* Tested at standard i2c 100kbps signaling rate.
*
* This library does not handle triggered conversion modes. It uses the INA219
* in continuous conversion mode. All reads are from continous conversions.
*
* A note about the gain (PGA) setting:
* The gain of the ADC pre-amplifier is programmable in the INA219, and can
* be set between 1/8x (default) and unity. This allows a shunt voltage
* range of +/-320mV to +/-40mV respectively. Something to keep in mind,
* however, is that this change in gain DOES NOT affect the resolution
* of the ADC, which is fixed at 1uV. What it does do is increase noise
* immunity by exploiting the integrative nature of the delta-sigma ADC.
* For the best possible reading, you should set the gain to the range
* of voltages that you expect to see in your particular circuit. See
* page 15 in the datasheet for more info about the PGA.
*
* Known bugs:
* * may return unreliable values if not connected to a bus or at
* bus currents below 10uA.
*
* Arduino 1.0 compatible as of 6/6/2012
*
* Dependencies:
* * Arduino Wire library
*
* MIT license
******************************************************************************/
#ifndef ina219_h
#define ina219_h
#if ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#include <Wire.h>
#define INA219_DEBUG 0
// INA219 memory registers
#define CONFIG_R 0x00 // configuration register
#define V_SHUNT_R 0x01 // differential shunt voltage
#define V_BUS_R 0x02 // bus voltage (wrt to system/chip GND)
#define P_BUS_R 0x03 // system power draw (= V_BUS * I_SHUNT)
#define I_SHUNT_R 0x04 // shunt current
#define CAL_R 0x05 // calibration register
#define INA_RESET 0xFFFF // send to CONFIG_R to reset unit
#define CONFIG_DEFAULT 0x399F
// config. register bit labels
#define RST 15
#define BRNG 13
#define PG1 12
#define PG0 11
#define BADC4 10
#define BADC3 9
#define BADC2 8
#define BADC1 7
#define SADC4 6
#define SADC3 5
#define SADC2 4
#define SADC1 3
#define MODE3 2
#define MODE2 1
#define MODE1 0
// default values
#define D_I2C_ADDRESS 0x40 // (64)
#define D_RANGE 1
#define D_GAIN 3
#define D_SHUNT_ADC 3
#define D_BUS_ADC 3
#define D_MODE 7
#define D_SHUNT 0.25
#define D_V_BUS_MAX 6
#define D_V_SHUNT_MAX 0.3
#define D_I_MAX_EXPECTED 1
class INA219
{
public:
INA219();
// by default uses addr = 0x40 (both a-pins tied low)
void begin(uint8_t addr = D_I2C_ADDRESS);
void calibrate(float r_shunt = D_SHUNT, float v_shunt_max = D_V_SHUNT_MAX, float v_bus_max = D_V_BUS_MAX, float i_max_expected = D_I_MAX_EXPECTED);
void configure(uint8_t range = D_RANGE, uint8_t gain = D_GAIN, uint8_t bus_adc = D_BUS_ADC, uint8_t shunt_adc = D_SHUNT_ADC, uint8_t mode = D_MODE);
void reset();
int16_t shuntVoltageRaw();
int16_t busVoltageRaw();
float shuntVoltage();
float busVoltage();
float shuntCurrent();
float busPower();
private:
uint8_t i2c_address;
float r_shunt, current_lsb, power_lsb;
uint16_t config, cal, gain;
int16_t read16(uint8_t addr);
void write16(uint8_t addr, uint16_t data);
};
#endif