can

stm32f107rct6芯片,hal库can2能发不能收,stm32cube+1.8.0生成,求教!

最近调试stm32f107can2发现问题,cube使用1.8.0hal库生成代码,can1正常收发,can2能发不能收。请高手赐教!


static void MX_CAN1_Init(void)

{


  /* USER CODE BEGIN CAN1_Init 0 */


  /* USER CODE END CAN1_Init 0 */


  /* USER CODE BEGIN CAN1_Init 1 */

  // 100K  ->  10

// 10K   ->  100   450

// 5K    ->  200   900

// 2K5   ->  400  1800

  /* USER CODE END CAN1_Init 1 */

  hcan1.Instance = CAN1;

  hcan1.Init.Prescaler = 900;

  hcan1.Init.Mode = CAN_MODE_NORMAL;

  hcan1.Init.SyncJumpWidth = CAN_SJW_1TQ;

  hcan1.Init.TimeSeg1 = CAN_BS1_8TQ;

  hcan1.Init.TimeSeg2 = CAN_BS2_7TQ;

  hcan1.Init.TimeTriggeredMode = DISABLE;

  hcan1.Init.AutoBusOff = DISABLE;

  hcan1.Init.AutoWakeUp = DISABLE;

  hcan1.Init.AutoRetransmission = DISABLE;

  hcan1.Init.ReceiveFifoLocked = DISABLE;

  hcan1.Init.TransmitFifoPriority = DISABLE;

  if (HAL_CAN_Init(&hcan1) != HAL_OK)

  {

    Error_Handler();

  }

  /* USER CODE BEGIN CAN1_Init 2 */

  can1_filterConfig();

HAL_CAN_Start(&hcan1);

HAL_CAN_ActivateNotification(&hcan1, CAN_IT_RX_FIFO0_MSG_PENDING);

  /* USER CODE END CAN1_Init 2 */


}


/**

  * @brief CAN2 Initialization Function

  * @param None

  * @retval None

  */

static void MX_CAN2_Init(void)

{


  /* USER CODE BEGIN CAN2_Init 0 */


  /* USER CODE END CAN2_Init 0 */


  /* USER CODE BEGIN CAN2_Init 1 */


  /* USER CODE END CAN2_Init 1 */

  hcan2.Instance = CAN2;

  hcan2.Init.Prescaler = 900;

  hcan2.Init.Mode = CAN_MODE_NORMAL;

  hcan2.Init.SyncJumpWidth = CAN_SJW_1TQ;

  hcan2.Init.TimeSeg1 = CAN_BS1_8TQ;

  hcan2.Init.TimeSeg2 = CAN_BS2_7TQ;

  hcan2.Init.TimeTriggeredMode = DISABLE;

  hcan2.Init.AutoBusOff = DISABLE;

  hcan2.Init.AutoWakeUp = DISABLE;

  hcan2.Init.AutoRetransmission = DISABLE;

  hcan2.Init.ReceiveFifoLocked = DISABLE;

  hcan2.Init.TransmitFifoPriority = DISABLE;

  if (HAL_CAN_Init(&hcan2) != HAL_OK)

  {

    Error_Handler();

  }

  /* USER CODE BEGIN CAN2_Init 2 */

  can2_filterConfig();

  HAL_CAN_Start(&hcan2);

  HAL_CAN_ActivateNotification(&hcan2, CAN_IT_RX_FIFO1_MSG_PENDING);

  /* USER CODE END CAN2_Init 2 */


}



void can1_filterConfig(void)

{          CAN_FilterTypeDef CAN_FilterInitStructure;

    

   CAN_FilterInitStructure.FilterBank  = 0;                                                 //指定过滤器为0

   CAN_FilterInitStructure.FilterMode  = CAN_FILTERMODE_IDMASK;     //标识符屏蔽位模式

   CAN_FilterInitStructure.FilterScale = CAN_FILTERSCALE_32BIT;            //过滤器宽度

   CAN_FilterInitStructure.FilterFIFOAssignment = CAN_FILTER_FIFO0 ;  //过滤器fifo

   CAN_FilterInitStructure.FilterIdHigh =0x0000 ;                                     //过滤ID高16位

           CAN_FilterInitStructure.FilterIdLow  = 0x0000;                                     //过滤ID低16位 

   CAN_FilterInitStructure.FilterMaskIdHigh = 0x0000;                            //过滤屏蔽高16位

           CAN_FilterInitStructure.FilterMaskIdLow  = 0x0000;                            //过滤屏蔽低16位

   CAN_FilterInitStructure.FilterActivation = CAN_FILTER_ENABLE;         //使能过滤器

           CAN_FilterInitStructure.SlaveStartFilterBank = 14;

   HAL_CAN_ConfigFilter(&hcan1,&CAN_FilterInitStructure);

    

 

}


void can2_filterConfig(void)

{    CAN_FilterTypeDef CAN_FilterInitStructure;

     

   CAN_FilterInitStructure.FilterBank  = 14;                                                //指定过滤器为1

   CAN_FilterInitStructure.FilterMode  = CAN_FILTERMODE_IDMASK;     //标识符屏蔽位模式

   CAN_FilterInitStructure.FilterScale = CAN_FILTERSCALE_32BIT;            //过滤器宽度

   CAN_FilterInitStructure.FilterFIFOAssignment = CAN_FILTER_FIFO1;    //过滤器fifo

   CAN_FilterInitStructure.FilterIdHigh =0x0000 ;                                      //过滤ID高16位

           CAN_FilterInitStructure.FilterIdLow  = 0x0000;                                      //过滤ID低16位 

   CAN_FilterInitStructure.FilterMaskIdHigh = 0x0000;                             //过滤屏蔽高16位

           CAN_FilterInitStructure.FilterMaskIdLow  = 0x0000;                              //过滤屏蔽低16位

   CAN_FilterInitStructure.FilterActivation = CAN_FILTER_ENABLE;            //使能过滤器

           CAN_FilterInitStructure.SlaveStartFilterBank = 14;

   HAL_CAN_ConfigFilter(&hcan2,&CAN_FilterInitStructure) ; 

}


 if(hcan->Instance==CAN2)

  {

  /* USER CODE BEGIN CAN2_MspInit 0 */

    __HAL_RCC_CAN1_CLK_ENABLE();

  /* USER CODE END CAN2_MspInit 0 */

    /* Peripheral clock enable */

    __HAL_RCC_CAN2_CLK_ENABLE();

    HAL_RCC_CAN1_CLK_ENABLED++;

    if(HAL_RCC_CAN1_CLK_ENABLED==1){

      __HAL_RCC_CAN1_CLK_ENABLE();

    }


    __HAL_RCC_GPIOB_CLK_ENABLE();

    /**CAN2 GPIO Configuration

    PB5     ------> CAN2_RX

    PB6     ------> CAN2_TX

    */

    GPIO_InitStruct.Pin = GPIO_PIN_5;

    GPIO_InitStruct.Mode = GPIO_MODE_INPUT;

    GPIO_InitStruct.Pull = GPIO_NOPULL;

    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);


    GPIO_InitStruct.Pin = GPIO_PIN_6;

    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;

    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;

    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);


    __HAL_AFIO_REMAP_CAN2_ENABLE();


    /* CAN2 interrupt Init */

    HAL_NVIC_SetPriority(CAN2_RX0_IRQn, 0, 0);

    HAL_NVIC_EnableIRQ(CAN2_RX0_IRQn);

    HAL_NVIC_SetPriority(CAN2_RX1_IRQn, 0, 0);

    HAL_NVIC_EnableIRQ(CAN2_RX1_IRQn);

  /* USER CODE BEGIN CAN2_MspInit 1 */


  /* USER CODE END CAN2_MspInit 1 */

  }



已邀请:

要回复问题请先登录注册