init_Filt()

Description

Function to filter CAN ID. On the 86Duino platform, there are 32 groups of CAN ID filters. When receiving data from the CAN bus, these filter ID can be used to determine whether the data need to be handled.
Combining with the init_Mask() function, receiving data can be limited to CAN ID within the A ~ B range.
When this function is used in a sketch without calling the init_Mask() or init)Filt() function first, data from all CAN ID will be accepted.

Syntax


CAN.init_Filt(num, ext, ulData)

Parameters

num:Identify filter ID to be used, within the 0 ~ 31 range.
ext:data transmission format. There are 4 different options:

  1. CAN_STDID:Standard data frame, comply with CAN 2.0A standard. ID range: 0 ~ 0x7FF
  2. CAN_EXTID:Extended data frame, comply with CAN 2.0B standard. ID range: 0 ~ 0x1FFFFFFF
  3. CAN_STDID_REMOTE:Standard remote frame, comply with CAN 2.0A standard. ID range: 0 ~ 0x7FF
  4. CAN_EXTID_REMOTE:Extended remote frame, comply with CAN 2.0B standard. ID range: 0 ~ 0x1FFFFFFF

ulData:Filter value. Refer to the following example:

        Without mask:
        0 0 0 0 0 0 1 1    Filter ID value: 0x03
        0 0 0 0 0 0 0 0    Mask: 0x00

        Range of CAN ID to receive data::0x03

        With mask
        0 0 0 1 1 0 0 1    Filter ID value: 0x19
        0 0 0 0 0 0 0 1    Mask: 0x01

        Range of CAN ID to receive data:0x18、0x19

        0 1 0 1 1 1 0 0    Filter ID value: 0x5C
        0 0 0 0 0 1 1 1    Mask: 0x07

        Range of CAN ID to receive data:0x58 ~ 0x5F

Returns

CAN_FAIL:Setting failed
CAN_OK:Setting success

Examples

#include <CANBus.h>
unsigned char buf[8] = {0, 1, 2, 3, 4, 5, 6, 7};

void setup() {
    Serial.begin(115200);
    CAN.begin(CAN_500KBPS); // Configure CAN bus transmission speed to 500KBPS
    CAN.init_Filt(0, CAN_STDID, 0x019); // Configure filter ID 0 to 0x019, using Standard data frame
    CAN.init_Mask(0, CAN_STDID, 0x01); // Configure mask for filter ID 0 to 0x01, using standard data frame
}

void loop() {
    CAN.beginTransmission(0x00, CAN_STDID);    // Set external CAN ID to 0x00, using standard data frame
    CAN.write(buf, 8);                         // Transmit 8 bytes of data
    CAN.endTransmission();                     // End transmission
    delay(10);
}

See also

init_Mask()
beginTransmission()
write()
endTransmission()


Libraries Reference Home

The text of the 86Duino reference is licensed under a Creative Commons Attribution-ShareAlike 3.0 License. Code samples in the reference are released into the public domain.