该部分程序如下:
CAN_FilterInitStructure.CAN_FilterNumber=14; //选择滤波器组,共14个
CAN_FilterInitStructure.CAN_FilterMode=CAN_FilterMode_IdMask; //过滤模式,选择位屏蔽模式而不是标识符列表模式
CAN_FilterInitStructure.CAN_FilterScale=CAN_FilterScale_32bit;//1个32位寄存器
CAN_FilterInitStructure.CAN_FilterIdHigh=(((u32)0x4321<<3)&0xFFFF0000)>>16;//要过滤的ID高位
CAN_FilterInitStructure.CAN_FilterIdLow=(((u32)0x4321<<3)|CAN_ID_EXT|CAN_RTR_DATA)&0xFFFF;要过滤的ID低位
CAN_FilterInitStructure.CAN_FilterMaskIdHigh=0xffff;//0x0000;//收到的ID每一位必须与必须滤波寄存器中的每一位相等
CAN_FilterInitStructure.CAN_FilterMaskIdLow=0xffff;//0x0000;
CAN_FilterInitStructure.CAN_FilterFIFOAssignment=CAN_FIFO0;
CAN_FilterInitStructure.CAN_FilterActivation=ENABLE;
CAN_FilterInit(&CAN_FilterInitStructure);
/* transmit */
//TxMessage.StdId=0x12;
TxMessage.ExtId=0x1234;
//TxMessage.IDE=CAN_ID_EXT;
TxMessage.IDE=CAN_ID_EXT;//CAN_ID_STD;
TxMessage.RTR=CAN_RTR_DATA;
TxMessage.DLC = 2;
TxMessage.Data[0]='N';
TxMessage.Data[1]='2';
CAN_ITConfig(CAN2, CAN_IT_FMP0, ENABLE);
//TxMessage.Data[0] ='h';
CAN_Transmit(CAN2, &TxMessage);
CAN_FilterIdHigh 和CAN_FilterIdLow为什么这么设置呢,看看下图就明白了。
以上是扩展帧,对于标准帧怎么处理呢
CAN_FilterInitStructure.CAN_FilterIdHigh = (((u32)CAN_ID<<21)&0xFFFF0000)>>16;
CAN_FilterInitStructure.CAN_FilterIdLow = (((u32)CAN_ID<<21)|CAN_ID_STD|CAN_RTR_DATA)&0xFFFF;
CAN_FilterInitStructure.CAN_FilterMaskIdHigh = 0xFFFF;
CAN_FilterInitStructure.CAN_FilterMaskIdLow = 0xFFFF;
上一篇:STM32F107 之USB通信
下一篇:对STM32F107中断向量优先级的理解
推荐阅读最新更新时间:2024-03-16 15:31