-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathbq769x0.h
159 lines (119 loc) · 4.78 KB
/
bq769x0.h
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
/*
bq769x0.h - Battery management system based on bq769x0 for Arduino
Copyright (C) 2015 Martin Jäger (m.jaeger@posteo.de)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this program. If not, see
<http://www.gnu.org/licenses/>.
*/
#ifndef BQ769X0_H
#define BQ769X0_H
#include <Arduino.h>
#include <Wire.h>
// can be reduced to save some memory if smaller ICs are used
#define MAX_NUMBER_OF_CELLS 15
#define MAX_NUMBER_OF_THERMISTORS 3
// IC type/size
#define bq76920 1
#define bq76930 2
#define bq76940 3
// output information to serial console for debugging
#define BQ769X0_DEBUG 1
class bq769x0 {
public:
// initialization, status update and shutdown
bq769x0(byte bqType = bq76920, int bqI2CAddress = 0x18);
int begin(byte alertPin, byte bootPin = -1);
int checkStatus(); // returns 0 if everything is OK
void update(void);
void shutdown(void);
// charging control
bool enableCharging(void);
void disableCharging(void);
bool enableDischarging(void);
void disableDischarging(void);
// hardware settings
void setShuntResistorValue(int res_mOhm);
void setThermistorBetaValue(int beta_K);
// limit settings (for battery protection)
void setTemperatureLimits(int minDischarge_degC, int maxDischarge_degC, int minCharge_degC, int maxCharge_degC); // °C
long setShortCircuitProtection(long current_mA, int delay_us = 70);
long setOvercurrentChargeProtection(long current_mA, int delay_ms = 8);
long setOvercurrentDischargeProtection(long current_mA, int delay_ms = 8);
int setCellUndervoltageProtection(int voltage_mV, int delay_s = 1);
int setCellOvervoltageProtection(int voltage_mV, int delay_s = 1);
// balancing settings
void setBalancingThresholds(int idleTime_min = 30, int absVoltage_mV = 3400, byte voltageDifference_mV = 20);
void setIdleCurrentThreshold(int current_mA);
// automatic balancing when battery is within balancing thresholds
void enableAutoBalancing(void);
void disableAutoBalancing(void);
// battery status
int getBatteryCurrent(void);
int getBatteryVoltage(void);
int getCellVoltage(byte idCell); // from 1 to 15
int getMinCellVoltage(void);
int getMaxCellVoltage(void);
float getTemperatureDegC(byte channel = 1);
float getTemperatureDegF(byte channel = 1);
// interrupt handling (not to be called manually!)
void setAlertInterruptFlag(void);
#if BQ769X0_DEBUG
void printRegisters(void);
#endif
private:
// Variables
int I2CAddress;
byte type;
byte shuntResistorValue_mOhm;
int thermistorBetaValue = 3435; // typical value for Semitec 103AT-5 thermistor
// indicates if a new current reading or an error is available from BMS IC
bool alertInterruptFlag = true; // init with true to check and clear errors at start-up
int numberOfCells;
int cellVoltages[MAX_NUMBER_OF_CELLS]; // mV
byte idCellMaxVoltage;
byte idCellMinVoltage;
long batVoltage; // mV
long batCurrent; // mA
int temperatures[MAX_NUMBER_OF_THERMISTORS]; // °C/10
// Current limits (mA)
long maxChargeCurrent;
long maxDischargeCurrent;
int idleCurrentThreshold = 30; // mA
// Temperature limits (°C/10)
int minCellTempCharge;
int minCellTempDischarge;
int maxCellTempCharge;
int maxCellTempDischarge;
// Cell voltage limits (mV)
int maxCellVoltage;
int minCellVoltage;
int balancingMinCellVoltage_mV;
byte balancingMaxVoltageDifference_mV;
int adcGain; // uV/LSB
int adcOffset; // mV
int errorStatus = 0;
bool autoBalancingEnabled = false;
bool balancingActive = false;
int balancingMinIdleTime_s = 1800; // default: 30 minutes
unsigned long idleTimestamp = 0;
unsigned int secSinceErrorCounter = 0;
unsigned long interruptTimestamp = 0;
static bq769x0* instancePointer;
// Methods
static void alertISR(void);
void updateVoltages(void);
void updateCurrent(bool ignoreCCReadyFlag = false);
void updateTemperatures(void);
byte updateBalancingSwitches(void);
int readRegister(byte address);
void writeRegister(byte address, int data);
};
#endif // BQ769X0_H