-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathIVT-S_monitor.ino
66 lines (52 loc) · 2.24 KB
/
IVT-S_monitor.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
// IVT-S meter using CAN-BUS Shield
// electric_dart 2020
#include <SPI.h>
#include "mcp_can.h"
// the cs pin of the version after v1.1 is default to D9
// v0.9b and v1.0 is default D10
const int SPI_CS_PIN = 10;
MCP_CAN CAN(SPI_CS_PIN); // Set CS pin
unsigned char len = 0;
unsigned char buf[8];
void setup() {
// Set your Serial Monitor to 115200 baud
Serial.begin(115200);
// Initialise the CAN bus
while (CAN_OK != CAN.begin(CAN_1000KBPS)) { // init can bus
Serial.println("Failed to initialise CAN bus. Retrying...");
delay(100);
}
Serial.println("CAN bus inititalised.");
/*
set receive mask
*/
CAN.init_Mask(0, 0, 0x7ff); // there are 2 masks in mcp2515, you need to set both of them
CAN.init_Mask(1, 0, 0x7ff); // 0x7ff is '11111111111' in binary, so we are checking 11 of the CAN message ID bits
/*
set receive filter
*/
CAN.init_Filt(0, 0, 0x521); // there are 6 filters in mcp2515
CAN.init_Filt(1, 0, 0x521); // 0x521 is the CAN message ID for IVT-S Current value
CAN.init_Filt(2, 0, 0x521);
CAN.init_Filt(3, 0, 0x521);
CAN.init_Filt(4, 0, 0x521);
CAN.init_Filt(5, 0, 0x521);
}
void loop() {
if (CAN_MSGAVAIL == CAN.checkReceive()) { // check if data coming
CAN.readMsgBuf(&len, buf); // read data, len: data length, buf: data buf
unsigned long canId = CAN.getCanId();
if (canId == 0x521) {
Serial.print("Data received from IVT_Msg_Result_I");
Serial.print("\t");
// Convert individual big endian byte values to actual reading
long reading = (buf[2] << 24) | (buf[3] << 16) | (buf[4] << 8) | (buf[5]);
Serial.println(reading);
}
}
// Refresh every 250ms
delay(250);
}
/*********************************************************************************************************
END FILE
*********************************************************************************************************/