Skip to content

Instantly share code, notes, and snippets.

@alexey-bass
Created January 21, 2021 07:21
Show Gist options
  • Save alexey-bass/0127f7781b4a0fa0614f5d142ee0864c to your computer and use it in GitHub Desktop.
Save alexey-bass/0127f7781b4a0fa0614f5d142ee0864c to your computer and use it in GitHub Desktop.
#include <SPI.h>
#include "mcp_can.h"
MCP_CAN CanBusShield(10); // Set CS pin: 10 for old, 9 for v1.2 and up
void setup() {
Serial.begin(500000);
Serial.println();
Serial.println();
while (CAN_OK != CanBusShield.begin(CAN_500KBPS)) {
Serial.println("CAN-BUS shield init failed, re-trying...");
delay(1000);
}
Serial.println("CAN-BUS shield init success");
Serial.println();
setIdFilterMask(0x300, 0x700);
Serial.println("PID DLC DATA ASCII");
Serial.println("----------------------------------------------");
}
unsigned char len = 0;
unsigned char buf[8];
char singleChar;
String asciiOutput;
unsigned long globalMask;
unsigned long globalFilter;
void setIdFilterMask(unsigned long filter, unsigned long mask) {
Serial.print("Setting CAN HW filter 0x");
Serial.print(filter, HEX);
Serial.print(" mask 0x");
Serial.println(mask, HEX);
Serial.print("0x");
Serial.print(mask, HEX);
Serial.print(" = 0");
Serial.println(mask, BIN);
Serial.print("0x");
Serial.print(filter, HEX);
Serial.print(" = 0");
Serial.println(filter, BIN);
globalMask = mask;
globalFilter = filter;
// 0-1 masks and 0-5 filters registers available
// need to set ALL masks and filters for correct filtering
CanBusShield.init_Mask(0, 0, mask);
CanBusShield.init_Mask(1, 0, mask);
CanBusShield.init_Filt(0, 0, filter);
CanBusShield.init_Filt(1, 0, filter);
CanBusShield.init_Filt(2, 0, filter);
CanBusShield.init_Filt(3, 0, filter);
CanBusShield.init_Filt(4, 0, filter);
CanBusShield.init_Filt(5, 0, filter);
Serial.println();
}
void loop() {
// receiveData();
sendData();
}
void sendData() {
unsigned char stmp[8] = {1, 2, 3, 4, 5, 6, 7, 8};
for (int i = 0x0; i < 0x800; i += 0x7F) {
for (int i = 0; i < 8; i++) { stmp[i] = random(0, 0xFF); }
CanBusShield.sendMsgBuf(i, 0, 8, stmp);
Serial.println("frame 0x"+ String(i, HEX) +" sent");
delay(100);
}
// for (int i = 0; i <= 0x300; i++) {
// CAN.sendMsgBuf(i, 0, 8, stmp);
// delay(10);
// }
}
void receiveData() {
if (CAN_MSGAVAIL == CanBusShield.checkReceive()) { // check if data coming
CanBusShield.readMsgBuf(&len, buf); // read data, len: data length, buf: data buf
unsigned long canId = CanBusShield.getCanId();
if (canId < 16) Serial.print("0");
if (canId < 256) Serial.print("0");
Serial.print(canId, HEX);
Serial.print(" ");
Serial.print(len);
Serial.print(" ");
asciiOutput = "";
for (int i = 0; i < 8; i++) {
if (i < len) {
if (buf[i] < 16) Serial.print("0");
Serial.print(buf[i], HEX);
Serial.print(" ");
singleChar = buf[i];
if (buf[i] >= 0x20 && buf[i] <= 0x7E) { // visible ASCII
asciiOutput += singleChar;
} else {
asciiOutput += " ";
}
} else {
Serial.print(" ");
}
}
Serial.print(" ");
Serial.print("'"+ asciiOutput);
if (asciiOutput.length() < 8) {
for (int i = asciiOutput.length(); i < 8; i++) {
Serial.print(" ");
}
}
Serial.print("'");
Serial.println();
// Serial.print("PID 0");
// Serial.println(canId, BIN);
// Serial.print("MSK 0");
// Serial.println(globalMask, BIN);
// Serial.print("P+M 0");
// Serial.println(canId & globalMask, BIN);
// Serial.print("FLT 0");
// Serial.println(globalFilter, BIN);
} else if (CAN_CTRLERROR == CanBusShield.checkError()) {
Serial.println("CAN-BUS error detected, check lines and speed");
delay(1000);
}
}
@alexey-bass
Copy link
Author

sendData()

CAN Sender

09:33:33.955 -> CAN-BUS shield init success
09:33:33.955 -> 
09:33:33.955 -> Setting CAN HW filter 0x300 mask 0x700
09:33:33.955 -> 0x700 = 011100000000
09:33:33.955 -> 0x300 = 01100000000
09:33:34.129 -> 
09:33:34.129 -> PID  DLC  DATA                       ASCII
09:33:34.129 -> ----------------------------------------------
09:33:34.129 -> frame 0x0 sent
09:33:34.230 -> frame 0x7f sent
09:33:34.333 -> frame 0xfe sent
09:33:34.435 -> frame 0x17d sent
09:33:34.538 -> frame 0x1fc sent
09:33:34.640 -> frame 0x27b sent
09:33:34.744 -> frame 0x2fa sent
09:33:34.811 -> frame 0x379 sent
09:33:34.914 -> frame 0x3f8 sent
09:33:35.016 -> frame 0x477 sent
09:33:35.119 -> frame 0x4f6 sent
09:33:35.224 -> frame 0x575 sent
09:33:35.326 -> frame 0x5f4 sent
09:33:35.428 -> frame 0x673 sent
09:33:35.532 -> frame 0x6f2 sent
09:33:35.634 -> frame 0x771 sent
09:33:35.733 -> frame 0x7f0 sent

CAN Receiver
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment