Hello,
I've been trying to get some CAN bus coding for my project, I can read all the CAN messages fine but I can't seem to get the filter and mask working. I have searched alot of forums posts about this issue but none seem to fix my issue that's why I am making this post.
I am using the CAN_BUS_Shield library with the MKR1010 with can bus shield.
What I want to end up with is a filter that only accept the ID 0xCFF20F3. The issue I have with the filters I have set up is that either everything gets accepted or just a random ID gets accepted and this outputs on the serial monitor, by unplugging and plugging the arduino back in it seems to get anogther ID then and loop it on the serial monitor.
Can anyone educate me about this issue please, thanks in advance.
// demo: CAN-BUS Shield, receive data with interrupt mode, and set mask and filter
//
// when in interrupt mode, the data coming can't be too fast, must >20ms, or else you can use check mode
// loovee, 2014-7-8
#include <SPI.h>
#define CAN_2515
// #define CAN_2518FD
// Set SPI CS Pin according to your hardware
#if defined(SEEED_WIO_TERMINAL) && defined(CAN_2518FD)
// For Wio Terminal w/ MCP2518FD RPi Hat:
// Channel 0 SPI_CS Pin: BCM 8
// Channel 1 SPI_CS Pin: BCM 7
// Interupt Pin: BCM25
const int SPI_CS_PIN = BCM8;
const int CAN_INT_PIN = BCM25;
#else
// For Arduino MCP2515 Hat:
// 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 = 3;
const int CAN_INT_PIN = 7;
#endif
#ifdef CAN_2518FD
#include "mcp2518fd_can.h"
mcp2518fd CAN(SPI_CS_PIN); // Set CS pin
#endif
#ifdef CAN_2515
#include "mcp2515_can.h"
mcp2515_can CAN(SPI_CS_PIN); // Set CS pin
#endif
//unsigned char flagRecv = 0;
unsigned char len = 0;
unsigned char buf[8];
char str[20];
void setup() {
SERIAL_PORT_MONITOR.begin(115200);
while(!Serial){};
//attachInterrupt(digitalPinToInterrupt(CAN_INT_PIN), MCP2515_ISR, FALLING); // start interrupt
while (CAN_OK != CAN.begin(CAN_500KBPS)) { // init can bus : baudrate = 500k
SERIAL_PORT_MONITOR.println("CAN init fail, retry...");
delay(100);
}
SERIAL_PORT_MONITOR.println("CAN init ok!");
CAN.init_Mask(0, 0,0xFFFFFFFF); // there are 2 mask in mcp2515, you need to set both of them
CAN.init_Filt(0, 0, 0xCF091F3);
CAN.init_Mask(1, 0, 0xFFFFFFFF);
}
void loop() {
CAN.readMsgBuf(&len, buf); // read data, len: data length, buf: data buf
SERIAL_PORT_MONITOR.println("\r\n------------------------------------------------------------------");
SERIAL_PORT_MONITOR.print("Get Data From id: ");
SERIAL_PORT_MONITOR.println(CAN.getCanId());
for (int i = 0; i < 8; i++) { // print the data
SERIAL_PORT_MONITOR.print("0x");
SERIAL_PORT_MONITOR.print(buf[i], HEX);
SERIAL_PORT_MONITOR.print("\t");
}
uint32_t value = (uint32_t)buf[5] << 16 +
(uint32_t)buf[4];
Serial.print(value);
SERIAL_PORT_MONITOR.println();
delay(50);
}
/*********************************************************************************************************
END FILE
*********************************************************************************************************/