在运用STM32的CAN控制器进行数据收发,当用到位屏蔽形式的时分,就要设置过滤器了,这个关系到是否能够接纳到想要的数据。下面针对几种不同状况对CAN过滤器(Filter)进行设置。
CAN_FilterInitStructure.CAN_FilterMode=CAN_FilterMode_IdMask;//标明符屏蔽形式
CAN_FilterInitStructure.CAN_FilterScale=CAN_FilterScale_32bit;
1、对扩展数据帧进行过滤:(只接纳扩展数据帧)
CAN_FilterInitStructure.CAN_FilterIdHigh=(EXT_ID》》13)&0xFFFF;
CAN_FilterInitStructure.CAN_FilterIdLow=((EXT_ID《《3)|(0x04))&0xFFFF;
//这儿设置|0x04(0b0100)便是为了IDE=1,RTR=0,
//这儿也能够写为(EXT_ID《《3)|CAN_ID_EXT|CAN_RTR_ROMOTE,作用相同,下同
CAN_FilterInitStructure.CAN_FilterMaskIdHigh=0xFFFF;
CAN_FilterInitStructure.CAN_FilterMaskIdLow=0xFFFF;
2、对扩展长途帧过滤:(只接纳扩展长途帧)
CAN_FilterInitStructure.CAN_FilterIdHigh=(EXT_ID》》13)&0xFFFF;
CAN_FilterInitStructure.CAN_FilterIdLow=((EXT_ID《《3)|(0x06)&0xFFFF;
//这儿设置|0x06(0b0110)便是为了IDE=1,RTR=1
CAN_FilterInitStructure.CAN_FilterMaskIdHigh=0xFFFF;
CAN_FilterInitStructure.CAN_FilterMaskIdLow=0xFFFF;
3、对规范长途帧过滤:(只接纳规范长途帧)
CAN_FilterInitStructure.CAN_FilterIdHigh=((STD_ID《《21)&0xFFFF0000)》》16;
CAN_FilterInitStructure.CAN_FilterIdLow=((STD_ID《《21)|(0x0))&0xFFFF;
//这儿设置|0x0(0b0000)便是为了IDE=0,RTR=0
CAN_FilterInitStructure.CAN_FilterMaskIdHigh=0xFFE0;//0xFFE0表明要重视11位的STD_ID
CAN_FilterInitStructure.CAN_FilterMaskIdLow=0x0006;//0110,表明要重视IDE和RTR位,只承受规范,不承受扩展
4、对规范数据帧过滤:(只接纳规范数据帧)
CAN_FilterInitStructure.CAN_FilterIdHigh=((STD_ID《《21)&0xFFFF0000)》》16;
CAN_FilterInitStructure.CAN_FilterIdLow=((STD_ID_ID《《21)|(0x06))&0xFFFF;
//这儿设置|0x02(0b0010)便是为了IDE=0,RTR=1
CAN_FilterInitStructure.CAN_FilterMaskIdHigh=0xFFE0;
CAN_FilterInitStructure.CAN_FilterMaskIdLow=0x0006;//只承受规范数据帧,不承受扩展数据帧