can

STM32F105RB配置CAN2收不到数,也不进中断

这个是参考网上写的程序自己配下来的结果

extern CAN_FilterConfTypeDef  sFilterConfig;

void can_init(void)

{

  sFilterConfig.FilterNumber = 0;

  sFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK;

  sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT;

  sFilterConfig.FilterIdHigh = 0x0000;

  sFilterConfig.FilterIdLow = 0x0000;

  sFilterConfig.FilterMaskIdHigh = 0x0000;

  sFilterConfig.FilterMaskIdLow = 0x0000;

  sFilterConfig.FilterFIFOAssignment = CAN_FILTER_FIFO0;

  sFilterConfig.BankNumber=14;

  sFilterConfig.FilterActivation = ENABLE;

  HAL_CAN_ConfigFilter(&hcan1, &sFilterConfig);

  sFilterConfig.FilterNumber = 14;

  HAL_CAN_ConfigFilter(&hcan2, &sFilterConfig);

}


这个是cube生成的can.c波特率配置是500k

void MX_CAN1_Init(void)

{


  hcan1.Instance = CAN1;

  hcan1.Init.Prescaler = 9;

  hcan1.Init.Mode = CAN_MODE_NORMAL;

  hcan1.Init.SJW = CAN_SJW_1TQ;

  hcan1.Init.BS1 = CAN_BS1_6TQ;

  hcan1.Init.BS2 = CAN_BS2_1TQ;

  hcan1.Init.TTCM = DISABLE;

  hcan1.Init.ABOM = ENABLE;

  hcan1.Init.AWUM = DISABLE;

  hcan1.Init.NART = DISABLE;

  hcan1.Init.RFLM = DISABLE;

  hcan1.Init.TXFP = DISABLE;

  if (HAL_CAN_Init(&hcan1) != HAL_OK)

  {

    _Error_Handler(__FILE__, __LINE__);

  }


}

/* CAN2 init function */

void MX_CAN2_Init(void)

{


  hcan2.Instance = CAN2;

  hcan2.Init.Prescaler = 9;

  hcan2.Init.Mode = CAN_MODE_NORMAL;

  hcan2.Init.SJW = CAN_SJW_1TQ;

  hcan2.Init.BS1 = CAN_BS1_6TQ;

  hcan2.Init.BS2 = CAN_BS2_1TQ;

  hcan2.Init.TTCM = DISABLE;

  hcan2.Init.ABOM = ENABLE;

  hcan2.Init.AWUM = DISABLE;

  hcan2.Init.NART = DISABLE;

  hcan2.Init.RFLM = DISABLE;

  hcan2.Init.TXFP = DISABLE;

  if (HAL_CAN_Init(&hcan2) != HAL_OK)

  {

    _Error_Handler(__FILE__, __LINE__);

  }


}

static uint32_t HAL_RCC_CAN1_CLK_ENABLED=0;


void HAL_CAN_MspInit(CAN_HandleTypeDef* canHandle)

{


  GPIO_InitTypeDef GPIO_InitStruct;

  if(canHandle->Instance==CAN1)

  {

  /* USER CODE BEGIN CAN1_MspInit 0 */


  /* USER CODE END CAN1_MspInit 0 */

    /* CAN1 clock enable */

    HAL_RCC_CAN1_CLK_ENABLED++;

    if(HAL_RCC_CAN1_CLK_ENABLED==1){

      __HAL_RCC_CAN1_CLK_ENABLE();

    }

  

    /**CAN1 GPIO Configuration    

    PA11     ------> CAN1_RX

    PA12     ------> CAN1_TX 

    */

    GPIO_InitStruct.Pin = GPIO_PIN_11;

    GPIO_InitStruct.Mode = GPIO_MODE_INPUT;

    GPIO_InitStruct.Pull = GPIO_NOPULL;

    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);


    GPIO_InitStruct.Pin = GPIO_PIN_12;

    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;

    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;

    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);


    /* CAN1 interrupt Init */

    HAL_NVIC_SetPriority(CAN1_RX0_IRQn, 0, 0);

    HAL_NVIC_EnableIRQ(CAN1_RX0_IRQn);

  /* USER CODE BEGIN CAN1_MspInit 1 */


  /* USER CODE END CAN1_MspInit 1 */

  }

  else if(canHandle->Instance==CAN2)

  {

  /* USER CODE BEGIN CAN2_MspInit 0 */


  /* USER CODE END CAN2_MspInit 0 */

    /* CAN2 clock enable */

    __HAL_RCC_CAN2_CLK_ENABLE();

    HAL_RCC_CAN1_CLK_ENABLED++;

    if(HAL_RCC_CAN1_CLK_ENABLED==1){

      __HAL_RCC_CAN1_CLK_ENABLE();

    }

  

    /**CAN2 GPIO Configuration    

    PB12     ------> CAN2_RX

    PB13     ------> CAN2_TX 

    */

    GPIO_InitStruct.Pin = GPIO_PIN_12;

    GPIO_InitStruct.Mode = GPIO_MODE_INPUT;

    GPIO_InitStruct.Pull = GPIO_NOPULL;

    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);


    GPIO_InitStruct.Pin = GPIO_PIN_13;

    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;

    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;

    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);


    /* CAN2 interrupt Init */

    HAL_NVIC_SetPriority(CAN2_RX0_IRQn, 0, 0);

    HAL_NVIC_EnableIRQ(CAN2_RX0_IRQn);

  /* USER CODE BEGIN CAN2_MspInit 1 */


  /* USER CODE END CAN2_MspInit 1 */

  }

}

在main.c的主函数里写

    CAN_FilterConfTypeDef sFilterConfig;

    CanTxMsgTypeDef TxMessage1;

    CanTxMsgTypeDef TxMessage2;

    CanRxMsgTypeDef RxMessage1;

    CanRxMsgTypeDef RxMessage2;


    hcan1.pTxMsg=&TxMessage1;

    hcan1.pRxMsg=&RxMessage1;

    hcan2.pTxMsg=&TxMessage2;

    hcan2.pRxMsg=&RxMessage2;


    HAL_CAN_Receive_IT(&hcan1,CAN_FIFO0);

    HAL_CAN_Receive_IT(&hcan2,CAN_FIFO0);

中断函数是

void HAL_CAN_RxCpltCallback(CAN_HandleTypeDef* hcan);

最后只有CAN1能进中断,CAN2不进中断,在线调试也是,RxMessage1里能收到数,RxMessage2里收不到数

确定CAN1.CAN2都通了,都能发出来数,只有CAN2收不到数,也进不了中断

求教各路大神,这是为什么?哪里配的不对吗


我用的CubeMX版本是4.27,stm32F1的包是1.61

已邀请:

要回复问题请先登录注册