Skip to content

Commit 8adebc9

Browse files
Added example of CAN receiver
1 parent 395c9cb commit 8adebc9

1 file changed

Lines changed: 45 additions & 0 deletions

File tree

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#include <SPI.h>
2+
3+
const int CAN_INT_PIN = 2;
4+
5+
#include <mcp2515.h>
6+
MCP2515 CAN(10);
7+
struct can_frame canMsg;
8+
9+
const uint8_t canId = 0x70; // TU USTAWIAMY CAN ID
10+
11+
void setup() {
12+
Serial.begin(9600);
13+
while(!Serial); // ZAKOMENTUJ JEŻELI NIE DEBUGUJESZ PRZEZ USB
14+
15+
while (!(CAN.reset() == MCP2515::ERROR_OK
16+
&& CAN.setBitrate(CAN_1000KBPS, MCP_16MHZ) == MCP2515::ERROR_OK
17+
&& CAN.setNormalMode() == MCP2515::ERROR_OK )) {
18+
19+
// OBSŁUGA BŁĘDU INICJALIZACJI KOMUNIKACJI CAN
20+
Serial.println("CAN ERROR");
21+
}
22+
23+
Serial.println("CAN OK");
24+
}
25+
26+
27+
void loop() {
28+
if (CAN.readMessage(&canMsg) == MCP2515::ERROR_OK) {
29+
if ((canMsg.can_id >> 5) == canId && (canMsg.can_id & 0b11111) == 0x0) {
30+
// OBSŁUGA ODBIORU DANYCH
31+
32+
Serial.print("Got ");
33+
Serial.print(canMsg.can_dlc);
34+
Serial.print("bytes: ");
35+
36+
for (int i = 0; i<canMsg.can_dlc; i++) {
37+
Serial.print(canMsg.data[i],HEX);
38+
Serial.print(" ");
39+
}
40+
41+
Serial.println();
42+
43+
}
44+
}
45+
}

0 commit comments

Comments
 (0)