CAN协议 筛选器解析
(5条消息) CAN芯片_ TJA1051T/3_R.ui..的博客-CSDN博客_can芯片
16位筛选器--标识符掩码模式解析
((((u32)0x13<<21)|CAN_ID_STD|CAN_RTR_DATA)&0xFFFF0000)>>16;
此代码执行如上图所示,后面5位不是ID的数据,是其它数据,标识符掩码进行判断的时候不会对其进行判断。
(6条消息) stm32之can总线过滤器的标准帧、扩展帧、远程帧等_hanchaoman的专栏-CSDN博客
stm32的can总线的配置如下:
CAN_InitStructure.CAN_TTCM=DISABLE;//禁止时间触发通信模式
CAN_InitStructure.CAN_ABOM=DISABLE;
CAN_InitStructure.CAN_AWUM=DISABLE;
CAN_InitStructure.CAN_NART=DISABLE;//CAN报文只被发送1次,不管发送的结果如何(成功、出错或仲裁丢失)
CAN_InitStructure.CAN_RFLM=DISABLE;
CAN_InitStructure.CAN_TXFP=DISABLE;
CAN_InitStructure.CAN_Mode=CAN_Mode_Normal;
//CAN_Mode_LoopBack
//CAN_Mode_Normal
CAN_InitStructure.CAN_SJW=CAN_SJW_1tq;
CAN_InitStructure.CAN_BS1=CAN_BS1_5tq;//1--16
CAN_InitStructure.CAN_BS2=CAN_BS2_2tq;//1--8
CAN_InitStructure.CAN_Prescaler=2;
CAN_InitStructure.CAN_ABOM=DISABLE;
CAN_InitStructure.CAN_AWUM=DISABLE;
CAN_InitStructure.CAN_NART=DISABLE;//CAN报文只被发送1次,不管发送的结果如何(成功、出错或仲裁丢失)
CAN_InitStructure.CAN_RFLM=DISABLE;
CAN_InitStructure.CAN_TXFP=DISABLE;
CAN_InitStructure.CAN_Mode=CAN_Mode_Normal;
//CAN_Mode_LoopBack
//CAN_Mode_Normal
CAN_InitStructure.CAN_SJW=CAN_SJW_1tq;
CAN_InitStructure.CAN_BS1=CAN_BS1_5tq;//1--16
CAN_InitStructure.CAN_BS2=CAN_BS2_2tq;//1--8
CAN_InitStructure.CAN_Prescaler=2;
CAN_Init(&CAN_InitStructure);
/* CAN filter init */
CAN_FilterInitStructure.CAN_FilterNumber=0;//选择过滤器0
CAN_FilterInitStructure.CAN_FilterMode=CAN_FilterMode_IdMask;//指定过滤器被设置为标识符屏蔽模式
CAN_FilterInitStructure.CAN_FilterScale=CAN_FilterScale_32bit;//给出过滤器位宽为32位
CAN_FilterInitStructure.CAN_FilterNumber=0;//选择过滤器0
CAN_FilterInitStructure.CAN_FilterMode=CAN_FilterMode_IdMask;//指定过滤器被设置为标识符屏蔽模式
CAN_FilterInitStructure.CAN_FilterScale=CAN_FilterScale_32bit;//给出过滤器位宽为32位
下面根据设置的参数不同来决定can总线can总线的配置情况:
1、对扩展数据帧进行过滤:(只接收扩展数据帧)
CAN_FilterInitStructure.CAN_FilterIdHigh = (((u32)slave_id<<3)&0xFFFF0000)>>16;
CAN_FilterInitStructure.CAN_FilterIdLow = (((u32)slave_id<<3)|CAN_ID_EXT|CAN_RTR_DATA)&0xFFFF;
CAN_FilterInitStructure.CAN_FilterMaskIdHigh = 0xFFFF;
CAN_FilterInitStructure.CAN_FilterMaskIdLow = 0xFFFF;
(注:标准帧数据帧、标准远程帧和扩展远程帧均被过滤)
2、对扩展远程帧过滤:(只接收扩展远程帧)
CAN_FilterInitStructure.CAN_FilterIdHigh = (((u32)slave_id<<3)&0xFFFF0000)>>16;
CAN_FilterInitStructure.CAN_FilterIdLow = (((u32)slave_id<<3)|CAN_ID_EXT|CAN_RTR_REMOTE)&0xFFFF;
CAN_FilterInitStructure.CAN_FilterMaskIdHigh = 0xFFFF;
CAN_FilterInitStructure.CAN_FilterMaskIdLow = 0xFFFF;
CAN_FilterInitStructure.CAN_FilterIdHigh = (((u32)slave_id<<3)&0xFFFF0000)>>16;
CAN_FilterInitStructure.CAN_FilterIdLow = (((u32)slave_id<<3)|CAN_ID_EXT|CAN_RTR_DATA)&0xFFFF;
CAN_FilterInitStructure.CAN_FilterMaskIdHigh = 0xFFFF;
CAN_FilterInitStructure.CAN_FilterMaskIdLow = 0xFFFF;
(注:标准帧数据帧、标准远程帧和扩展远程帧均被过滤)
2、对扩展远程帧过滤:(只接收扩展远程帧)
CAN_FilterInitStructure.CAN_FilterIdHigh = (((u32)slave_id<<3)&0xFFFF0000)>>16;
CAN_FilterInitStructure.CAN_FilterIdLow = (((u32)slave_id<<3)|CAN_ID_EXT|CAN_RTR_REMOTE)&0xFFFF;
CAN_FilterInitStructure.CAN_FilterMaskIdHigh = 0xFFFF;
CAN_FilterInitStructure.CAN_FilterMaskIdLow = 0xFFFF;
3、对标准远程帧过滤:(只接收标准远程帧)
CAN_FilterInitStructure.CAN_FilterIdHigh = (((u32)slave_id<<21)&0xffff0000)>>16;
CAN_FilterInitStructure.CAN_FilterIdLow = (((u32)slave_id<<21)|CAN_ID_STD|CAN_RTR_REMOTE)&0xffff;
CAN_FilterInitStructure.CAN_FilterMaskIdHigh = 0xFFFF;
CAN_FilterInitStructure.CAN_FilterMaskIdLow = 0xFFFF;
4、对标准数据帧过滤:(只接收标准数据帧)
CAN_FilterInitStructure.CAN_FilterIdHigh = (((u32)slave_id<<21)&0xffff0000)>>16;
CAN_FilterInitStructure.CAN_FilterIdLow = (((u32)slave_id<<21)|CAN_ID_STD|CAN_RTR_DATA)&0xffff;
CAN_FilterInitStructure.CAN_FilterMaskIdHigh = 0xFFFF;
CAN_FilterInitStructure.CAN_FilterMaskIdLow = 0xFFFF;
CAN_FilterInitStructure.CAN_FilterIdHigh = (((u32)slave_id<<21)&0xffff0000)>>16;
CAN_FilterInitStructure.CAN_FilterIdLow = (((u32)slave_id<<21)|CAN_ID_STD|CAN_RTR_REMOTE)&0xffff;
CAN_FilterInitStructure.CAN_FilterMaskIdHigh = 0xFFFF;
CAN_FilterInitStructure.CAN_FilterMaskIdLow = 0xFFFF;
4、对标准数据帧过滤:(只接收标准数据帧)
CAN_FilterInitStructure.CAN_FilterIdHigh = (((u32)slave_id<<21)&0xffff0000)>>16;
CAN_FilterInitStructure.CAN_FilterIdLow = (((u32)slave_id<<21)|CAN_ID_STD|CAN_RTR_DATA)&0xffff;
CAN_FilterInitStructure.CAN_FilterMaskIdHigh = 0xFFFF;
CAN_FilterInitStructure.CAN_FilterMaskIdLow = 0xFFFF;
5、对扩展帧进行过滤:(扩展帧不会被过滤掉)
CAN_FilterInitStructure.CAN_FilterIdHigh = (((u32)slave_id<<3)&0xFFFF0000)>>16;
CAN_FilterInitStructure.CAN_FilterIdLow = (((u32)slave_id<<3)|CAN_ID_EXT)&0xFFFF;
CAN_FilterInitStructure.CAN_FilterMaskIdHigh = 0xFFFF;
CAN_FilterInitStructure.CAN_FilterMaskIdLow = 0xFFFC;
6、对标准帧进行过滤:(标准帧不会被过滤掉)
CAN_FilterInitStructure.CAN_FilterIdHigh = (((u32)slave_id<<21)&0xffff0000)>>16;
CAN_FilterInitStructure.CAN_FilterIdLow = (((u32)slave_id<<21)|CAN_ID_STD)&0xffff;
CAN_FilterInitStructure.CAN_FilterMaskIdHigh = 0xFFFF;
CAN_FilterInitStructure.CAN_FilterMaskIdLow = 0xFFFC;
注:slave_id为要过滤的id号。
CAN_FilterInitStructure.CAN_FilterIdHigh = (((u32)slave_id<<3)&0xFFFF0000)>>16;
CAN_FilterInitStructure.CAN_FilterIdLow = (((u32)slave_id<<3)|CAN_ID_EXT)&0xFFFF;
CAN_FilterInitStructure.CAN_FilterMaskIdHigh = 0xFFFF;
CAN_FilterInitStructure.CAN_FilterMaskIdLow = 0xFFFC;
6、对标准帧进行过滤:(标准帧不会被过滤掉)
CAN_FilterInitStructure.CAN_FilterIdHigh = (((u32)slave_id<<21)&0xffff0000)>>16;
CAN_FilterInitStructure.CAN_FilterIdLow = (((u32)slave_id<<21)|CAN_ID_STD)&0xffff;
CAN_FilterInitStructure.CAN_FilterMaskIdHigh = 0xFFFF;
CAN_FilterInitStructure.CAN_FilterMaskIdLow = 0xFFFC;
注:slave_id为要过滤的id号。
(7条消息) CAN通讯难点———验收筛选器_Auto0313的博客-CSDN博客_can筛选器 (印象笔记STM32中有)
模式1:32位掩码模式
void CAN_Filter_Config(void) { CAN_FilterInitTypeDef CAN_FilterInitStructure; /*CAN筛选器初始化*/ CAN_FilterInitStructure.CAN_FilterNumber=0; //筛选器组0 CAN_FilterInitStructure.CAN_FilterMode=CAN_FilterMode_IdMask; //工作在掩码模式 CAN_FilterInitStructure.CAN_FilterScale=CAN_FilterScale_32bit; //筛选器位宽为单个32位。 /* 使能筛选器,按照标志的内容进行比对筛选,扩展ID不是如下的就抛弃掉,是的话,会存入FIFO0。 */ CAN_FilterInitStructure.CAN_FilterIdHigh= ((((u32)0x1314<<3)|CAN_ID_EXT|CAN_RTR_DATA)&0xFFFF0000)>>16; //要筛选的ID高位 CAN_FilterInitStructure.CAN_FilterIdLow= (((u32)0x1314<<3)|CAN_ID_EXT|CAN_RTR_DATA)&0xFFFF; //要筛选的ID低位 CAN_FilterInitStructure.CAN_FilterMaskIdHigh= 0xFFFF; //筛选器高16位每位必须匹配 CAN_FilterInitStructure.CAN_FilterMaskIdLow= 0xFFFF; //筛选器低16位每位必须匹配 CAN_FilterInitStructure.CAN_FilterFIFOAssignment=CAN_Filter_FIFO0 ; //筛选器被关联到FIFO0 CAN_FilterInitStructure.CAN_FilterActivation=ENABLE; //使能筛选器 CAN_FilterInit(&CAN_FilterInitStructure); /*CAN通信中断使能*/ CAN_ITConfig(CANx, CAN_IT_FMP0, ENABLE); }
模式2:32位标识符模式
void CAN_Filter_Config(void) { CAN_FilterInitTypeDef CAN_FilterInitStructure; /*CAN筛选器初始化*/ CAN_FilterInitStructure.CAN_FilterNumber=0; //筛选器组0 CAN_FilterInitStructure.CAN_FilterMode=CAN_FilterMode_IdList; //工作在标识符模式 CAN_FilterInitStructure.CAN_FilterScale=CAN_FilterScale_32bit; //筛选器位宽为单个32位。 /* 使能筛选器,按照标志的内容进行比对筛选,扩展ID不是如下的就抛弃掉,是的话,会存入FIFO0。 */ CAN_FilterInitStructure.CAN_FilterIdHigh= ((((u32)0x13<<3)|CAN_ID_EXT|CAN_RTR_DATA)&0xFFFF0000)>>16; CAN_FilterInitStructure.CAN_FilterIdLow= (((u32)0x13<<3)|CAN_ID_EXT|CAN_RTR_DATA)&0xFFFF; CAN_FilterInitStructure.CAN_FilterMaskIdHigh= ((((u32)0x14<<3)|CAN_ID_EXT|CAN_RTR_DATA)&0xFFFF0000)>>16; CAN_FilterInitStructure.CAN_FilterMaskIdLow= (((u32)0x14<<3)|CAN_ID_EXT|CAN_RTR_DATA)&0xFFFF; CAN_FilterInitStructure.CAN_FilterFIFOAssignment=CAN_Filter_FIFO0 ; //筛选器被关联到FIFO0 CAN_FilterInitStructure.CAN_FilterActivation=ENABLE; //使能筛选器 CAN_FilterInit(&CAN_FilterInitStructure); /*CAN通信中断使能*/ CAN_ITConfig(CANx, CAN_IT_FMP0, ENABLE); }
模式3:16位掩码模式
void CAN_Filter_Config(void) { CAN_FilterInitTypeDef CAN_FilterInitStructure; /*CAN筛选器初始化*/ CAN_FilterInitStructure.CAN_FilterNumber=0; //筛选器组0 CAN_FilterInitStructure.CAN_FilterMode=CAN_FilterMode_IdMask; //工作在掩码模式 CAN_FilterInitStructure.CAN_FilterScale=CAN_FilterScale_16bit; //筛选器位宽为单个16位。 /* 使能筛选器,按照标志的内容进行比对筛选,扩展ID不是如下的就抛弃掉,是的话,会存入FIFO0。 */ CAN_FilterInitStructure.CAN_FilterIdHigh= ((((u32)0x13<<21)|CAN_ID_STD|CAN_RTR_DATA)&0xFFFF0000)>>16; CAN_FilterInitStructure.CAN_FilterIdLow= 0xFFFF; CAN_FilterInitStructure.CAN_FilterMaskIdHigh= ((((u32)0x24<<21)|CAN_ID_STD|CAN_RTR_DATA)&0xFFFF0000)>>16; CAN_FilterInitStructure.CAN_FilterMaskIdLow= 0xFFFF; CAN_FilterInitStructure.CAN_FilterFIFOAssignment=CAN_Filter_FIFO0 ; //筛选器被关联到FIFO0 CAN_FilterInitStructure.CAN_FilterActivation=ENABLE; //使能筛选器 CAN_FilterInit(&CAN_FilterInitStructure); /*CAN通信中断使能*/ CAN_ITConfig(CANx, CAN_IT_FMP0, ENABLE); }
模式4:16位标识符模式
void CAN_Filter_Config(void) { CAN_FilterInitTypeDef CAN_FilterInitStructure; /*CAN筛选器初始化*/ CAN_FilterInitStructure.CAN_FilterNumber=0; //筛选器组0 CAN_FilterInitStructure.CAN_FilterMode=CAN_FilterMode_IdList; //工作在标识符模式 CAN_FilterInitStructure.CAN_FilterScale=CAN_FilterScale_16bit; //筛选器位宽为单个16位。 /* 使能筛选器,按照标志的内容进行比对筛选,扩展ID不是如下的就抛弃掉,是的话,会存入FIFO0。 */ CAN_FilterInitStructure.CAN_FilterIdHigh= ((((u32)0x13<<21)|CAN_ID_STD|CAN_RTR_DATA)&0xFFFF0000)>>16; CAN_FilterInitStructure.CAN_FilterIdLow= ((((u32)0x13<<21)|CAN_ID_STD|CAN_RTR_DATA)&0xFFFF0000)>>16; CAN_FilterInitStructure.CAN_FilterMaskIdHigh= ((((u32)0x24<<21)|CAN_ID_STD|CAN_RTR_DATA)&0xFFFF0000)>>16; CAN_FilterInitStructure.CAN_FilterMaskIdLow= ((((u32)0x24<<21)|CAN_ID_STD|CAN_RTR_DATA)&0xFFFF0000)>>16; CAN_FilterInitStructure.CAN_FilterFIFOAssignment=CAN_Filter_FIFO0 ; //筛选器被关联到FIFO0 CAN_FilterInitStructure.CAN_FilterActivation=ENABLE; //使能筛选器 CAN_FilterInit(&CAN_FilterInitStructure); /*CAN通信中断使能*/ CAN_ITConfig(CANx, CAN_IT_FMP0, ENABLE); }
要注意:在主函数(main.c)里和中断函数(stm32f10x_it.c 文件)里也要按相应的要求改。
如果想要修改关联到FIFO0或者FIFO1,中断服务函数名要改。
#define CAN_RX_IRQ USB_LP_CAN1_RX0_IRQn //当使用CAN_Filter_FIFO0时 #define CAN_RX_IRQHandler USB_LP_CAN1_RX0_IRQHandler #define CAN_RX_IRQ1 CAN1_RX1_IRQn //当使用CAN_Filter_FIFO1时 #define CAN_RX_IRQHandler1 CAN1_RX1_IRQHandler
筛选器组修改如下:
void CAN_Filter_Config(void) { CAN_FilterInitTypeDef CAN_FilterInitStructure; /*CAN筛选器初始化*/ CAN_FilterInitStructure.CAN_FilterNumber=1; //筛选器组1 CAN_FilterInitStructure.CAN_FilterMode=CAN_FilterMode_IdMask; //工作在掩码模式 CAN_FilterInitStructure.CAN_FilterScale=CAN_FilterScale_16bit; //筛选器位宽为单个16位。 /* 使能筛选器,按照标志的内容进行比对筛选,扩展ID不是如下的就抛弃掉,是的话,会存入FIFO1。 */ CAN_FilterInitStructure.CAN_FilterIdHigh= ((((u32)0x67<<21)|CAN_ID_STD|CAN_RTR_DATA)&0xFFFF0000)>>16; CAN_FilterInitStructure.CAN_FilterIdLow= 0xFFFF; CAN_FilterInitStructure.CAN_FilterMaskIdHigh= ((((u32)0x45<<21)|CAN_ID_STD|CAN_RTR_DATA)&0xFFFF0000)>>16; CAN_FilterInitStructure.CAN_FilterMaskIdLow= 0xFFFF; CAN_FilterInitStructure.CAN_FilterFIFOAssignment=CAN_Filter_FIFO1 ; //筛选器被关联到FIFO1 CAN_FilterInitStructure.CAN_FilterActivation=ENABLE; //使能筛选器 CAN_FilterInit(&CAN_FilterInitStructure); /*CAN通信中断使能*/ CAN_ITConfig(CANx, CAN_IT_FMP1, ENABLE);