STM32h7 接收各种can id情况下滤波器的配置

2023-12-14 17:05:50

1、接收所有数据

/* 此处id2都为0,不进行id校验,接收所有数据*/
static void CAN_Filter_Config(void)

{

FDCAN_FilterTypeDef sFilterConfig1;


/* Configure Rx filter */

sFilterConfig1.IdType = FDCAN_STANDARD_ID;

sFilterConfig1.FilterIndex = 0;

sFilterConfig1.FilterType = FDCAN_FILTER_MASK;

sFilterConfig1.FilterConfig = FDCAN_FILTER_TO_RXFIFO0;

sFilterConfig1.FilterID1 = 0x111;

sFilterConfig1.FilterID2 = 0x000; /* For acceptance, MessageID and FilterID1 must match exactly */

HAL_FDCAN_ConfigFilter(&hfdcan1, &sFilterConfig1);

}

2、只接受单个id

/*此处只接收ID为“123”的can数据 */

static void CAN_Filter_Config(void)

{

FDCAN_FilterTypeDef sFilterConfig1;

/* Configure Rx filter */

sFilterConfig1.IdType = FDCAN_STANDARD_ID;

sFilterConfig1.FilterIndex = 0;

sFilterConfig1.FilterType = FDCAN_FILTER_MASK;

sFilterConfig1.FilterConfig = FDCAN_FILTER_TO_RXFIFO0;

sFilterConfig1.FilterID1 = 0x123;

sFilterConfig1.FilterID2 = 0x7FF; /* For acceptance, MessageID and FilterID1 must match exactly */

HAL_FDCAN_ConfigFilter(&hfdcan1, &sFilterConfig1);

}

3、接收两个id

/*此处接收ID为“123”及“456”的can数据 */

static void CAN_Filter_Config(void)

{

FDCAN_FilterTypeDef sFilterConfig1;

/* Configure Rx filter */

sFilterConfig1.IdType = FDCAN_STANDARD_ID;

sFilterConfig1.FilterIndex = 0;

sFilterConfig1.FilterType = FDCAN_FILTER_DUAL;

sFilterConfig1.FilterConfig = FDCAN_FILTER_TO_RXFIFO0;

sFilterConfig1.FilterID1 = 0x123;

sFilterConfig1.FilterID2 = 0x456; /* For acceptance, MessageID and FilterID1 must match exactly */

HAL_FDCAN_ConfigFilter(&hfdcan1, &sFilterConfig1);

}

4、接收一定范围的id

/*此处接收ID在“111”~“666”之间的can数据 */

static void CAN_Filter_Config(void)

{

FDCAN_FilterTypeDef sFilterConfig1;

/* Configure Rx filter */

sFilterConfig1.IdType = FDCAN_STANDARD_ID;

sFilterConfig1.FilterIndex = 0;

sFilterConfig1.FilterType = FDCAN_FILTER_RANGE;

sFilterConfig1.FilterConfig = FDCAN_FILTER_TO_RXFIFO0;

sFilterConfig1.FilterID1 = 0x111;

sFilterConfig1.FilterID2 = 0x666; /* For acceptance, MessageID and FilterID1 must match exactly */

HAL_FDCAN_ConfigFilter(&hfdcan1, &sFilterConfig1);

}

5、接收特定规则的id

/*

FilterType 类型配置为 FDCAN_FILTER_MASK 表示经典的位屏蔽过滤。

FilterID1 = filter 表示 ID。

FilterID2 = mask 表示 ID 屏蔽位,mask 每个 bit 含义:

0: 表示 FilterID1 相应 bit 不关心,该位不用于比较。

1: 表示 FilterID1 相应 bit 必须匹配,即接收到的 ID 位必须与 FilterID1 的相应位一致。

我们这里 FilterID1 = 0x200,bit “1 0 0 0 0 0 0 0 0 0”

        FilterID2 = 0x300 ,bit “1 1 0 0 0 0 0 0 0 0”

表示接收Id bit9必须为1,bit8必须为0,其他bit0~bit7不受限制。也就是接收0x200~0x300之间的数据

*/

static void CAN_Filter_Config(void)

{

FDCAN_FilterTypeDef sFilterConfig1;

/* Configure Rx filter */

sFilterConfig1.IdType = FDCAN_STANDARD_ID;

sFilterConfig1.FilterIndex = 0;

sFilterConfig1.FilterType = FDCAN_FILTER_MASK;

sFilterConfig1.FilterConfig = FDCAN_FILTER_TO_RXFIFO0;

sFilterConfig1.FilterID1 = 0x200;

sFilterConfig1.FilterID2 = 0x300; /* For acceptance, MessageID and FilterID1 must match exactly */

HAL_FDCAN_ConfigFilter(&hfdcan1, &sFilterConfig1);

}

文章来源:https://blog.csdn.net/zhuimeng_ruili/article/details/134854662
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。