-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathSDL_Arduino_INA3221.cpp
181 lines (152 loc) · 5.6 KB
/
SDL_Arduino_INA3221.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
//
// SDL_Arduino_INA3221 Library
// SDL_Arduino_INA3221.cpp Arduino code - runs in continuous mode
// Version 1.1
// SwitchDoc Labs January 31, 2015
//
//
/*
Initial code from INA219 code (Basically just a core structure left)
@author K.Townsend (Adafruit Industries)
@license BSD (see BSDlicense.txt)
*/
#if ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#include <Wire.h>
#include "SDL_Arduino_INA3221.h"
/**************************************************************************/
/*!
@brief Sends a single command byte over I2C
*/
/**************************************************************************/
void SDL_Arduino_INA3221::wireWriteRegister (uint8_t reg, uint16_t value)
{
Wire.beginTransmission(INA3221_i2caddr);
#if ARDUINO >= 100
Wire.write(reg); // Register
Wire.write((value >> 8) & 0xFF); // Upper 8-bits
Wire.write(value & 0xFF); // Lower 8-bits
#else
Wire.send(reg); // Register
Wire.send(value >> 8); // Upper 8-bits
Wire.send(value & 0xFF); // Lower 8-bits
#endif
Wire.endTransmission();
}
/**************************************************************************/
/*!
@brief Reads a 16 bit values over I2C
*/
/**************************************************************************/
void SDL_Arduino_INA3221::wireReadRegister(uint8_t reg, uint16_t *value)
{
Wire.beginTransmission(INA3221_i2caddr);
#if ARDUINO >= 100
Wire.write(reg); // Register
#else
Wire.send(reg); // Register
#endif
Wire.endTransmission();
delay(1); // Max 12-bit conversion time is 586us per sample
Wire.requestFrom(INA3221_i2caddr, (uint8_t)2);
#if ARDUINO >= 100
// Shift values to create properly formed integer
*value = ((Wire.read() << 8) | Wire.read());
#else
// Shift values to create properly formed integer
*value = ((Wire.receive() << 8) | Wire.receive());
#endif
}
//
void SDL_Arduino_INA3221::INA3221SetConfig(void)
{
// Set Config register to take into account the settings above
uint16_t config = INA3221_CONFIG_ENABLE_CHAN1 |
INA3221_CONFIG_ENABLE_CHAN2 |
INA3221_CONFIG_ENABLE_CHAN3 |
INA3221_CONFIG_AVG1 |
INA3221_CONFIG_VBUS_CT2 |
INA3221_CONFIG_VSH_CT2 |
INA3221_CONFIG_MODE_2 |
INA3221_CONFIG_MODE_1 |
INA3221_CONFIG_MODE_0;
wireWriteRegister(INA3221_REG_CONFIG, config);
}
/**************************************************************************/
/*!
@brief Instantiates a new SDL_Arduino_INA3221 class
*/
/**************************************************************************/
SDL_Arduino_INA3221::SDL_Arduino_INA3221(uint8_t addr, float shuntresistor) {
INA3221_i2caddr = addr;
INA3221_shuntresistor = shuntresistor;
}
/**************************************************************************/
/*!
@brief Setups the HW (defaults to 32V and 2A for calibration values)
*/
/**************************************************************************/
void SDL_Arduino_INA3221::begin() {
Wire.begin();
// Set chip to known config values to start
INA3221SetConfig();
Serial.print("shut resistor="); Serial.println(INA3221_shuntresistor);
Serial.print("address="); Serial.println(INA3221_i2caddr);
}
/**************************************************************************/
/*!
@brief Gets the raw bus voltage (16-bit signed integer, so +-32767)
*/
/**************************************************************************/
int16_t SDL_Arduino_INA3221::getBusVoltage_raw(int channel) {
uint16_t value;
wireReadRegister(INA3221_REG_BUSVOLTAGE_1+(channel -1) *2, &value);
// Serial.print("BusVoltage_raw=");
// Serial.println(value,HEX);
// Shift to the right 3 to drop CNVR and OVF and multiply by LSB
return (int16_t)(value );
}
/**************************************************************************/
/*!
@brief Gets the raw shunt voltage (16-bit signed integer, so +-32767)
*/
/**************************************************************************/
int16_t SDL_Arduino_INA3221::getShuntVoltage_raw(int channel) {
uint16_t value;
wireReadRegister(INA3221_REG_SHUNTVOLTAGE_1+(channel -1) *2, &value);
// Serial.print("ShuntVoltage_raw=");
// Serial.println(value,HEX);
return (int16_t)value;
}
/**************************************************************************/
/*!
@brief Gets the shunt voltage in mV (so +-168.3mV)
*/
/**************************************************************************/
float SDL_Arduino_INA3221::getShuntVoltage_mV(int channel) {
int16_t value;
value = getShuntVoltage_raw(channel);
return value * 0.005;
}
/**************************************************************************/
/*!
@brief Gets the shunt voltage in volts
*/
/**************************************************************************/
float SDL_Arduino_INA3221::getBusVoltage_V(int channel) {
int16_t value = getBusVoltage_raw(channel);
return value * 0.001;
}
/**************************************************************************/
/*!
@brief Gets the current value in mA, taking into account the
config settings and current LSB
*/
/**************************************************************************/
float SDL_Arduino_INA3221::getCurrent_mA(int channel) {
float valueDec = getShuntVoltage_mV(channel)/INA3221_shuntresistor;
return valueDec;
}