CAN Filter MCP2515 not working

Hey Guys, i know i know there r already a lot of threads open for this problem but i have read them all and still cant figure out what i have done wrong.

So im trying to filter out 0x150, 0x151 and 0x361. I have set both masks and the filters -> in the monitor it says filter initialized successfully but when i then send my CAN signals all of them are showing up in my monitor.

This is my code:

#include <SPI.h>
#include <mcp_can.h>

// Initialisierung des CAN-Bus auf dem MCP2515-Modul
MCP_CAN CAN(10);

void setup() {
  Serial.begin(9600);

  // Initialisierung des MCP2515
  if(CAN.begin(MCP_ANY, CAN_500KBPS, MCP_16MHZ) == CAN_OK) {
    Serial.println("CAN-Bus initialisiert");
    CAN.setMode(MCP_NORMAL);
  } else {
    Serial.println("Fehler bei der Initialisierung des CAN-Bus");
    while(1);
  }


  CAN.init_Mask(0, 0, 0x1ff);              // Init first mask (Std CAN. No masking applied)
  CAN.init_Filt(0, 0, 0x150);              // Init first filter...
  CAN.init_Filt(1, 0, 0x151);              // Init second filter...

  CAN.init_Mask(1, 0, 0x3ff);              // Init second mask (Std CAN. No masking applied)
  CAN.init_Filt(2, 0, 0x361);              // Init third filter...
  //CAN.init_Filt(3, 0, 0x01);              // Init fourth filter...
  //CAN.init_Filt(4, 0, 0x01);              // Init fifth filter...
  //CAN.init_Filt(5, 0, 0x01);              // Init sixth filter...
}


void loop() {
  // Prüfung auf eingehende CAN-Nachrichten
  if(CAN_MSGAVAIL == CAN.checkReceive()) {
    // Empfangen einer CAN-Nachricht
    unsigned char len = 0;
    unsigned char buf[8];
    unsigned long canId = 0;
    CAN.readMsgBuf(&canId, &len, buf);

    Serial.print("CAN-Nachricht empfangen - ID: ");
    Serial.print(canId, HEX);
    Serial.print(" Länge: ");
    Serial.print(len);
    Serial.print(" Daten: ");
    for(int i = 0; i < len; i++) {
      Serial.print(buf[i], HEX);
      Serial.print(" ");
    }
    Serial.println();
  }
}

There's a flaw in the chip that requires you to treat 11 bit addresses as 29 bit addresses.

Here's an example for a range of IDs from 07E8 to 07ED.
Modify to suit the IDs you require.

// There are two independent receive paths.
// Path 0 (RXB0 in the datasheet) has a mask and two filters labelled 0 and 1.  
// Path 1 (RXB1 in the datasheet) has a mask and four filters labelled 2 to 5.
// The messages received by the two paths are combined and sent to the Arduino.

CAN0.init_Mask(0,0,0x07FF0000); // check all 11 address bits for RXB0  
CAN0.init_Filt(0,0,0x07E80000); // Allow 07E8 through RXB0.
CAN0.init_Filt(1,0,0x07E90000); // Allow 07E9 through RXB0  

CAN0.init_Mask(1,0,0x07FF0000); // check all 11 address bits for RXB1  
CAN0.init_Filt(2,0,0x07EA0000); // Allow 07EA through RXB1  
CAN0.init_Filt(3,0,0x07EB0000); // Allow 07EB through RXB1  
CAN0.init_Filt(4,0,0x07EC0000); // Allow 07EC through RXB1  
CAN0.init_Filt(5,0,0x07ED0000); // Allow 07ED through RXB1

so i just have to add the 0000 behind my IDs

like that?


  CAN.init_Mask(0, 0, 0x1ff0000);              // Init first mask (Std CAN. No masking applied)
  CAN.init_Filt(0, 0, 0x1500000);              // Init first filter...
  CAN.init_Filt(1, 0, 0x1510000);              // Init second filter...

  CAN.init_Mask(1, 0, 0x3ff0000);              // Init second mask (Std CAN. No masking applied)
  CAN.init_Filt(2, 0, 0x361000);              // Init third filter...
  //CAN.init_Filt(3, 0, 0x010000);              // Init fourth filter...
  //CAN.init_Filt(4, 0, 0x01);              // Init fifth filter...
  //CAN.init_Filt(5, 0, 0x01);              // Init sixth filter...

Copy the masks as I have shown.

Hey, so i copied the code exactly as u gave me which means i should only see IDs from 07E8 to 07ED, but all other messages r also going trough.

Also u wrote " CAN0. init_mask....." but it can only compile with "CAN. init_mask...." so without the 0 after CAN. Maybe it was just a typo or something is wrong on my end.

code:

#include <SPI.h>
#include <mcp_can.h>

// Initialisierung des CAN-Bus auf dem MCP2515-Modul
MCP_CAN CAN(10);

void setup() {
  Serial.begin(9600);

  // Initialisierung des MCP2515
  if(CAN.begin(MCP_ANY, CAN_500KBPS, MCP_16MHZ) == CAN_OK) {
    Serial.println("CAN-Bus initialisiert");
    CAN.setMode(MCP_NORMAL);
  } else {
    Serial.println("Fehler bei der Initialisierung des CAN-Bus");
    while(1);
  }


// There are two independent receive paths.
// Path 0 (RXB0 in the datasheet) has a mask and two filters labelled 0 and 1.  
// Path 1 (RXB1 in the datasheet) has a mask and four filters labelled 2 to 5.
// The messages received by the two paths are combined and sent to the Arduino.

CAN.init_Mask(0,0,0x07FF0000); // check all 11 address bits for RXB0  
CAN.init_Filt(0,0,0x07E80000); // Allow 07E8 through RXB0.
CAN.init_Filt(1,0,0x07E90000); // Allow 07E9 through RXB0  

CAN.init_Mask(1,0,0x07FF0000); // check all 11 address bits for RXB1  
CAN.init_Filt(2,0,0x07EA0000); // Allow 07EA through RXB1  
CAN.init_Filt(3,0,0x07EB0000); // Allow 07EB through RXB1  
CAN.init_Filt(4,0,0x07EC0000); // Allow 07EC through RXB1  
CAN.init_Filt(5,0,0x07ED0000); // Allow 07ED through RXB1
}


void loop() {
  // Prüfung auf eingehende CAN-Nachrichten
  if(CAN_MSGAVAIL == CAN.checkReceive()) {
    // Empfangen einer CAN-Nachricht
    unsigned char len = 0;
    unsigned char buf[8];
    unsigned long canId = 0;
    CAN.readMsgBuf(&canId, &len, buf);

    Serial.print("CAN-Message received - ID: ");
    Serial.print(canId, HEX);
    Serial.print(" Länge: ");
    Serial.print(len);
    Serial.print(" Daten: ");
    for(int i = 0; i < len; i++) {
      Serial.print(buf[i], HEX);
      Serial.print(" ");
    }
    Serial.println();
  }
}

Use MCP_STDEXT instead of MCP_ANY

That puts it into 29 bit mode.

MCP_ANY disables masks and filters.

works like a charm now thank u so much man <3

Thank you so much for your help!!!!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.