Mich interessierten noch die MCP2515 Masken und Filter, die mit der Bibliothek gesetzt werden können. Dazu lasse ich den Teensy senden und den UNO empfangen, wobei die vorhandene Kommunikation weiterläuft, die LEDs blinken auch weiter. Das Beispiel MCP_CAN\examples\Extended_MaskFilter\Extended_MaskFilter.ino habe ich in meinen bestehenden UNO-Sketch eingebunden:
If you send the following extended IDs below to an Arduino loaded with this sketch
you will find that 0x00FFCC00 and 0x00FF9900 will not get in.ID in Hex is the same as the Filter in Hex.
0x00FFEE00
0x00FFDD00
0x00FFCC00 This example will NOT be receiving this ID
0x00FFBB00
0x00FFAA00
0x00FF9900 This example will NOT be receiving this ID
0x00FF8800
0x00FF7700
Bei den Masken markiert ein Bit mit 1 ein signifikantes Bit für die Filterung. Beispiel:
Maske: 0x1FFFFF00 [sub](habe ich gegenüber dem Beispiel verändert, 0x1FFFFFFF Maximum für Maske und ID)[/sub]
Extended ID: 0x00FF7700 kommt durch
Extended ID: 0x00FF7755 kommt durch
Extended ID: 0x00FF9900 kommt nicht durch
// UNO
// https://web.archive.org/web/20190916005524/http://henrysbench.capnfatz.com/henrys-bench/arduino-projects-tips-and-more/arduino-can-bus-module-1st-network-tutorial/
// CAN Send Example der Bibliothek https://github.com/coryjfowler/MCP_CAN_lib
#include <mcp_can.h>
#include <SPI.h>
MCP_CAN CAN0(10); // Set CS to pin 10
char msgString[128]; // Serial Output String Buffer
const uint32_t sendeIntervall = 1000;
uint32_t jetzt, sendeMillis;
void setup()
{
Serial.begin(115200);
while (CAN_OK != CAN0.begin(MCP_STDEXT, CAN_500KBPS, MCP_8MHZ)) // init can bus : masks and filters disabled, baudrate, Quarz vom MCP2551
{
Serial.println("CAN BUS Shield init fail");
Serial.println(" Init CAN BUS Shield again");
delay(100);
}
Serial.println("CAN BUS Shield init ok!");
CAN0.init_Mask(0, 1, 0x1FFFFF00); // Init first mask...
CAN0.init_Filt(0, 1, 0x00FFEE00); // Init first filter...
CAN0.init_Mask(1, 1, 0x1FFFFF00); // Init second mask...
CAN0.init_Filt(1, 1, 0x00FFDD00); // Init second filter...
CAN0.init_Filt(2, 1, 0x00FFBB00); // Init third filter...
CAN0.init_Filt(3, 1, 0x00FFAA00); // Init fouth filter...
CAN0.init_Filt(4, 1, 0x00FF8800); // Init fifth filter...
CAN0.init_Filt(5, 1, 0x00FF7700); // Init sixth filter...
CAN0.setMode(MCP_NORMAL); // Change to normal mode to allow messages to be transmitted
}
byte data[] = {0, 1, 2, 3, 4, 5, 6, 7};
byte len = sizeof(data);
void loop()
{
jetzt = millis();
if (jetzt - sendeMillis >= sendeIntervall) {
sendeMillis = jetzt;
// byte sndStat = CAN0.sendMsgBuf(0x70, 0, len, data); // send data: id = 0x70, standard frame, data len = 8, data buf
byte sndStat = CAN0.sendMsgBuf(0x12345670, 1, len, data); // send data: id = 0x12345670, extended frame, data len = 8, data buf
if (sndStat == CAN_OK) {
Serial.println("Message Sent Successfully!");
} else {
Serial.println("Error Sending Message...");
}
data[2]++;
}
unsigned long rxId = 0;
byte len = 0;
byte rxBuf[8];
if (CAN_MSGAVAIL == CAN0.checkReceive()) // check if data coming
{
CAN0.readMsgBuf(&rxId, &len, rxBuf); // Read data: len = data length, buf = data byte(s)
//Serial.println("-----------------------------");
if ((rxId & 0x80000000) == 0x80000000) // Determine if ID is standard (11 bits) or extended (29 bits)
sprintf(msgString, "Extended ID: 0x%.8lX DLC: %1d Data:", (rxId & 0x1FFFFFFF), len);
else
sprintf(msgString, "Standard ID: 0x%.3lX DLC: %1d Data:", rxId, len);
Serial.print(msgString);
if ((rxId & 0x40000000) == 0x40000000) { // Determine if message is a remote request frame.
sprintf(msgString, " REMOTE REQUEST FRAME");
Serial.print(msgString);
} else {
for (byte i = 0; i < len; i++) {
sprintf(msgString, " 0x%.2X", rxBuf[i]);
Serial.print(msgString);
}
}
Serial.println();
}
}
// Mega2560
// SCK - 52
// SI - 51
// SO - 50
// CS - 53
// http://henrysbench.capnfatz.com/henrys-bench/arduino-projects-tips-and-more/arduino-can-bus-module-1st-network-tutorial/
// CAN Receive Example der Bibliothek https://github.com/coryjfowler/MCP_CAN_lib
#include <mcp_can.h>
#include <SPI.h>
MCP_CAN CAN0(53); // Set CS pin
char msgString[128]; // Serial Output String Buffer
const int LEDpin = 13;
void setup()
{
Serial.begin(115200);
pinMode(LEDpin, OUTPUT);
while (CAN_OK != CAN0.begin(MCP_ANY, CAN_500KBPS, MCP_8MHZ)) // init can bus : masks and filters disabled, baudrate, Quarz vom MCP2551
{
Serial.println("CAN BUS Shield init fail");
Serial.println(" Init CAN BUS Shield again");
delay(100);
}
Serial.println("CAN BUS Shield init ok!");
CAN0.setMode(MCP_NORMAL); // Change to normal mode to allow messages to be transmitted
}
void loop()
{
unsigned long rxId = 0;
byte len = 0;
byte rxBuf[8];
if (CAN_MSGAVAIL == CAN0.checkReceive()) // check if data coming
{
CAN0.readMsgBuf(&rxId, &len, rxBuf); // Read data: len = data length, buf = data byte(s)
Serial.println("-----------------------------");
if ((rxId & 0x80000000) == 0x80000000) // Determine if ID is standard (11 bits) or extended (29 bits)
sprintf(msgString, "Extended ID: 0x%.8lX DLC: %1d Data:", (rxId & 0x1FFFFFFF), len);
else
sprintf(msgString, "Standard ID: 0x%.3lX DLC: %1d Data:", rxId, len);
Serial.print(msgString);
if ((rxId & 0x40000000) == 0x40000000) { // Determine if message is a remote request frame.
sprintf(msgString, " REMOTE REQUEST FRAME");
Serial.print(msgString);
} else {
for (byte i = 0; i < len; i++) {
sprintf(msgString, " 0x%.2X", rxBuf[i]);
Serial.print(msgString);
if (i == 2)
{
digitalWrite(LEDpin, rxBuf[i] % 2);
}
}
}
Serial.println();
}
}
Fortsetzung folgt ...