can 总线过滤器

2019-10-11 15:53发布

can1filter.FilterFIFOAssignment=CAN_FILTER_FIFO0;
can1filter.FilterIdHigh=0x1200;
can1filter.FilterIdLow=0x0000;
can1filter.FilterMaskIdHigh=0xff00;//ÉèÖõÄidÊÇ10000000000000
can1filter.FilterMaskIdLow=0x0000;
can1filter.FilterMode=CAN_FILTERMODE_IDMASK;
can1filter.FilterNumber=0;
can1filter.FilterScale=CAN_FILTERSCALE_32BIT;
can1filter.FilterActivation = ENABLE;
if(HAL_CAN_ConfigFilter(&hcan, &can1filter)!=HAL_OK)
{
   Error_Handler();
}
过滤器的初始化
while (1)
  {
  /* USER CODE END WHILE */
    temperture=gettemperture();
//Ìî³ä·¢Ë͵Äbuff,»º³åÇøÊÇÊý×éa
  HAL_Delay(500);
    charge_buff(data_buff,temperture,0);
   MX_Can_SendMessenge(&hcan,data_buff, data_length,CAN_ID_STD,CAN_RTR_DATA ,0x12);
  
  if(HAL_CAN_Receive_IT(&hcan,CAN_FIFO0)!=HAL_OK)
  {
    Error_Handler();  
  }
   receive_data[0]=hcan.pRxMsg->Data[0];
   receive_data[1]=hcan.pRxMsg->Data[1];
  receive_data[2]=hcan.pRxMsg->Data[2];
  receive_data[3]=hcan.pRxMsg->Data[3];
  receive_data[4]=hcan.pRxMsg->Data[4];
  receive_data[5]=hcan.pRxMsg->Data[5];
  receive_data[6]=hcan.pRxMsg->Data[6];
    HAL_UART_Transmit_IT(&huart1,hcan.pRxMsg->Data,7);
   
  主函数
void MX_CAN_Init(void)
{
  hcan.Instance = CAN1;
hcan.pTxMsg=&cantxmessenge;
  hcan.pRxMsg=&canrxmessenge;
  hcan.Init.Prescaler = 6;
  hcan.Init.Mode =CAN_MODE_LOOPBACK;
  hcan.Init.SJW = CAN_SJW_1TQ;
  hcan.Init.BS1 = CAN_BS1_9TQ;
  hcan.Init.BS2 = CAN_BS2_8TQ;
  hcan.Init.TTCM = DISABLE;
  hcan.Init.ABOM = DISABLE;
  hcan.Init.AWUM = DISABLE;
  hcan.Init.NART = DISABLE;
  hcan.Init.RFLM = DISABLE;
  hcan.Init.TXFP = DISABLE;
  if (HAL_CAN_Init(&hcan) != HAL_OK)
  {
    Error_Handler();
  }
can初始化
oid MX_Can_SendMessenge (CAN_HandleTypeDef *hcan1,uint8_t *a,uint8_t datalegth,uint32_t ide,uint32_t rtr,uint16_t id)
{
//ÉèÖñ¨ÎĵÄIDºÍÊý¾ÝµÄÄÚÈÝ
hcan1->pTxMsg->DLC=datalegth;
hcan1->pTxMsg->IDE=ide;
hcan1->pTxMsg->RTR=rtr;
hcan1->pTxMsg->StdId=0x12;
hcan1->pTxMsg->Data[0]=a[0];
hcan1->pTxMsg->Data[1]=a[1];
hcan1->pTxMsg->Data[2]=a[2];
hcan1->pTxMsg->Data[3]=a[3];
hcan1->pTxMsg->Data[4]=a[4];
hcan1->pTxMsg->Data[5]=a[5];
hcan1->pTxMsg->Data[6]=a[6];
// hcan1->pTxMsg->Data[7]=a[7];
if(HAL_CAN_Transmit(hcan1,10)!=HAL_OK)
{
      Error_Handler();
}
HAL_GPIO_WritePin(GPIOE,GPIO_PIN_5,GPIO_PIN_SET);
  
  if(HAL_CAN_GetState(hcan1) != HAL_CAN_STATE_READY)
{
    Error_Handler();
}
发送函数
这个是我用原子开发板测试can的程序,如果我把那个can1filter.FilterMaskIdHigh=0x0000,串口就有我发的数据
00 0F 00 00 55 55 55 00 0F 00 00 55 55 55 00 0F 00 00 55 55 55 00 0F 00 00 55 55 55 00 0F 00 00 55 55 55 00 0F 00 00 55 55 55 00 0F 00 00 55 55 55 00 0F 00 00 55 55 55 00 0F 00 00 55 55 55
如果我把can1filter.FilterMaskIdHigh=0xf000,串口就没有数据,全是000000000000000000000000000000000000000000000000000000000000000
是我的过滤器设置有问题吗?应该设置成多杀呀!求大神指点

0条回答

一周热门 更多>