-
Notifications
You must be signed in to change notification settings - Fork 4
/
Thanos_INA260.cpp
180 lines (156 loc) · 5.37 KB
/
Thanos_INA260.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
/**************************************************************************/
/*!
@file Thanos_INA260.cpp
@author Thanos Kontogiannis
@license BSD (see license.txt)
Driver for the INA260 current sensor
This is a library for the INA260
@section HISTORY
v1.0 - First release
*/
/**************************************************************************/
#if ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#include <Wire.h>
#include "Thanos_INA260.h"
/**************************************************************************/
/*!
@brief Sends a single command byte over I2C
*/
/**************************************************************************/
void Thanos_INA260::wireWriteRegister (uint8_t reg, uint16_t value)
{
Wire.beginTransmission(ina260_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 Thanos_INA260::wireReadRegister(uint8_t reg, uint16_t *value)
{
Wire.beginTransmission(ina260_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(ina260_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
}
/**************************************************************************/
/*!
@brief Configures to INA260
*/
/**************************************************************************/
void Thanos_INA260::setConfigRegister(void)
{
// Sets 4 samples average and sampling time for voltage and current to 8.244ms
// Set Config register
uint16_t config = INA260_CONFIG_AVGRANGE_4 |
INA260_CONFIG_BVOLTAGETIME_8244US |
INA260_CONFIG_SCURRENTTIME_8244US |
INA260_CONFIG_MODE_SANDBVOLT_CONTINUOUS;
wireWriteRegister(INA260_REG_CONFIG, config);
}
/**************************************************************************/
/*!
@brief Instantiates a new INA260 class
*/
/**************************************************************************/
Thanos_INA260::Thanos_INA260(uint8_t addr) {
ina260_i2caddr = addr;
}
/**************************************************************************/
/*!
@brief Setups the HW
*/
/**************************************************************************/
void Thanos_INA260::begin(uint8_t addr) {
ina260_i2caddr = addr;
begin();
}
void Thanos_INA260::begin(void) {
Wire.begin();
// Set chip to large range config values to start
setConfigRegister();
}
/**************************************************************************/
/*!
@brief Gets the raw bus voltage (16-bit signed integer, so +-32767)
*/
/**************************************************************************/
int16_t Thanos_INA260::getBusVoltage_raw() {
uint16_t value;
wireReadRegister(INA260_REG_BUSVOLTAGE, &value);
return (int16_t)value;
}
/**************************************************************************/
/*!
@brief Gets the raw shunt voltage (16-bit signed integer, so +-32767)
*/
/**************************************************************************/
int16_t Thanos_INA260::getShuntVoltage_raw() {
uint16_t value;
wireReadRegister(INA260_REG_SHUNTVOLTAGE, &value);
return (int16_t)value;
}
/**************************************************************************/
/*!
@brief Gets the raw current value (16-bit signed integer, so +-32767)
*/
/**************************************************************************/
int16_t Thanos_INA260::getCurrent_raw() {
uint16_t value;
wireReadRegister(INA260_REG_CURRENT, &value);
return (int16_t)value;
}
/**************************************************************************/
/*!
@brief Gets the shunt voltage in mV
*/
/**************************************************************************/
float Thanos_INA260::getShuntVoltage_mV() {
int16_t value;
value = getShuntVoltage_raw();
return value * 0.007;
}
/**************************************************************************/
/*!
@brief Gets the shunt voltage in volts
*/
/**************************************************************************/
float Thanos_INA260::getBusVoltage_V() {
int16_t value = getBusVoltage_raw();
return value * 0.00125;
}
/**************************************************************************/
/*!
@brief Gets the current value in mA
*/
/**************************************************************************/
float Thanos_INA260::getCurrent_mA() {
float valueDec = getCurrent_raw();
return valueDec * 1.25;
}