CAN Bus IDs filtering using MCP2515

Good morning,

since some weeks i try to read specific values from the CAN Bus (J1939, 500Kbps, 29 bit messages)
Using arduino MEGA and library mcp_can

I want to filter on ID: 0x10FF410B

When i setup a mask and filter, i get no value,
please could someone can give me the Mask and filter setup and try to explain me how it works !
What value to filter in ID: 0x10FF410B ?

i read so many topics and articles, i could not understand the mask / Filtering method.

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

Thanks for help,

Regards

if you really want to filter messages only containing this, you might need something like:

    const uint32_t myMask = 0xFFFFFFFF;         // where to look at:                <U+202D><U+202D>11111111111111111111111111111111<U+202C>
    const uint32_t myFilter = 0x10FF410B;       // what to find:                    00010000111111110100000100001011
    CAN.init_Mask(0, 0, myMask);                // Init first mask
    CAN.init_Filt(0, 0, myFilter);              // Init first filter
    //  CAN.init_Filt(1, 0, myFilter);              // Init second filter...
    CAN.init_Mask(1, 0, myMask);                // Init second mask - must be set, otherwise first mask isn't working

the mask defines which bits will be checked.
the filter defines, how a bit (in the mask) schould look like.
for any reason, you must set the second mask also. Otherwise it will not work.
my tipp: think bitwise, just put mask and filter one row after the other, so it is clear what get checked and how it should look like.

Thank you !

it works like a charm !

But, please i need to understand ...

For example, if i want to look for the ID only, where is the ID located in 10FF410B ?

and can i look only for FF410B, is it possible ?

Thanks

Regards

I don't know what you see as ID. I just see 0x10FF410B . you have to describe what is an ID.

if you want to get all messages which end with 0xFF410B ... again, I advice notate it in binary!

you want to filter:
0b111111110100000100001011 //0xFF410B in Binary
your mask:
0b111111111111111111111111 // which is equal 0xFFFFFF

binary is your friend!

To ignore the first byte of the 32-bit ID, set those 8 bits in the 'mask' to zero ('don't care')
mask = 0x00FFFFFF;

I understood !

This was so easy ...

For example, if i want to filter only "FF4",

mask is : 0x00FFF000
Filter is : 0xFF4

I think this will help so many people like me that have brain problem to understand how it works !

1 Like

is that ok to filter only FF4 ?

Thanks

Regards

again my tipp: notate the mask and the filter in binary in two separate lines and you will easily see if it can work (in other words: your examples #6 might not do what you want...)

Setting the filter on the MCP2515 can be mind boggling! :slight_smile:
hopefully this breif explanation and set of examples will help you get a better image of how it should be done:

  1. The Rx Mask is nothing more than a bit level switch to enable or disable the filter bits.
    Assuming an 11 bit Id, if, for example, the Mask is set to a value of 0b00000000111 (0x0007) then only set bits (ie bits 0,1 & 2) of the filter bits will be used to filter the received Id against.

  2. The Rx filter is used to check the received CAN Id against.
    If a match is detected on the active filter bits, then the received CAN message is placed in the Rx buffer, else the CAN message is discarded.

Examples (assuming 11 bit Id)
Eg 1/
Mask Value: 0b11111111111 (0x03FF) (all filter bits enabled)
Filter Value: 0b11111111111 (0x03FF)
The Rx buffer will only receive CAN frames with an Id of 0x03FF

Eg 2/
Mask Value: 0b11111111111 (0x03FF) (all filter bits enabled)
Filter Value: 0b11111111110 (0x03FE)
The Rx buffer will only receive CAN frames with an Id of 0x03FE

Eg 3/
Mask Value: 0b11111111110 (0x03FE)
Filter Value: 0b11111111111 (0x03FF)
The Rx buffer will receive CAN frames with an Id of 0x03FE or 0x03FF as bit 0 of the filter bit is disabled.

Eg 4/
Mask Value: 0b11111111110 (0x03FE)
Filter Value: 0b11111111110 (0x03FE)
The Rx buffer will receive CAN frames with an Id of 0x03FE or 0x03FF as bit 0 of the filter bit is disabled.

Eg 5/
Mask Value: 0b11111111111 (0x03FF) (all filter bits enabled)
Filter Value: 0b11111111000 (0x03F8)
The Rx buffer will only receive CAN frames with an Id of 0x03F8

Eg 6/
Mask Value: 0b11111111000 (0x03F8)
Filter Value: 0b11111111000 (0x03F8)
The Rx buffer will receive CAN frames with an Id of 0x03F8,0x03F9,0x03FA,0x03FB,0x03FC,0x03FD,0x03FE,0x03FF as bit 0,1 & 2 of the filter bits are disabled.

Eg 7/
Mask Value: 0b11111111111 (0x03FF) (all filter bits enabled)
Filter Value: 0b00000000000 (0x0000)
The Rx buffer will only receive CAN frames with an Id of 0x000

Eg 8/
Mask Value: 0b11111111111 (0x03FF) (all filter bits enabled)
Filter Value: 0b00000000001 (0x0001)
The Rx buffer will only receive CAN frames with an Id of 0x001

Eg 9/
Mask Value: 0b11111111000 (0x03F8)
Filter Value: 0b00000000000 (0x0000)
The Rx buffer will receive CAN frames with an Id of 0x000,0x001,0x002,0x003,0x004,0x005,0x006,0x007 as bit 0,1 & 2 of the filter bits are disabled.

Eg 10/
Mask Value: 0b00000000111 (0x0007)
Filter Value: 0b00000000000 (0x0000)
The Rx buffer will receive CAN frames that end either 0,1,2,3,4,5,6,7 (0x121 = accepted, 0x0128 = discarded)

1 Like

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