想要实现只接受一个扩展ID发来的数据,尝试对CAN初始化进行配置,配置的初始化程序如下:
u32 ext_id=0x1800f001;//需要接收的特定ID数据
CAN_FilterInitStructure.CAN_FilterNumber=0; //过滤器0
CAN_FilterInitStructure.CAN_FilterMode=CAN_FilterMode_IdMask; //屏蔽位模式
CAN_FilterInitStructure.CAN_FilterScale=CAN_FilterScale_32bit; //32位宽
CAN_FilterInitStructure.CAN_FilterIdHigh=((ext_id<<3)>>16)&0xffff; //32位ID 不屏蔽的话都设置为0
CAN_FilterInitStructure.CAN_FilterIdLow=(ext_id<<3)&0xffff | CAN_ID_EXT;
CAN_FilterInitStructure.CAN_FilterMaskIdHigh=(((ext_id<<3)|0x02)>>16)&0xffff;//32位MASK 或0X02只接受数据帧,不接受远程帧
CAN_FilterInitStructure.CAN_FilterMaskIdLow=(((ext_id<<3)|0x02))&0xffff;
CAN_FilterInitStructure.CAN_FilterFIFOAssignment=CAN_Filter_FIFO0;//过滤器0关联到FIFO0
CAN_FilterInitStructure.CAN_FilterActivation=ENABLE;//激活过滤器0
CAN_FilterInit(&CAN_FilterInitStructure); //滤波器初始化
设置后,能够接收0x1800f001,0x1800f003,0x1800f005,0x1800f007,0x1800f009这样的数据,好像只判断了最后一位。
未能满足想要的效果。
CAN_FilterInitStructure.CAN_FilterNumber=0; //过滤器0
CAN_FilterInitStructure.CAN_FilterMode=CAN_FilterMode_IdMask; //屏蔽位模式
CAN_FilterInitStructure.CAN_FilterScale=CAN_FilterScale_32bit; //32位宽
CAN_FilterInitStructure.CAN_FilterIdHigh=((ext_id<<3)>>16)&0xffff; //32位ID 不屏蔽的话都设置为0
CAN_FilterInitStructure.CAN_FilterIdLow=(ext_id<<3)&0xffff | CAN_ID_EXT;
CAN_FilterInitStructure.CAN_FilterMaskIdHigh= 0xffff;//32位MASK 不接受远程帧 设置为1要求ID必须匹配filter
CAN_FilterInitStructure.CAN_FilterMaskIdLow=(((0xffff<<3)|0x02))&0xffff; //或0X02只接受数据帧,
CAN_FilterInitStructure.CAN_FilterFIFOAssignment=CAN_Filter_FIFO0;//过滤器0关联到FIFO0
CAN_FilterInitStructure.CAN_FilterActivation=ENABLE;//激活过滤器0
CAN_FilterInit(&CAN_FilterInitStructure); //滤波器初始化
此时,只接受0x1800f001这个ID发送的数据帧,其他的不接受。