CAN过滤

 两种过滤模式(列表模式与掩码模式)

列表模式:与ID一致

掩码模式:ID的部分一致(置1)

32位列表模式一个ID占16位,一共可以给4个ID

16位列表模式一个ID占32位,一共可以给2个ID

1 32 位过滤器,包括: STDID[10:0] EXTID[17:0] IDE RTR
2 16 位过滤器,包括: STDID[10:0] IDE RTR EXTID[17:15]

标准CAN ID与扩展CAN ID

标准的ID放入16位中的高11位(所以要<<5),扩展帧只能存ID的15-17于低三位,另外两位存IDE位和RTR位,分别表示扩展帧(1)/标准帧(0),数据帧(0)/遥控帧(1)。 

扩展帧的ID是29位,放入寄存器的高29位,低位放IDE(1),RTR(0)和0(一共29位,取出ID的高16位所以>>13

 

​​​​​​​

代码

//16位宽列表
void CANFilterConfig_Scale16_IdList(void)   
{
	CAN_FilterInitTypeDef  	CAN_FilterInitStructure;
  uint32_t StdId1 =0x65;						//这里采用4个标准CAN ID作为例子101 103 106 114
  uint32_t StdId2 =0x67;
  uint32_t StdId3 =0x6A;
  uint32_t StdId4 =0x72;
  
  CAN_FilterInitStructure.CAN_FilterNumber = 1;				//使用过滤器1
  CAN_FilterInitStructure.CAN_FilterMode = CAN_FilterMode_IdList;		//设为列表模式
  CAN_FilterInitStructure.CAN_FilterScale = CAN_FilterScale_16bit;	//位宽设置为16位
  CAN_FilterInitStructure.CAN_FilterIdHigh = StdId1<<5;	 //4个标准CAN ID分别放入到4个存储中
  CAN_FilterInitStructure.CAN_FilterIdLow = StdId2<<5;
  CAN_FilterInitStructure.CAN_FilterMaskIdHigh = StdId3<<5;
  CAN_FilterInitStructure.CAN_FilterMaskIdLow = StdId4<<5;
  CAN_FilterInitStructure.CAN_FilterFIFOAssignment = 0;			//接收到的报文放入到FIFO0中
  CAN_FilterInitStructure.CAN_FilterActivation = ENABLE;

	CAN_FilterInit(&CAN_FilterInitStructure);			//滤波器初始化
}
#if CAN_RX0_INT_ENABLE	//使能RX0中断
//中断服务函数			    
void USB_LP_CAN1_RX0_IRQHandler(void)
{
  	CanRxMsg RxMessage;
	u8 i=0;
	
	if(CAN_MessagePending(CAN1,CAN_FIFO0)==0)
	{	
		CAN_ClearITPendingBit(CAN1,CAN_IT_FMP0);
		return;		//没有接收到数据,直接退出 
	}
	CAN_Receive(CAN1, CAN_FIFO0, &RxMessage);//读取数据	
	
	if(RxMessage.Data[0] != CAN_ID)
	{	
		
		CAN_ClearITPendingBit(CAN1,CAN_IT_FMP0);
		return;		//没有接收到数据,直接退出 
	}
	


	CAN_ClearITPendingBit(CAN1,CAN_IT_FMP0); //清除中断挂起
}
#endif
 //32位宽列表
void CANFilterConfig_Scale32_IdList(void)  
{
	CAN_FilterInitTypeDef  	CAN_FilterInitStructure;
  uint32_t StdId =0x321;				//这里写入两个CAN ID,一个位标准CAN ID
  uint32_t ExtId =0x7890abc;			//一个位扩展CAN ID
  
  CAN_FilterInitStructure.CAN_FilterNumber = 0;				//使用过滤器0
  CAN_FilterInitStructure.CAN_FilterMode = CAN_FilterMode_IdList;		//设为列表模式
  CAN_FilterInitStructure.CAN_FilterScale = CAN_FilterScale_32bit;	//配置为32位宽
  CAN_FilterInitStructure.CAN_FilterIdHigh = StdId<<5;			//基本ID放入到STID中
  CAN_FilterInitStructure.CAN_FilterIdLow = 0|CAN_ID_STD;			//设置IDE位为0
  CAN_FilterInitStructure.CAN_FilterMaskIdHigh = 0x7890abc>>13;//ID高16位
  CAN_FilterInitStructure.CAN_FilterMaskIdLow = ((0x7890abc<<3)&0xffff)|(0x1<<2);	//ID的低13位,设置IDE位为1
  CAN_FilterInitStructure.CAN_FilterFIFOAssignment = 0;			//接收到的报文放入到FIFO0中
  CAN_FilterInitStructure.CAN_FilterActivation = ENABLE;

	CAN_FilterInit(&CAN_FilterInitStructure);			//滤波器初始化
}
void CANFilterConfig_Scale16_IdMask(void)   
{
	CAN_FilterInitTypeDef  	CAN_FilterInitStructure;
  uint32_t StdId1 =0x01;						//0x0*和0x1*都可以通过
  uint32_t StdId2 =0x10;
  uint32_t StdId3 =0x70;
  uint32_t StdId4 =0xf0;
  
  CAN_FilterInitStructure.CAN_FilterNumber = 5;				//使用过滤器1
  CAN_FilterInitStructure.CAN_FilterMode = CAN_FilterMode_IdMask;		//配置为掩码模式
  CAN_FilterInitStructure.CAN_FilterScale = CAN_FilterScale_16bit;	//位宽设置为16位
  CAN_FilterInitStructure.CAN_FilterIdHigh = StdId1<<5;	 //CAN ID分别放入到4个存储中
  CAN_FilterInitStructure.CAN_FilterIdLow = StdId2<<5;
  CAN_FilterInitStructure.CAN_FilterMaskIdHigh = StdId3<<5;
  CAN_FilterInitStructure.CAN_FilterMaskIdLow = StdId4<<5;
  CAN_FilterInitStructure.CAN_FilterFIFOAssignment = 0;			//接收到的报文放入到FIFO0中
  CAN_FilterInitStructure.CAN_FilterActivation = ENABLE;

	CAN_FilterInit(&CAN_FilterInitStructure);			//滤波器初始化
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值