-
Notifications
You must be signed in to change notification settings - Fork 0
/
STC3105.cpp
229 lines (175 loc) · 4.36 KB
/
STC3105.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
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
#include "STC3105.h"
STC3105::STC3105(uint8_t address, float sense_resistor)
{
this->devAddr = address;
this->sense_resistor = sense_resistor;
voltage = 0;
current = 0;
}
bool STC3105::testConnection()
{
uint8_t rx;
int8_t status = I2Cdev::readByte(devAddr, STC3105_REG_ID, &rx);
return (status > 0) ? true : false;
}
void STC3105::initialize()
{
enableOperatingMode();
}
// MODE REG functions
void STC3105::enableOperatingMode(bool enable)
{
I2Cdev::writeBit(devAddr, STC3105_REG_MODE, 4, enable ? 1 : 0);
}
void STC3105::enablePowerSaving(bool enable)
{
I2Cdev::writeBit(devAddr, STC3105_REG_MODE, 2, enable ? 1 : 0);
}
void STC3105::enableAlarm(bool enable)
{
I2Cdev::writeBit(devAddr, STC3105_REG_MODE, 3, enable ? 1 : 0);
}
// CTRL REG functions
// Alarm
void STC3105::enableAlarmPin(bool enable)
{
I2Cdev::writeBit(devAddr, STC3105_REG_CTRL, 1, enable ? 1 : 0);
}
bool STC3105::checkAlarmPin()
{
uint8_t data = 0;
I2Cdev::readBit(devAddr, STC3105_REG_CTRL, 1, &data);
return data ? true : false;
}
bool STC3105::checkLowVoltageReg()
{
uint8_t data = 0;
I2Cdev::readBit(devAddr, STC3105_REG_CTRL, 6, &data);
return data ? true : false;
}
void STC3105::clearLowVoltageReg()
{
I2Cdev::writeBit(devAddr, STC3105_REG_CTRL, 6, 0);
}
bool STC3105::checkLowSOCReg()
{
uint8_t data = 0;
I2Cdev::readBit(devAddr, STC3105_REG_CTRL, 5, &data);
return data ? true : false;
}
void STC3105::clearLowSOCReg()
{
I2Cdev::writeBit(devAddr, STC3105_REG_CTRL, 5, 0);
}
void STC3105::reset()
{
I2Cdev::writeBit(devAddr, STC3105_REG_CTRL, 1, 0);
}
void STC3105::soft_reset(bool enable)
{
I2Cdev::writeBit(devAddr, STC3105_REG_CTRL, 4, enable ? 1 : 0);
}
bool STC3105::readPowerOnReset()
{
uint8_t data = 0;
I2Cdev::readBit(devAddr, STC3105_REG_CTRL, 4, &data);
return data ? true : false;
}
// Updated? functions
bool STC3105::voltageUpdated()
{
uint8_t data = 0;
I2Cdev::readBit(devAddr, STC3105_REG_CTRL, 3, &data);
return data ? true : false;
}
bool STC3105::currentUpdated()
{
uint8_t data = 0;
I2Cdev::readBit(devAddr, STC3105_REG_CTRL, 2, &data);
return data ? true : false;
}
// Battery voltage relax counter functions
int STC3105::readRelaxCounter()
{
uint8_t data = 0;
I2Cdev::readByte(devAddr, STC3105_REG_RELAX_COUNT, &data);
return data;
}
int STC3105::readRelaxCounterCurrentThreshold()
{
uint8_t data = 0;
I2Cdev::readByte(devAddr, STC3105_REG_CURRENT_THRES, &data);
return data;
}
void STC3105::setRelaxCounterCurrentThreshold(uint8_t data)
{
I2Cdev::writeByte(devAddr, STC3105_REG_CURRENT_THRES, data);
}
// DATA functions
float STC3105::readVoltage()
{
uint8_t data[2] = {0, 0}; // TO READ
int vlt = 0;
I2Cdev::readBytes(devAddr, STC3105_REG_VOLTAGE_LOW, 2, data);
vlt = data[1] << 8 | data[0];
voltage = 0.00244 * (float)vlt;
return voltage;
}
float STC3105::readCurrent()
{
uint8_t data[2] = {0, 0}; // TO READ
uint16_t vlt = 0;
float voltage_drop;
I2Cdev::readBytes(devAddr, STC3105_REG_CURRENT_LOW, 2, data);
vlt = data[1] << 8 | data[0]; // The current data is in two's complement code
vlt = ~vlt + 1; // binary code now
// on the sense resistor
voltage_drop = 0.000001 * 11.77 * (float)vlt;
return current = voltage_drop / sense_resistor;
}
float STC3105::readStateOfCharge()
{
uint8_t data[2] = {0, 0}; // TO READ
uint16_t vlt = 0;
float voltage_drop;
I2Cdev::readBytes(devAddr, STC3105_REG_CHARGE_LOW, 2, data);
vlt = data[1] << 8 | data[0]; // The voltage drop data is in two's complement code
vlt = ~vlt + 1; // binary code now
voltage_drop = 0.000001 * 6.7 * float(vlt);
return state_of_charge = voltage_drop / sense_resistor;
}
int STC3105::readNumberOfConversions()
{
uint8_t data[2] = {0, 0}; // TO READ
I2Cdev::readBytes(devAddr, STC3105_REG_COUNTER_LOW, 2, data);
return number_of_conversions = data[1] << 8 | data[0];
}
uint16_t STC3105::readStateOfChargeBase()
{
uint8_t data[2] = {0, 0}; // TO READ
I2Cdev::readBytes(devAddr, STC3105_REG_SOC_BASE_LOW, 2, data);
return data[1] << 8 | data[0];
}
void STC3105::setStateOfChargeBase(uint16_t data)
{
uint8_t d[2] = {0, 0}; // To write
d[0] = data;
d[1] = data >> 8;
I2Cdev::writeBytes(devAddr, STC3105_REG_SOC_BASE_LOW, 2, d);
}
float STC3105::getVoltage()
{
return voltage;
}
float STC3105::getCurrent()
{
return current;
}
float STC3105::getStateOfCharge()
{
return state_of_charge;
}
int STC3105::getNumberOfConversions()
{
return number_of_conversions;
}