打印
[STM32H7]

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

[复制链接]
805|1
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
drer|  楼主 | 2024-2-18 17:29 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
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);

}

————————————————

                            版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

原文链接:https://blog.csdn.net/zhuimeng_ruili/article/details/134854662

使用特权

评论回复
沙发
weifeng90| | 2024-2-19 07:50 | 只看该作者
关键要明白CAN的帧过滤机制

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

60

主题

3315

帖子

4

粉丝