-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBMS_v1.ino
121 lines (103 loc) · 3.28 KB
/
BMS_v1.ino
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
#include <Wire.h>
#include "SSD1306Ascii.h"
#include "SSD1306AsciiWire.h"
#include "globals.h"
#include "declares.h"
#include "display.h"
#include "settings.h"
#include "bq769x0.h" // Library for Texas Instruments bq76920 battery management IC
SSD1306AsciiWire oled;
userSettings settings;
long internalVCC; // A global variable for storing the internal voltage reference of the 1.1V rail of the chip.
void setup()
{
internalVCC = readVcc(); // A global variable for storing the internal voltage reference of the 1.1V rail of the chip.
Serial.begin(115200);
Wire.begin();
Wire.setClock(400000L);
pinMode(LED_BUILTIN, OUTPUT);
int err = bmsBegin();
setupDisplay();
setDefaults();
PrintMenu();
}
void loop() {
unsigned long currentTimer = millis();
static unsigned long bmsUpdateLast;
static unsigned long serialLastUpdate;
static long previousBlink;
static bool blinkOn;
if (currentTimer - bmsUpdateLast > settings.bmsRefreshInt)
{
bmsUpdate(); // should be called at least every 250 ms
bmsUpdateLast = currentTimer;
}
if (currentTimer - previousBlink > 1000)
{
if (blinkOn == false)
{
digitalWrite(LED_BUILTIN, HIGH);
}
else if (blinkOn == true)
{
digitalWrite(LED_BUILTIN, LOW);
}
blinkOn = !blinkOn;
}
if (currentTimer - serialLastUpdate > settings.displayStatusRefresh)
{
//BMS.printRegisters();
Serial.print(F("Cell Voltages: "));
for (int i = 1; i <= 5; i++)
{
Serial.print(String(getCellVoltage(i))+ " ");
}
Serial.println();
serialLastUpdate = currentTimer;
}
readSerial(); // Check serial input for commands
}
void setDefaults()
{
setBMSDefaults();
settings.displayStatusRefresh = 5000;
settings.bmsRefreshInt = 250; // Later we will pull this from a user stored variable
settings.oledDisplay = DISPLAYSTATUS;
}
#ifdef __arm__
// should use uinstd.h to define sbrk but Due causes a conflict
extern "C" char* sbrk(int incr);
#else // __ARM__
extern char *__brkval;
#endif // __arm__
int freeMemory() {
char top;
#ifdef __arm__
return &top - reinterpret_cast<char*>(sbrk(0));
#elif defined(CORE_TEENSY) || (ARDUINO > 103 && ARDUINO != 151)
return &top - __brkval;
#else // __arm__
return __brkval ? &top - __brkval : &top - __malloc_heap_start;
#endif // __arm__
}
long readVcc() {
// Read 1.1V reference against AVcc
// set the reference to Vcc and the measurement to the internal 1.1V reference
#if defined(__AVR_ATmega32U4__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
ADMUX = _BV(REFS0) | _BV(MUX4) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
#elif defined (__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__)
ADMUX = _BV(MUX5) | _BV(MUX0);
#elif defined (__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__)
ADMUX = _BV(MUX3) | _BV(MUX2);
#else
ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
#endif
delay(2); // Wait for Vref to settle
ADCSRA |= _BV(ADSC); // Start conversion
while (bit_is_set(ADCSRA, ADSC)); // measuring
uint8_t low = ADCL; // must read ADCL first - it then locks ADCH
uint8_t high = ADCH; // unlocks both
long result = (high << 8) | low;
result = 1125300L / result; // Calculate Vcc (in mV); 1125300 = 1.1*1023*1000
return result; // Vcc in millivolts
}