help in understanding mask and filters

So I am seeking out for a clearer understanding on the Can bus mask and filters as I am struggling with it tbh. Below is a snippet of code from an example that i decided to use to help me get my head around this,
Say for example i wanted to only see standard 11 bit :

ID:0x260 (3) 0x7F 0x32 0x7F

and

ID:0x305 (7) 0x00 0x00 0xC0 0x00 0x10 0x00 0x00

How would i set the mask and filters up to do this?

Appreciated any help with this :slight_smile: thanks.

 CAN.init_Mask(0, 0, 0x3ff);                         
 CAN.init_Mask(1, 0, 0x3ff);

    

    
 CAN.init_Filt(0, 0, 0x04);                          
 CAN.init_Filt(1, 0, 0x05);                        

 CAN.init_Filt(2, 0, 0x06);                         
 CAN.init_Filt(3, 0, 0x07);                          
 CAN.init_Filt(4, 0, 0x08);                          
 CAN.init_Filt(5, 0, 0x09);

if you are using coryjfowler library any refering to this example

then IMHO to filter/receive only CAN IDs 0x260 and 0x305

you could try something like this:

#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_STD, CAN_500KBPS, MCP_16MHZ) == CAN_OK) Serial.print("MCP2515 Init Okay!!\r\n");
  else Serial.print("MCP2515 Init Failed!!\r\n");
  pinMode(2, INPUT);                       // Setting pin 2 for /INT input


  CAN0.init_Mask(0, 0, 0x07FF0000);              // Init first mask...
  CAN0.init_Filt(0, 0, 0x02600000);              // Init first filter...
  CAN0.init_Filt(1, 0, 0x03050000);              // Init second filter...

  CAN0.init_Mask(1, 0, 0x07FF0000);              // Init second mask...
  CAN0.init_Filt(2, 0, 0x02600000);              // Init third filter...
  CAN0.init_Filt(3, 0, 0x03050000);              // Init fouth filter...
  CAN0.init_Filt(4, 0, 0x02600000);              // Init fifth filter...
  CAN0.init_Filt(5, 0, 0x03050000);              // Init sixth filter...

  Serial.println("MCP2515 Library Mask & Filter Example...");
  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....

PS: Could you also confirm what CAN shield you are using please

appreciate the quick reply, Thanks! i am using a mcp2515 shield for arduino uno :slight_smile:

RyanAES:
appreciate the quick reply, Thanks! i am using a mcp2515 shield for arduino uno :slight_smile:

there is a few of those sheilds available... is it the SEED one, Sparkfun, from eBay...??

asking coz they don't all have the same crystal frequency... but that for later to check I guess if are getting reading (with not filter) issues! :wink:

yes sorry just a generic one from ebay, 16mHz crystal, I have no issues reading or sending normally but it i needed to know how to use the filters properly as i only want to receive 2 specific messages one which i want to read and other to send :), No doubt i will be back asking more another time lol :slight_smile:

RyanAES:
So I am seeking out for a clearer understanding on the Can bus mask and filters as I am struggling with it tbh.

I think the best explanation is in the chip datasheet.