-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBMS-Arduino-Pi-Arduino-Code.ino
58 lines (49 loc) · 1.05 KB
/
BMS-Arduino-Pi-Arduino-Code.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
//#include <SPI.h>
volatile bool received;
volatile byte Slavereceived,Slavesend;
volatile byte lsbVolt, msbVolt;
int battVolt;
int battPin = 2;
float analogConversion = 5.7;
void setup()
{
Serial.begin(9600);
analogReference(INTERNAL);
pinMode(MISO, OUTPUT); // Make MISO an output
SPCR |= _BV(SPE); // Turn on spi in slave mode
SPCR |= _BV(SPIE); // Turn on interrupts
received = false;
}
ISR (SPI_STC_vect)
{
Slavereceived = SPDR;
if (Slavereceived == 1) // First byte
{
SPDR = lsbVolt;
return;
}
else
{
SPDR = msbVolt;
received = true;
return;
}
}
void loop()
{
battVolt = (analogRead(battPin) / 10.24) * 1.1 * analogConversion;
lsbVolt = (battVolt & 0x00FF);
msbVolt = ((battVolt & 0xFF00) >>8);
Serial.println(msbVolt*256 + lsbVolt); // The real math
if (received)
{
Serial.print("Received: ");
Serial.println(Slavereceived);
received = false;
}
else
{
// Serial.println("Nothing Received");
delay(300);
}
}