STM32同时使用can1与can2,can2无法使用的问题
首先 我们先介绍一下原理
bxCAN 主要特性的其中一点需要注意
在can接收数据时:
● 两个具有三级深度的接收 FIFO
● 可调整的筛选器组:
— CAN1 和 CAN2 之间共享 28 个筛选器组
从这张图片不难看出can1为主机,can2为从机。因此can2的启动前提是已经启动了can1.
- can通讯can2是基于can1的
使用can2之前必须初始化can1 - 因此举例如果使用FIFO0的话,将筛选器分配:
can1的筛选器组就从0开始 范围为0-14
CAN_FilterInitStructure.CAN_FilterNumber=0;
can2的筛选器组就从14开始 范围为14-27
CAN_FilterInitStructure.CAN_FilterNumber=14;
注意:can过滤器配置必须用16进制
很多小伙伴可能懒得看原理,多说无益,咋们直接上代码:
1.can1&can2正常配置
2.配置can总线滤波器:
void can_filter(CAN_HandleTypeDef* hcan)
{
CAN_FilterTypeDef CAN_FilterConfigStructure;
if (hcan == &hcan2)
{
CAN_FilterConfigStructure.FilterBank = 14;//***********************!!
}
if (hcan == &hcan1)
{
CAN_FilterConfigStructure.FilterBank = 0;
}
CAN_FilterConfigStructure.FilterMode = CAN_FILTERMODE_IDMASK;
CAN_FilterConfigStructure.FilterScale = CAN_FILTERSCALE_32BIT;
CAN_FilterConfigStructure.FilterIdHigh = 0x0000;
CAN_FilterConfigStructure.FilterIdLow = 0x0000;
CAN_FilterConfigStructure.FilterMaskIdHigh = 0x0000;
CAN_FilterConfigStructure.FilterMaskIdLow = 0x0000;
CAN_FilterConfigStructure.FilterFIFOAssignment = CAN_RX_FIFO0;
CAN_FilterConfigStructure.SlaveStartFilterBank = 14;
CAN_FilterConfigStructure.FilterActivation = ENABLE;
HAL_CAN_ConfigFilter(hcan, &CAN_FilterConfigStructure);
}
3.主函数里初始化
MX_CAN1_Init();//先初始化can1!!!!!
MX_CAN2_Init();
can_filter(hcan1);
can_filter(hcan2);
4.齐活
如果你觉得还有用就点一个赞叭。
转载:https://blog.csdn.net/qq_43685530/article/details/115423802
查看评论