-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathina219.cpp
113 lines (101 loc) · 2.65 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
/*
* Data Sheet: http://www.ti.com/lit/ds/symlink/ina219.pdf
*/
#include "include/TinyWireM.h"
#include "include/ina219.h"
INA219::INA219(void) {}
void write_register(uint8_t reg_addr, uint8_t upper, uint8_t lower)
{
TinyWireM.beginTransmission(INA219_I2C_ADDR);
TinyWireM.send(reg_addr);
TinyWireM.send(upper);
TinyWireM.send(lower);
TinyWireM.endTransmission();
}
uint16_t read_register(uint8_t reg_addr)
{
TinyWireM.beginTransmission(INA219_I2C_ADDR);
TinyWireM.send(reg_addr);
TinyWireM.endTransmission();
#ifdef WAIT_CONVERSION
WAIT_CONVERSION; // wait conversion time
#endif
TinyWireM.requestFrom(INA219_I2C_ADDR, 2);
return ((TinyWireM.read() << 8) | TinyWireM.read());
}
static bool high_value_mode;
void INA219::begin(void)
{
high_value_mode = false;
write_register(CONFIG_REGISTER_ADDR, CONFIG_UPPER, CONFIG_LOWER);
write_register(CALIBRATION_REGISTER_ADDR, CALIBRATION_UPPER, CALIBRATION_LOWER);
}
void INA219::to_high_value_mode(void)
{
high_value_mode = true;
write_register(CONFIG_REGISTER_ADDR, CONFIG_UPPER_3A, CONFIG_LOWER);
write_register(CALIBRATION_REGISTER_ADDR, CALIBRATION_UPPER_3A, CALIBRATION_LOWER_3A);
}
bool INA219::is_high_value_mode(void)
{
return high_value_mode;
}
/*
* Shunt Voltage Register (address = 01h)
* Data Sheet Page 23 Figure 20
*/
float INA219::read_shunt_voltage(void)
{
return ((int16_t)read_register(SHUNT_VOLTAGE_REGISTER_ADDR)) * 0.00001; // Shunt voltage
}
/*
* Bus Voltage Register (address = 02h)
* Data Sheet Page 23 Figure 24
*/
float INA219::read_bus_voltage(void)
{
int16_t value = read_register(BUS_VOLTAGE_REGISTER_ADDR);
if ((!high_value_mode) && (value & 0x01)) // overflow
{
to_high_value_mode();
value = read_register(BUS_VOLTAGE_REGISTER_ADDR);
}
if (value & 0x01) // overflow
{
return 99.99;
}
return (value >> 3) * 0.004; // shift Bits 3-15 and LSB = 4 mV
}
/*
* Power Register (address = 03h)
* Data Sheet Page 23 Figure 25
*/
float INA219::read_power(void)
{
if (high_value_mode)
{
return read_register(POWER_REGISTER_ADDR) * CURRENT_LSB_3A * POWER_CONSTANT;
}
return read_register(POWER_REGISTER_ADDR) * CURRENT_LSB * POWER_CONSTANT;
}
/*
* Current Register (address = 04h)
* Data Sheet Page 23 Figure 26
*/
float INA219::read_current(void)
{
if (high_value_mode)
{
return ((int16_t)read_register(CURRENT_REGISTER_ADDR)) * CURRENT_LSB_3A;
}
else
{
float current = ((int16_t)read_register(CURRENT_REGISTER_ADDR)) * CURRENT_LSB;
if (current > DETECT_HIGH_CURRENT_THRESHOLD)
{
to_high_value_mode();
current = ((int16_t)read_register(CURRENT_REGISTER_ADDR)) * CURRENT_LSB_3A;
}
return current;
}
}