Created
January 21, 2021 07:21
-
-
Save alexey-bass/0127f7781b4a0fa0614f5d142ee0864c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
sendData()
CAN Sender
CAN Receiver
