forked from alikian/ArduinoLib_MAX17048
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMAX17048.cpp
161 lines (111 loc) · 3.02 KB
/
MAX17048.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
// MAX17048/49 library for Arduino
//
// Created by Luca Dentella (http://www.lucadentella.it)
// Modified by Ali Kianzadeh
#include "MAX17048.h"
#include "Wire.h"
float MAX17048::getVCell() {
byte MSB = 0;
byte LSB = 0;
readRegister(VCELL_REGISTER, MSB, LSB);
int value = (MSB << 4) | (LSB >> 4);
return map(value, 0x000, 0xFFF, 0, 50000) / 10000.0;
//return value * 0.00125;
}
float MAX17048::getSoC() {
byte MSB = 0;
byte LSB = 0;
readRegister(SOC_REGISTER, MSB, LSB);
float decimal = LSB / 256.0;
return MSB + decimal;
}
int MAX17048::getVersion() {
byte MSB = 0;
byte LSB = 0;
readRegister(VERSION_REGISTER, MSB, LSB);
return (MSB << 8) | LSB;
}
byte MAX17048::getCompensateValue() {
byte MSB = 0;
byte LSB = 0;
readConfigRegister(MSB, LSB);
return MSB;
}
byte MAX17048::getAlertThreshold() {
byte MSB = 0;
byte LSB = 0;
readConfigRegister(MSB, LSB);
return 32 - (LSB & 0x1F);
}
void MAX17048::setAlertThreshold(byte threshold) {
byte MSB = 0;
byte LSB = 0;
readConfigRegister(MSB, LSB);
if(threshold > 32) threshold = 32;
threshold = 32 - threshold;
writeRegister(CONFIG_REGISTER, MSB, (LSB & 0xE0) | threshold);
}
boolean MAX17048::inAlert() {
byte MSB = 0;
byte LSB = 0;
readConfigRegister(MSB, LSB);
return LSB & 0x20;
}
void MAX17048::clearAlert() {
byte MSB = 0;
byte LSB = 0;
readConfigRegister(MSB, LSB);
}
// HibThr UNIT: %/hr, ActThr UNIT: mV
void MAX17048::getHibernateConfig(float &hibThrPercentPerHour, float &actThr_mV) {
byte HibThr = 0;
byte ActThr = 0;
readRegister(HIBRT_REGISTER, HibThr, ActThr);
// 0.208%/hr Unit
hibThrPercentPerHour = HibThr * 0.208f;
// 1.25mV Unit
actThr_mV = ActThr * 1.25f;
}
// 53.04%/hr, 318.75mV (or bigger) = always hibernate (maximums)
void MAX17048::setHibernateConfig(float hibThrPercentPerHour, float actThr_mV) {
byte HibThr = (byte) min(max(roundf(hibThrPercentPerHour / 0.208f),0), 255);
byte ActThr = (byte) min(max(roundf(actThr_mV / 1.25f),0), 255);
writeRegister(HIBRT_REGISTER, HibThr, ActThr);
}
boolean MAX17048::inHibernate() {
byte MSB = 0;
byte LSB = 0;
readRegister(MODE_REGISTER, MSB, LSB);
return MSB & 0x10;
}
// Approximate charge or discharge rate of the battery in %/h
float MAX17048::getCrate() {
byte MSB = 0;
byte LSB = 0;
readRegister(CRATE_REGISTER, MSB, LSB);
return ((MSB << 8) | LSB) * 0.208;
}
void MAX17048::reset() {
writeRegister(COMMAND_REGISTER, 0x00, 0x54);
}
void MAX17048::quickStart() {
writeRegister(MODE_REGISTER, 0x40, 0x00);
}
void MAX17048::readConfigRegister(byte &MSB, byte &LSB) {
readRegister(CONFIG_REGISTER, MSB, LSB);
}
void MAX17048::readRegister(byte startAddress, byte &MSB, byte &LSB) {
Wire.beginTransmission(MAX17048_ADDRESS);
Wire.write(startAddress);
Wire.endTransmission();
Wire.requestFrom(MAX17048_ADDRESS, 2);
MSB = Wire.read();
LSB = Wire.read();
}
void MAX17048::writeRegister(byte address, byte MSB, byte LSB) {
Wire.beginTransmission(MAX17048_ADDRESS);
Wire.write(address);
Wire.write(MSB);
Wire.write(LSB);
Wire.endTransmission();
}