-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathLTC2941.cpp
More file actions
243 lines (182 loc) · 6.09 KB
/
LTC2941.cpp
File metadata and controls
243 lines (182 loc) · 6.09 KB
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*
A library for Grove - Coulomb Counter for 3.3V to 5V (LTC2941)
Copyright (c) 2018 seeed technology co., ltd.
Author : Wayen Weng
Create Time : June 2018
Change Log :
The MIT License (MIT)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include "LTC2941.h"
const float LTC2941_CHARGE_LSB = 0.085;
const uint8_t prescalarTable[8] = {1, 2, 4, 8, 16, 32, 64, 128};
LTC2941::LTC2941(void) {
resistor = 0.050;
prescalar = 128;
powerMaxAMh = LTC2941_BATTERY_MAX;
powerExpend = 0;
devAddr = LINEAR_LTC2941_ADDRESS;
}
void LTC2941::initialize(void) {
setBatteryAlert(VBAT_ALERT_OFF);
setAlertConfig(ALERT_DISABLED);
setPrescaler(PRESCALAR_M_128);
setAccumulatedCharge(0xffff);
}
void LTC2941::setBatteryFullMAh(uint16_t mAh, bool flag) {
uint8_t i = 0;
float qLSB = 0, temp = mAh;
if (mAh > LTC2941_BATTERY_MAX) {
return;
}
qLSB = temp / 65536;
for (i = 0; i < 8; i ++) {
temp = (LTC2941_CHARGE_LSB * prescalarTable[i] * 0.05) / (resistor * 128);
if (qLSB <= temp) {
break;
}
}
if (i >= 8) {
return;
}
powerMaxAMh = mAh;
prescalar = prescalarTable[i];
qLSB = (LTC2941_CHARGE_LSB * prescalar * 0.05) / (resistor * 128);
temp = mAh;
temp = temp / qLSB;
mAh = (uint16_t)temp;
if (flag) {
setAccumulatedCharge(mAh);
}
}
void LTC2941::setBatteryAlert(LTC2941_VBAT_ALERT voltage) {
updateReg(LTC2941_CONTROL_REG, LTC2941_VBAT_ALERT_MASK, MEASURE_VBAT_ALERT_POS, voltage);
}
void LTC2941::setPrescaler(LTC2941_PRESCALAR prescale) {
prescalar = prescalarTable[prescale];
updateReg(LTC2941_CONTROL_REG, LTC2941_PRESCALAR_MASK, MEASURE_PRESCALAR_POS, prescale);
}
void LTC2941::setAlertConfig(LTC2941_ALERT_CONF config) {
updateReg(LTC2941_CONTROL_REG, LTC2941_ALERT_MODE_MASK, MEASURE_ALERT_MODE_POS, config);
}
void LTC2941::setShutdown(bool enable) {
updateReg(LTC2941_CONTROL_REG, LTC2941_SHUTDOWN_MASK, MEASURE_SHUTDOWN_POS, enable);
}
void LTC2941::setAccumulatedCharge(uint16_t thresh) {
write16(LTC2941_ACCUM_CHARGE_MSB_REG, thresh);
}
void LTC2941::setChargeThresholdHigh(uint16_t thresh) {
write16(LTC2941_CHARGE_THRESH_HIGH_MSB_REG, thresh);
}
void LTC2941::setChargeThresholdLow(uint16_t thresh) {
write16(LTC2941_CHARGE_THRESH_LOW_MSB_REG, thresh);
}
uint8_t LTC2941::getStatus(void) {
return read8(LTC2941_STATUS_REG);
}
float LTC2941::getCoulombs(void) {
uint16_t data = 0;
float coulomb = 0;
data = read16(LTC2941_ACCUM_CHARGE_MSB_REG);
coulomb = (float)(data * LTC2941_CHARGE_LSB * prescalar * 0.05) / (resistor * 128);
coulomb = coulomb * 3.6f;
return coulomb;
}
float LTC2941::getmAh(void) {
uint16_t data = 0;
float mAh = 0;
data = read16(LTC2941_ACCUM_CHARGE_MSB_REG);
mAh = (float)(data * LTC2941_CHARGE_LSB * prescalar * 0.05) / (resistor * 128);
return mAh;
}
float LTC2941::getPercent(void) {
float mAh = getmAh();
return (mAh * 100 / powerMaxAMh);
}
float LTC2941::getCoulombsExpend(void) {
uint16_t data = 0;
float coulomb = 0;
data = 0xffff - read16(LTC2941_ACCUM_CHARGE_MSB_REG);
if (data == 0xffff) {
data = 0;
} else if (data >= 0xf000) {
powerExpend += 0xf000;
data -= 0xf000;
setAccumulatedCharge(0xffff - data);
}
coulomb = (float)((data + powerExpend) * LTC2941_CHARGE_LSB * prescalar * 0.05) / (resistor * 128);
coulomb = coulomb * 3.6f;
return coulomb;
}
float LTC2941::getmAhExpend(void) {
uint16_t data = 0;
float mAh = 0;
data = 0xffff - read16(LTC2941_ACCUM_CHARGE_MSB_REG);
if (data == 0xffff) {
data = 0;
} else if (data >= 0xf000) {
powerExpend += 0xf000;
data -= 0xf000;
setAccumulatedCharge(0xffff - data);
}
mAh = (float)((data + powerExpend) * LTC2941_CHARGE_LSB * prescalar * 0.05) / (resistor * 128);
return mAh;
}
void LTC2941::write8(uint8_t reg, uint8_t val) {
Wire.beginTransmission(devAddr);
Wire.write(reg);
Wire.write(val);
Wire.endTransmission();
}
void LTC2941::write16(uint8_t reg, uint16_t val) {
Wire.beginTransmission(devAddr);
Wire.write(reg);
Wire.write(val >> 8);
Wire.write(val & 0xff);
Wire.endTransmission();
}
uint8_t LTC2941::read8(uint8_t reg) {
uint8_t data = 0;
Wire.beginTransmission(devAddr);
Wire.write(reg);
Wire.endTransmission(false);
Wire.requestFrom((int16_t)devAddr, 1);
while (Wire.available()) {
data = Wire.read();
}
return data;
}
uint16_t LTC2941::read16(uint8_t reg) {
uint16_t msb = 0, lsb = 0;
Wire.beginTransmission(devAddr);
Wire.write(reg);
Wire.endTransmission(false);
Wire.requestFrom((int16_t)devAddr, 2);
while (Wire.available()) {
msb = Wire.read();
lsb = Wire.read();
}
return (lsb | (msb << 8));
}
void LTC2941::updateReg(uint8_t reg, uint8_t mask, uint8_t shift, uint8_t val) {
uint8_t tmp;
tmp = read8(reg);
tmp &= mask;
tmp |= (val << shift) & ~mask;
write8(reg, tmp);
}
LTC2941 ltc2941;