27,440
社区成员
发帖
与我相关
我的任务
分享
CAN_HandleTypeDef hcan;
CanTxMsgTypeDef TxMessage;
CanRxMsgTypeDef RxMessage;
/* CAN init function */
void MX_CAN_Init(void)
{
hcan.Instance = CAN;
hcan.pTxMsg = &TxMessage;
hcan.pRxMsg = &RxMessage;
//250K
hcan.Init.Prescaler = 12;
hcan.Init.Mode = CAN_MODE_NORMAL;
hcan.Init.SJW = CAN_SJW_1TQ;
hcan.Init.BS1 = CAN_BS1_7TQ;
hcan.Init.BS2 = CAN_BS2_8TQ;
hcan.Init.TTCM = DISABLE;
hcan.Init.ABOM = ENABLE;
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_FilterConfTypeDef sFilterConfig;
// /*##-2- Configure the CAN Filter ###########################################*/
// 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.FilterActivation = ENABLE;
// sFilterConfig.BankNumber = 14;
// if (HAL_CAN_ConfigFilter(&hcan, &sFilterConfig) != HAL_OK)
// {
// /* Filter configuration Error */
// Error_Handler();
// }
// HAL_CAN_Receive_IT(&hcan, CAN_FIFO0); //使能CAN接收中断
/*##-2- Configure the CAN Filter ###########################################*/
sFilterConfig.FilterNumber = 1;
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_FIFO1;
sFilterConfig.FilterActivation = ENABLE;
sFilterConfig.BankNumber = 14;
if (HAL_CAN_ConfigFilter(&hcan, &sFilterConfig) != HAL_OK)
{
/* Filter configuration Error */
Error_Handler();
}
HAL_CAN_Receive_IT(&hcan, CAN_FIFO1); //使能CAN接收中断
}