Struggling with CANBUS filtering

Hello,

Full code at the bottom of post.

I have read most if not all of the CANBUS filtering posts but I'm still struggling to get it to work,

Quick summary,

I'm trying to filter on ID's 0x280 & 0x288, so only these are presented to the Arduino buffer. I've tried the following in turn but none of them appear to work;

CAN0.init_Mask( 0, 0, 0b001010001000 );          // Init first mask...
CAN0.init_Filt( 0, 0, 0b001010000000 );                // Init 1st filter...
CAN0.init_Filt( 1, 0, 0b001010001000 );                // Init 2nd filter...

CAN0.init_Mask( 0, 0, 0b111111111111 );          // Init first mask...
CAN0.init_Filt( 0, 0, 0b001010000000 );                // Init 1st filter...
CAN0.init_Filt( 1, 0, 0b001010001000 );                // Init 2nd filter...

CAN0.init_Mask( 0, 0, 640 | 648 );          // Init first mask...
CAN0.init_Filt( 0, 0, 640 );                // Init 1st filter...
CAN0.init_Filt( 1, 0, 648 );                // Init 2nd filter...

CAN0.init_Mask( 0, 0, 0x280 | 0x288 );          // Init first mask...
CAN0.init_Filt( 0, 0, 0x280 );                // Init 1st filter...
CAN0.init_Filt( 1, 0, 0x288 );                // Init 2nd filter...

The filters are being set,

Entering Configuration Mode Successful!
Setting Baudrate Successful!
MCP2515 Initialized Successfully!
Entering Configuration Mode Successful!
Setting Baudrate Successful!
MCP2515 Initialized Successfully!
Starting to Set Mask!
Setting Mask Successful!
Starting to Set Filter!
Setting Filter Successful!
Starting to Set Filter!
Setting Filter Successful!

This is what I'm seeing after trying to apply the filters, as you can see all the ID's still appear,

ID: 2C0 Data: 00 83
ID: 488 Data: 00 23 23 7E A6 00 00 00
ID: 2A0 Data: 00 00 00
ID: 2C0 Data: 00 83
ID: 1A0 Data: 00 40 00 00 FE FE 00 15
ID: 280 Data: 09 23 2C 13 22 00 20 22
RPM: 1227
Max_RPM: 1727
ID: C2 Data: 00 80 00 80 04 B2 00 4D
ID: 320 Data: 04 02 12 00 00 00 00 00
ID: 38A Data: A8 00 A8 00
ID: 2C0 Data: 00 83
ID: 2A0 Data: 00 00 00
ID: 280 Data: 09 22 3C 13 22 00 20 22
RPM: 1231
Max_RPM: 1727
ID: 5A0 Data: 80 00 00 31 A3 01 00 AB
ID: 1A0 Data: 00 40 00 00 FE FE 00 19
ID: 38A Data: B8 00 B8 00
ID: 1A0 Data: 00 40 00 00 FE FE 00 1A
ID: 2C0 Data: 00 83
ID: 288 Data: 63 51 10 00 00 7B B4 1E
ID: 4A8 Data: 90 80 01 00 00 00 A0 B1
ID: C2 Data: 00 80 00 80 04 F2 00 0D
ID: 4A0 Data: 00 00 00 00 00 00 00 00
ID: 38A Data: C8 00 C8 00
ID: 1A0 Data: 00 40 00 00 FE FE 00 1D
ID: 280 Data: 09 21 49 13 22 00 20 22
RPM: 1234
Max_RPM: 1727
ID: 480 Data: F0 00 AA 7C 00 A5 00 00
ID: 2C0 Data: 00 83
ID: 280 Data: 09 21 49 13 22 00 20 22

I'm sure it must be something simple I'm missing but for the life of me I can't find what it is, any help would be most appreciated.

Thanks Ian


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

//DWIN
const unsigned long eventInterval = 333;
unsigned long previousTimeRPM = 0;
unsigned long previousTimeSpeed = 0;
unsigned long previousTimeWM = 0;
unsigned long previousTimeIAT = 0;
unsigned long previousTimePSI = 0;

SoftwareSerial DWINSerial(3, 2); // RX, TX

unsigned char Buffer[8];

unsigned char   RPMDWIN[8] = {0x5a, 0xa5, 0x05, 0x82, 0x12, 0x01, 0x00, 0x00};
unsigned char   IAT_CDWIN[8] = {0x5a, 0xa5, 0x05, 0x82, 0x15, 0x01, 0x00, 0x00};
unsigned char   PSIDWIN[8] = {0x5a, 0xa5, 0x05, 0x82, 0x20, 0x01, 0x00, 0x00};
unsigned char   Max_RPMDWIN[8] = {0x5a, 0xa5, 0x05, 0x82, 0x16, 0x01, 0x00, 0x00};
unsigned char   Max_IATDWIN[8] = {0x5a, 0xa5, 0x05, 0x82, 0x19, 0x01, 0x00, 0x00};
unsigned char   Max_PSIDWIN[8] = {0x5a, 0xa5, 0x05, 0x82, 0x21, 0x01, 0x00, 0x00};

//CanBus
#define CAN0_INT 9                              // Set INT to pin 9
MCP_CAN CAN0(10);                               // Set CS to pin 10

long unsigned int rxId;
unsigned char len = 0;
unsigned char rxBuf[8];
bool init_state = true;

//Change lights
int numLEDSLit = 1;
int latchPin = 8;      // (8) ST_CP [RCK] on 74HC595
int clockPin = 7;      // (5) SH_CP [SCK] on 74HC595
int dataPin = 6;     // (3) DS [S1] on 74HC595
int leds = 0;

// Boost Pressure and IAT
// int offset = 125; // low pressure calibration
// int span = 895; // high pressure calibration
int offset = 111; // low pressure calibration
int span = 912; // high pressure calibration
float Boost_pressure_Pa;
float Boost_pressure_kPa;
float Boost_pressure_PSI;
int boost_pressure_pin = A3;
float Max_PSI = 0;
int IAT_voltage = 0;
float IAT_Vin = 4.6; // 4.6 volts - USB powered
float IAT_Vout = 0;
float R1 = 2000; // Temperature resistor value
float IAT_buffer = 0;
float logR2;
float R2;
float IAT;
int IAT_pin = A1;
float IAT_C = 0;
float Max_IAT = 0;

// float A = 1.297373711e-03, B = 2.587931666e-04, C = 1.639343214e-07;  // Steinhart-Hart and Hart Coefficients Max
// float A = 1.306496435e-03, B = 2.580557547e-04, C = 1.752303942e-07;  // Steinhart-Hart and Hart Coefficients Nominal
float A = 1.316012871e-03, B = 2.572431200e-04, C = 1.877359037e-07;  // Steinhart-Hart and Hart Coefficients Min

//RPM
int RPM = 0;
int Max_RPM = 0;
int ARPM = 0;
int BRPM = 0;

//Speed
int Speed = 0;
int Max_Speed = 0;

//W-M
int Max_WM = 0;
int WM_value_percent = 1;
int WM_pin = 6;
int WM_value_T;
int WM_value_P;
int WM_value;
//int WM_Level_pin = A2;
int WM_Level = 101;

void setup() {
  Serial.begin(115200);
  DWINSerial.begin(115200);
  pinMode(WM_pin, OUTPUT);
  pinMode(latchPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
  pinMode(clockPin, OUTPUT);

  // Initialize MCP2515 running at 8MHz with a baudrate of 500kb/s and the masks and filters disabled.
  if (CAN0.begin(MCP_ANY, CAN_500KBPS, MCP_8MHZ) == CAN_OK) Serial.println("MCP2515 Initialized Successfully!");
  else Serial.println("Error Initializing MCP2515...");
  if (CAN0.begin(MCP_ANY, CAN_500KBPS, MCP_8MHZ) == CAN_OK)
    Serial.println("MCP2515 Initialized Successfully!");
  else
    Serial.println("Error Initializing MCP2515...");

  CAN0.setMode(MCP_NORMAL);                     // Set operation mode to normal so the MCP2515 sends acks to received data.

  pinMode(9, INPUT);                            // Setting pin 9 for /INT input
  pinMode(CAN0_INT, INPUT);                            // Configuring pin for /INT input

//  CAN0.init_Mask( 0, 0, 0b001010001000 );          // Init first mask...
//  CAN0.init_Filt( 0, 0, 0b001010000000 );                // Init 1st filter...
//  CAN0.init_Filt( 1, 0, 0b001010001000 );                // Init 2nd filter...

    CAN0.init_Mask( 0, 0, 0b111111111111 );          // Init first mask...
    CAN0.init_Filt( 0, 0, 0b001010000000 );                // Init 1st filter...
    CAN0.init_Filt( 1, 0, 0b001010001000 );                // Init 2nd filter...

  //  CAN0.init_Mask( 0, 0, 640 | 648 );          // Init first mask...
  //  CAN0.init_Filt( 0, 0, 640 );                // Init 1st filter...
  //  CAN0.init_Filt( 1, 0, 648 );                // Init 2nd filter...

  //  CAN0.init_Mask( 0, 0, 0x280 | 0x288 );          // Init first mask...
  //  CAN0.init_Filt( 0, 0, 0x280 );                // Init 1st filter...
  //  CAN0.init_Filt( 1, 0, 0x288 );                // Init 2nd filter...
}

void updateShiftRegister()
{
  digitalWrite(latchPin, LOW);
  shiftOut(dataPin, clockPin, MSBFIRST, leds);
  digitalWrite(latchPin, HIGH);
}

void sendRPMDWIN() {
  unsigned long currentTimeRPM = millis();

  if (currentTimeRPM - previousTimeRPM >= eventInterval) {
    /* Event code */
    RPMDWIN[6] = highByte(RPM);
    RPMDWIN[7] = lowByte(RPM);
    DWINSerial.write(RPMDWIN, 8);

    /* Update the timing for the next time around */
    previousTimeRPM = currentTimeRPM;
  }
}

void sendSpeedDWIN() {

  unsigned long currentTimeSpeed = millis();

  if (currentTimeSpeed - previousTimeSpeed >= eventInterval) {
    byte SpeedDWIN = Speed;

    byte open[] = {0x5A, 0xA5, 0x05, 0x82, 0x13, 0x01, 0x00, SpeedDWIN};

    DWINSerial.write(open, sizeof(open));
    previousTimeSpeed = currentTimeSpeed;
  }
}

void sendWM_value_percentDWIN() {

  unsigned long currentTimeWM = millis();

  if (currentTimeWM - previousTimeWM >= eventInterval) {

    byte WM_value_percentDWIN = WM_value_percent;

    byte open[] = {0x5A, 0xA5, 0x05, 0x82, 0x14, 0x01, 0x00, WM_value_percentDWIN};

    DWINSerial.write(open, sizeof(open));
    previousTimeWM = currentTimeWM;
  }
}

void sendIAT_CDWIN() {

  unsigned long currentTimeIAT = millis();

  if (currentTimeIAT - previousTimeIAT >= eventInterval) {


    int DWINIAT_C = IAT_C * 100;

    IAT_CDWIN[6] = highByte(DWINIAT_C);
    IAT_CDWIN[7] = lowByte(DWINIAT_C);
    DWINSerial.write(IAT_CDWIN, 8);

    previousTimeIAT = currentTimeIAT;
  }
}

void sendPSI_DWIN() {

  unsigned long currentTimePSI = millis();

  if (currentTimePSI - previousTimePSI >= eventInterval) {

    int DWINPSI = Boost_pressure_PSI * 100;

    PSIDWIN[6] = highByte(DWINPSI);
    PSIDWIN[7] = lowByte(DWINPSI);
    DWINSerial.write(PSIDWIN, 8);

    previousTimePSI = currentTimePSI;
  }
}
void sendMax_RPMDWIN() {

  if (RPM > Max_RPM) {

    Max_RPM = RPM;

    Max_RPMDWIN[6] = highByte(Max_RPM);
    Max_RPMDWIN[7] = lowByte(Max_RPM);
    DWINSerial.write(Max_RPMDWIN, 8);
    Serial.print ("sending Max_RPM: ");
    Serial.println (Max_RPM);
  }
}

void sendMax_SpeedDWIN() {

  if (Speed > Max_Speed) {

    Max_Speed = Speed;

    byte Max_SpeedDWIN = Max_Speed;

    byte open[] = {0x5A, 0xA5, 0x05, 0x82, 0x17, 0x01, 0x00, Max_SpeedDWIN};

    DWINSerial.write(open, sizeof(open));
  }
}

void sendMax_WMDWIN() {

  if (WM_value_percent > Max_WM) {

    Max_WM = WM_value_percent;

    byte Max_WMDWIN = Max_WM;

    byte open[] = {0x5A, 0xA5, 0x05, 0x82, 0x18, 0x01, 0x00, Max_WMDWIN};

    DWINSerial.write(open, sizeof(open));
  }
}

void sendMax_IATDWIN() {

  if (IAT_C > Max_IAT) {

    Max_IAT = IAT_C;

    int Max_DWINIAT_C = Max_IAT * 100;

    Max_IATDWIN[6] = highByte(Max_DWINIAT_C);
    Max_IATDWIN[7] = lowByte(Max_DWINIAT_C);
    DWINSerial.write(Max_IATDWIN, 8);
  }
}

void sendMax_PSIDWIN() {

  if (Boost_pressure_PSI > Max_PSI) {

    Max_PSI = Boost_pressure_PSI;

    int Max_DWINPSI = Max_PSI * 100;

    Max_PSIDWIN[6] = highByte(Max_DWINPSI);
    Max_PSIDWIN[7] = lowByte(Max_DWINPSI);
    DWINSerial.write(Max_PSIDWIN, 8);
  }
}
void MAXRESET () {

  if (DWINSerial.available())
  {
    for (int i = 0; i <= 8; i++) //this loop will store whole frame in buffer array.
    {
      Buffer[i] = DWINSerial.read();
      //      Serial.print(Buffer[0], HEX);
      //      Serial.print(Buffer[2], HEX);
      //      Serial.print(Buffer[2], HEX);
      //      Serial.print(Buffer[3], HEX);
      //      Serial.print(Buffer[4], HEX);
      //      Serial.print(Buffer[5], HEX);
      //      Serial.print(Buffer[6], HEX);
      //      Serial.print(Buffer[7], HEX);
      //      Serial.println(Buffer[8], HEX);
    }

    if (Buffer[0] == 0X5A)
    {
      switch (Buffer[4])
      {
        case 0x11:
          if (Buffer[8] == 1)
          {
            ResetRPM();
            ResetSpeed();
            ResetIAT();
            ResetWM();
            ResetPSI();

          }
      }
    }
  }
}


void ResetRPM () {
  Max_RPM = 0;
  byte open[] = {0x5a, 0xa5, 0x05, 0x82, 0x16, 0x01, 0x00, 0x00};

  DWINSerial.write(open, sizeof(open));
}

void ResetSpeed () {
  Max_Speed = 0;
  byte open[] = {0x5A, 0xA5, 0x05, 0x82, 0x17, 0x01, 0x00, 0x00};

  DWINSerial.write(open, sizeof(open));
}

void ResetIAT () {
  Max_IAT = 0;
  byte open[] = {0x5a, 0xa5, 0x05, 0x82, 0x19, 0x01, 0x00, 0x00};

  DWINSerial.write(open, sizeof(open));
}

void ResetWM () {
  Max_WM = 0;
  byte open[] = {0x5A, 0xA5, 0x05, 0x82, 0x18, 0x01, 0x00, 0x00};

  DWINSerial.write(open, sizeof(open));
}

void ResetPSI () {
  Max_PSI = 0;
  byte open[] = {0x5A, 0xA5, 0x05, 0x82, 0x21, 0x01, 0x00, 0x00};

  DWINSerial.write(open, sizeof(open));
}

void loop() {

  //            Serial.println("loop start");
  if (!digitalRead(9))                   // If pin 9 is low, read receive buffer
  {
    //            Serial.println("before readMsgBuf");
    CAN0.readMsgBuf(&rxId, &len, rxBuf); // Read data: len = data length, buf = data byte(s)

    Serial.print("ID: ");
    Serial.print(rxId, HEX);
    Serial.print(" Data: ");
    for (int i = 0; i < len; i++)        // Print each byte of the data
    {
      if (rxBuf[i] < 0x10)               // If data byte is less than 0x10, add a leading zero
      {
        Serial.print("0");
      }
      Serial.print(rxBuf[i], HEX);
      Serial.print(" ");
    }
    Serial.println();
    //      unsigned long msTime = millis();
    //      Serial.println(msTime);

    if (rxId == 0x280) {

      //          throttle = rxBuf[5] * 0.4;
      //          Serial.print("Throttle (%): ");
      //          Serial.print(throttle, HEX);
      //          Serial.print("   ");
      //          Serial.println(throttle, DEC);
      //          if (throttle > throttlemax) throttlemax = throttle;
      //          Serial.print("Throttlemax: ");
      //          Serial.println(throttlemax, DEC);
      //
      //
      //          load = rxBuf[1] * 0.39;
      //          Serial.print("Engine Load (%): ");
      //          Serial.print(load, HEX);
      //          Serial.print("   ");
      //          Serial.println(load, DEC);
      //          if (load > loadmax) loadmax = load;
      //          Serial.print("loadmax: ");
      //          Serial.println(loadmax, DEC);

      //          RPM = ((rxBuf[3] * 256) + rxBuf[2]) * 0.25;
      ARPM = rxBuf[3] * 256;
      BRPM = ARPM + rxBuf[2];
      RPM = BRPM * 0.25;
      Serial.print("RPM: ");
      Serial.println(RPM, DEC);
      Serial.print("Max_RPM: ");
      Serial.println(Max_RPM, DEC);
    }

    //          if(rxId == 0x288){
    //
    //          Speed = (rxBuf[3]*0.759);
    //          Serial.print("vehicle Speed (MPH): ");
    //          Serial.print(Speed);
    //          Serial.print("   ");
    //          Serial.println(Speed, DEC);
    //          if (Speed > Max_Speed) Max_Speed = Speed;
    //          Serial.print("Max_Speed: ");
    //          Serial.println(Max_Speed, DEC);
    //          }
  }


  // WM level code
  //  int WM_Level = analogRead(WM_Level_pin); // get adc value
  //
  //  if (((HistoryValue >= WM_Level) && ((HistoryValue - WM_Level) > 10)) || ((HistoryValue < WM_Level) && ((WM_Level - HistoryValue) > 10)))
  //  {
  //    //sprintf(printBuffer, "ADC%d level is %d\n", WM_Level_pin, WM_Level);
  //    //Serial.print(printBuffer);
  //    HistoryValue = WM_Level;
  //  }

  // Boost Pressure

  Boost_pressure_Pa = map(analogRead(boost_pressure_pin), offset, span, 500, 4000);
  Boost_pressure_kPa = Boost_pressure_Pa / 10, 1;
  if (Boost_pressure_kPa < 20) Boost_pressure_kPa = 20;
  Boost_pressure_PSI = Boost_pressure_kPa * 0.145038;
  Boost_pressure_PSI = Boost_pressure_PSI - 14;
  if (Boost_pressure_PSI < 1) Boost_pressure_PSI = 0;
  //  if (Boost_pressure_PSI > Max_PSI) Max_PSI = Boost_pressure_PSI;

  //IAT

  IAT_voltage = analogRead(IAT_pin);
  if (IAT_voltage) {
    IAT_buffer = IAT_voltage * IAT_Vin;
    IAT_Vout = (IAT_buffer) / 1024.0;
    IAT_buffer = (IAT_Vin / IAT_Vout) - 1;
    R2 = R1 / IAT_buffer;
  }

  logR2 = log(R2);
  IAT = (1.0 / (A + B * logR2 + C * logR2 * logR2 * logR2)); // Steinhart and Hart Equation. T  = 1 / {A + B[ln(R)] + C[ln(R)]^3}
  IAT_C =  IAT - 273.15;

  // WM delivery
  if (IAT_C >= 27) {
    WM_value_T = map (IAT_C, 27, 40, 0, 255);
    WM_value_P = map(Boost_pressure_PSI, 8, 25, 0, 255);
    if (WM_value_T >= WM_value_P) WM_value = WM_value_T;
    if (WM_value_P >= WM_value_T) WM_value = WM_value_P;
    if (WM_value <= 0) WM_value = 0;
    WM_value_percent = WM_value / 2.5;
    if (Boost_pressure_PSI >= 31) WM_value_percent = 100;
    if (WM_value_percent >= 100) WM_value_percent = 100;
    WM_value = WM_value_percent * 2.55;
    analogWrite(WM_pin, WM_value);
  }
  else
  {
    WM_value_P = map(Boost_pressure_PSI, 8, 25, 0, 255);
    WM_value_T = map (IAT_C, 28, 40, 0, 255);
    if (WM_value_P >= WM_value_T) WM_value = WM_value_P;
    if (WM_value_T >= WM_value_P) WM_value = WM_value_T;
    if (WM_value <= 0) WM_value = 0;
    WM_value_percent = WM_value / 2.5;
    if (Boost_pressure_PSI >= 25) WM_value_percent = 100;
    if (WM_value_percent >= 100) WM_value_percent = 100;
    WM_value = WM_value_percent * 2.55;
    analogWrite(WM_pin, WM_value);
  }

  // Shift lights not sure how it works
  int numLEDSLit = RPM / 1000;
  if (numLEDSLit >= 6) numLEDSLit = 8;
  if (numLEDSLit < 1) numLEDSLit = 1;
  leds = 0;   // no LEDs lit to start
  for (int i = 0; i < numLEDSLit; i++)
  {
    leds = leds + (1 << i);  // sets the i'th bit
  }
  updateShiftRegister();

  sendRPMDWIN();
  sendSpeedDWIN();
  sendWM_value_percentDWIN();
  sendIAT_CDWIN();
  sendPSI_DWIN();
  sendMax_RPMDWIN();
  sendMax_SpeedDWIN();
  sendMax_WMDWIN();
  sendMax_IATDWIN();
  sendMax_PSIDWIN();
  MAXRESET ();

}

Sorry, I do not know what exactly your problem is, but one small remark.

When testing filters, do not limit yourself to reset the arduino, be sure to turn off the power to the entire circuit. When resetting the arduino, the mcp2515 chip is not reset and the old filter settings in it are mixed with the new ones.
I read on one forum that a person suffered for a week with filters until he figured out that he must to reset the chip every time

Thanks for the tip, but unfortunately that's not the problem in this case as I have to take the project to the car each time to test it which involves powering down and up again. Would have been nice if it was that simple.

maybe be something like this would work for you:
(Compiles, NOT tested!)

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

long unsigned int rxId;
unsigned char len = 0;
unsigned char rxBuf[8];

MCP_CAN CAN0(10);                          // Set CS to pin 10

void setup()
{
  Serial.begin(115200);
  if (CAN0.begin(MCP_STDEXT, CAN_500KBPS, MCP_16MHZ) == CAN_OK) {
    Serial.print("MCP2515 Init Okay!!\r\n");
  }
  else {
    Serial.print("MCP2515 Init Failed!!\r\n");
    while (1); //stay here forever
  }

  pinMode(2, INPUT);                       // Setting pin 2 for /INT input


  CAN0.init_Mask(0, 0, 0x07FF0000);              // Init first mask (Std CAN. No masking applied)
  CAN0.init_Filt(0, 0, 0x02800000);              // Init first filter...
  CAN0.init_Filt(1, 0, 0x02800000);              // Init second filter...

  CAN0.init_Mask(1, 0, 0x07FF0000);              // Init second mask (Std CAN. No masking applied)
  CAN0.init_Filt(2, 0, 0x02880000);              // Init third filter...
  CAN0.init_Filt(3, 0, 0x02880000);              // Init fourth filter...
  CAN0.init_Filt(4, 0, 0x02880000);              // Init fifth filter...
  CAN0.init_Filt(5, 0, 0x02880000);              // Init sixth filter...

  CAN0.setMode(MCP_NORMAL);                // Change to normal mode to allow messages to be transmitted
}

void loop()
{
  if (!digitalRead(2))                   // If pin 2 is low, read receive buffer
  {
    CAN0.readMsgBuf(&rxId, &len, rxBuf); // Read data: len = data length, buf = data byte(s)
    Serial.print("ID: ");
    Serial.print(rxId, HEX);
    Serial.print(" Data: ");
    for (int i = 0; i < len; i++)        // Print each byte of the data
    {
      if (rxBuf[i] < 0x10)               // If data byte is less than 0x10, add a leading zero
      {
        Serial.print("0");
      }
      Serial.print(rxBuf[i], HEX);
      Serial.print(" ");
    }
    Serial.println();
  }
}

Hope that helps....

A 0 in a mask is like a leaky hole. The more 0 you have, the more likely you will receive unwanted messages.
The masks operate in parallel therefore if one is checking all bits and the other isn't then unwanted messages will leak through. So, for a simple filter that only needs to allow 2 messages, set both masks to inspect all bits and only set 2 filters for one of those masks.

Also, don't use MCP_ANY because in MCP_CAN_lib/mcp_can_dfs.h at master · coryjfowler/MCP_CAN_lib · GitHub
it says
#define MCP_ANY 3 /* Disables Masks and Filters */

As noted here CAN_BUS_Shield-1/README.md at master · Mebus/CAN_BUS_Shield-1 · GitHub
"Due to a bug in the MCP2515 silicon it is not possible to reliably match only standard or only extended IDs. "
Therefore treating the 11 bit address as a 29 bit address seems like the only reliable way of making things work. This explains the use of the 0x07FF0000 which translates as 111 1111 1111 0000 0000 0000 0000. From the datasheet


As it shows in the diagram you add 16 bits of zero beyond the 11 you are interested in.

Thanks both mikb55 & sherzaad.

Changed the MCP to STDEXT as pointed out by mikb55 and applied masks & filters as below from sherzaad and BINGO all working as required. It's been bugging on and off for weeks, wouldn't have worked out the MCP_ANY disabling Masks & filters.

Thanks again both most appreciated :slight_smile:

CAN0.init_Mask(0, 0, 0x07FF0000); // Init first mask (Std CAN. No masking applied)
CAN0.init_Filt(0, 0, 0x02800000); // Init first filter...
CAN0.init_Filt(1, 0, 0x02800000); // Init second filter...

CAN0.init_Mask(1, 0, 0x07FF0000); // Init second mask (Std CAN. No masking applied)
CAN0.init_Filt(2, 0, 0x02880000); // Init third filter...
CAN0.init_Filt(3, 0, 0x02880000); // Init fourth filter...
CAN0.init_Filt(4, 0, 0x02880000); // Init fifth filter...
CAN0.init_Filt(5, 0, 0x02880000); // Init sixth filter...

Hi. It works because you have 2 filters begining with 28
I have 320 , 38A , 448 and 520 and i broke my brain from 2 weeks to filter them but not work.
Anybody can help me?

There are 6 filters in total and you are only searching for 4 Ids therefore just setup a filter for each Id.

I made it , doesn't work
I receive only first id
first try
CAN0.init_Mask(0, 0, 0x07FF0000);
CAN0.init_Filt(0, 0, 0x03200000);
CAN0.init_Mask(1, 0, 0x07FF0000);
CAN0.init_Filt(1, 0, 0x038A0000);
CAN0.init_Filt(2, 0, 0x04480000);
CAN0.init_Filt(3, 0, 0x05200000);

second try
CAN0.init_Mask(0, 0, 0x07FF0000);
CAN0.init_Mask(1, 0, 0x07FF0000);
CAN0.init_Filt(0, 0, 0x03200000);
CAN0.init_Filt(1, 0, 0x038A0000);
CAN0.init_Filt(2, 0, 0x04480000);
CAN0.init_Filt(3, 0, 0x05200000);

t-rd try
CAN0.init_Mask(0, 0, 0x07FF0000);
CAN0.init_Filt(0, 0, 0x03200000);
CAN0.init_Filt(1, 0, 0x038A0000);
CAN0.init_Filt(2, 0, 0x04480000);
CAN0.init_Filt(3, 0, 0x05200000);
CAN0.init_Mask(1, 0, 0x07FF0000);

Use the receive example in the library that receives everything.
Do you see all 4 Ids?

this is what library had
CAN0.init_Mask(0,0,0x010F0000); // Init first mask...
CAN0.init_Filt(0,0,0x01000000); // Init first filter...
CAN0.init_Filt(1,0,0x01010000); // Init second filter...

CAN0.init_Mask(1,0,0x010F0000); // Init second mask...
CAN0.init_Filt(2,0,0x01030000); // Init third filter...
CAN0.init_Filt(3,0,0x01040000); // Init fouth filter...
CAN0.init_Filt(4,0,0x01060000); // Init fifth filter...
CAN0.init_Filt(5,0,0x01070000); // Init sixth filter...

how can i receive my id's (320 , 38A , 448 and 520 ) with it????

This is a receive everything sketch.

(Modify for crystal frequency if you have a 8MHz instead of 16MHz crystal)

Do you see all 4 message Ids?
If you don't see a particular message Id then setting a filter will do nothing.

Hi again. We don't understood or maybe my english is very bad.
I can receive all can id's on canbus , but i want to filter only this 4 id's 320 , 38A , 448 and 520
If i use 3 example like wrote upstairs i receive only firstg id's fropm the first filter.

this then maybe...
(Compiles, NOT tested!)

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

long unsigned int rxId;
unsigned char len = 0;
unsigned char rxBuf[8];

MCP_CAN CAN0(10);                          // Set CS to pin 10

void setup()
{
  Serial.begin(115200);
  if (CAN0.begin(MCP_STDEXT, CAN_500KBPS, MCP_16MHZ) == CAN_OK) {
    Serial.print("MCP2515 Init Okay!!\r\n");
  }
  else {
    Serial.print("MCP2515 Init Failed!!\r\n");
    while (1); //stay here forever
  }

  pinMode(2, INPUT);                       // Setting pin 2 for /INT input


  CAN0.init_Mask(0, 0, 0x07FF0000);              // Init first mask (Std CAN. No masking applied)
  CAN0.init_Filt(0, 0, 0x03200000);              // Init first filter...
  CAN0.init_Filt(1, 0, 0x03200000);              // Init second filter...

  CAN0.init_Mask(1, 0, 0x07FF0000);              // Init second mask (Std CAN. No masking applied)
  CAN0.init_Filt(2, 0, 0x03200000);              // Init third filter...
  CAN0.init_Filt(3, 0, 0x038A0000);              // Init fourth filter...
  CAN0.init_Filt(4, 0, 0x04480000);              // Init fifth filter...
  CAN0.init_Filt(5, 0, 0x05200000);              // Init sixth filter...

  CAN0.setMode(MCP_NORMAL);                // Change to normal mode to allow messages to be transmitted
}

void loop()
{
  if (!digitalRead(2))                   // If pin 2 is low, read receive buffer
  {
    CAN0.readMsgBuf(&rxId, &len, rxBuf); // Read data: len = data length, buf = data byte(s)
    Serial.print("ID: ");
    Serial.print(rxId, HEX);
    Serial.print(" Data: ");
    for (int i = 0; i < len; i++)        // Print each byte of the data
    {
      if (rxBuf[i] < 0x10)               // If data byte is less than 0x10, add a leading zero
      {
        Serial.print("0");
      }
      Serial.print(rxBuf[i], HEX);
      Serial.print(" ");
    }
    Serial.println();
  }
}

hope that helps...

thanks for yout help and time, i will test them tommorow
thanks a lot

they works like

CAN0.init_Mask(0, 0, 0x07FF0000);              // Init first mask (Std CAN. No masking applied)
  CAN0.init_Filt(0, 0, 0x03200000);              // Init first filter...
  CAN0.init_Filt(1, 0, 0x03200000);              // Init second filter...

  CAN0.init_Mask(1, 0, 0x07FF0000);              // Init second mask (Std CAN. No masking applied)
  CAN0.init_Filt(2, 0, 0x03200000);              // Init third filter...
  CAN0.init_Filt(3, 0, 0x038A0000);              // Init fourth filter...
  CAN0.init_Filt(4, 0, 0x04480000);              // Init fifth filter...
  CAN0.init_Filt(5, 0, 0x05200000);              // Init sixth filter...

i had something bad after filters in my code. I use a k-line too and there the canbus id's was lost.

I use now other library for esp32, ACAN_ESP32 with can put a filters directly in a list , without other words. It works greate. I use it because for ESP32CAN standard canbus library nevere understood how can put the filter there
p_filter.ACR0 = 0x29;
p_filter.ACR1 = 0;
p_filter.ACR2 = 0;
p_filter.ACR3 = 0;

p_filter.AMR0 = 0;
p_filter.AMR1 = 0xFF;
p_filter.AMR2 = 0xFF;
p_filter.AMR3 = 0xFF;

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