init_Mask()

Description

On an 86Duino platform, there are 32 group of CAN ID mask. When there are data from external CAN device, the mask is used to determine the range of device ID (0 ~ N) which their data is to be handled. Combine with the init_Filt() function, this function can limit the range of CAN ID to be handled, within the A ~ B range. When the init_Mask() and ini_Filt() function are not called within a sketch, the CAN function library is set to accept data from all CAN ID.

Syntax


CAN.init_Mask(num, ext, ulData)

Parameters

num:Mask ID, within the 0 ~ 31 range.
ext:One of the following 4 data transmission format.

  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:3. CAN_STDID_REMOTE: Standard remote data frame, comply with CAN 2.0A standard. ID range: 0 ~ 0x7FF
  4. CAN_EXTID_REMOTE:4. CAN_EXTID_REMOTE: Extended remote data frame, comply with CAN 2.0B standard. ID range: 0 ~ 0x1FFFFFFF

ulData:Mask value. Refer to the following examples:

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

        Range of CAN ID to receive data: 0x00 ~ 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_Filt()
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.