最近用cube配置can通信can2一直进不了接收中断can1的功能完好,很烦不知道哪里有错,上网查了很多资料也没有解决,希望这里的大神能帮我解决这个问题,拜谢!下面附上源码。
static CanTxMsgTypeDef Tx1Message;
static CanRxMsgTypeDef Rx1Message;
static CanTxMsgTypeDef Tx2Message;
static CanRxMsgTypeDef Rx2Message;
u8 canbuf[8];
/* USER CODE END 0 */
CAN_HandleTypeDef hcan1;
CAN_HandleTypeDef hcan2;
/* CAN1 init function */
void MX_CAN1_Init(void)
{
hcan1.Instance = CAN1;
hcan1.pTxMsg=&Tx1Message;
hcan1.pRxMsg=&Rx1Message;
hcan1.Init.Prescaler = 3;
hcan1.Init.Mode = CAN_MODE_LOOPBACK;
hcan1.Init.SJW = CAN_SJW_1TQ;
hcan1.Init.BS1 = CAN_BS1_7TQ;
hcan1.Init.BS2 = CAN_BS2_6TQ;
hcan1.Init.TTCM = DISABLE;
hcan1.Init.ABOM = ENABLE;
hcan1.Init.AWUM = DISABLE;
hcan1.Init.NART = DISABLE;
hcan1.Init.RFLM = DISABLE;
hcan1.Init.TXFP = ENABLE;
HAL_CAN_Init(&hcan1);
}
/* CAN2 init function */
void MX_CAN2_Init(void)
{
hcan2.Instance = CAN2;
hcan2.pTxMsg=&Tx2Message;
hcan2.pRxMsg=&Rx2Message;
hcan2.Init.Prescaler = 3;
hcan2.Init.Mode = CAN_MODE_LOOPBACK;
hcan2.Init.SJW = CAN_SJW_1TQ;
hcan2.Init.BS1 = CAN_BS1_7TQ;
hcan2.Init.BS2 = CAN_BS2_6TQ;
hcan2.Init.TTCM = DISABLE;
hcan2.Init.ABOM = ENABLE;
hcan2.Init.AWUM = DISABLE;
hcan2.Init.NART = DISABLE;
hcan2.Init.RFLM = DISABLE;
hcan2.Init.TXFP = ENABLE;
HAL_CAN_Init(&hcan2);
}
static int CAN1_CLK_ENABLED=0;
void HAL_CAN_MspInit(CAN_HandleTypeDef* hcan)
{
GPIO_InitTypeDef GPIO_InitStruct;
if(hcan->Instance==CAN1)
{
/* USER CODE BEGIN CAN1_MspInit 0 */
__HAL_RCC_GPIOA_CLK_ENABLE();
/* USER CODE END CAN1_MspInit 0 */
/* Peripheral clock enable */
CAN1_CLK_ENABLED++;
if(CAN1_CLK_ENABLED==1){
__CAN1_CLK_ENABLE();
}
/**CAN1 GPIO Configuration
PA11 ------> CAN1_RX
PA12 ------> CAN1_TX
*/
GPIO_InitStruct.Pin = GPIO_PIN_11|GPIO_PIN_12;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF9_CAN1;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/* Peripheral interrupt init*/
HAL_NVIC_SetPriority(CAN1_RX0_IRQn, 1, 2);
HAL_NVIC_EnableIRQ(CAN1_RX0_IRQn);
/* USER CODE BEGIN CAN1_MspInit 1 */
CAN_FilterConfTypeDef CAN1_FilerConf;
CAN1_FilerConf.FilterIdHigh=0X0000;
CAN1_FilerConf.FilterIdLow=0X0000;
CAN1_FilerConf.FilterMaskIdHigh=0X0000;
CAN1_FilerConf.FilterMaskIdLow=0X0000;
CAN1_FilerConf.FilterFIFOAssignment=CAN_FILTER_FIFO0;
CAN1_FilerConf.FilterNumber=0;
CAN1_FilerConf.FilterMode=CAN_FILTERMODE_IDMASK;
CAN1_FilerConf.FilterScale=CAN_FILTERSCALE_32BIT;
CAN1_FilerConf.FilterActivation=ENABLE;
CAN1_FilerConf.BankNumber=14;
HAL_CAN_ConfigFilter(&hcan1,&CAN1_FilerConf);
__HAL_CAN_ENABLE_IT(&hcan1,CAN_IT_FMP0);
/* USER CODE END CAN1_MspInit 1 */
}
else if(hcan->Instance==CAN2)
{
/* USER CODE BEGIN CAN2_MspInit 0 */
/* USER CODE END CAN2_MspInit 0 */
/* Peripheral clock enable */
__CAN1_CLK_ENABLE();
__CAN2_CLK_ENABLE();
CAN1_CLK_ENABLED++;
if(CAN1_CLK_ENABLED==1){
__CAN1_CLK_ENABLE();
}
/**CAN2 GPIO Configuration
PB12 ------> CAN2_RX
PB13 ------> CAN2_TX
*/
GPIO_InitStruct.Pin = GPIO_PIN_12|GPIO_PIN_13;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF9_CAN2;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
/* Peripheral interrupt init*/
HAL_NVIC_SetPriority(CAN2_RX0_IRQn, 1, 1);
HAL_NVIC_EnableIRQ(CAN2_RX0_IRQn);
/* USER CODE BEGIN CAN2_MspInit 1 */
CAN_FilterConfTypeDef CAN2_FilerConf;
CAN2_FilerConf.FilterIdHigh=0X0000;
CAN2_FilerConf.FilterIdLow=0X0000;
CAN2_FilerConf.FilterMaskIdHigh=0X0000;
CAN2_FilerConf.FilterMaskIdLow=0X0000;
CAN2_FilerConf.FilterFIFOAssignment=CAN_FILTER_FIFO1;
CAN2_FilerConf.FilterNumber=14;
CAN2_FilerConf.FilterMode=CAN_FILTERMODE_IDMASK;
CAN2_FilerConf.FilterScale=CAN_FILTERSCALE_32BIT;
CAN2_FilerConf.FilterActivation=ENABLE;
CAN2_FilerConf.BankNumber=14;
HAL_CAN_ConfigFilter(&hcan2,&CAN2_FilerConf);
__HAL_CAN_ENABLE_IT(&hcan2,CAN_IT_FMP1);
/* USER CODE END CAN2_MspInit 1 */
}
}
void HAL_CAN_MspDeInit(CAN_HandleTypeDef* hcan)
{
if(hcan->Instance==CAN1)
{
/* USER CODE BEGIN CAN1_MspDeInit 0 */
/* USER CODE END CAN1_MspDeInit 0 */
/* Peripheral clock disable */
CAN1_CLK_ENABLED--;
if(CAN1_CLK_ENABLED==0){
__CAN1_CLK_DISABLE();
}
/**CAN1 GPIO Configuration
PA11 ------> CAN1_RX
PA12 ------> CAN1_TX
*/
HAL_GPIO_DeInit(GPIOA, GPIO_PIN_11|GPIO_PIN_12);
/* Peripheral interrupt Deinit*/
HAL_NVIC_DisableIRQ(CAN1_RX0_IRQn);
/* USER CODE BEGIN CAN1_MspDeInit 1 */
/* USER CODE END CAN1_MspDeInit 1 */
}
else if(hcan->Instance==CAN2)
{
/* USER CODE BEGIN CAN2_MspDeInit 0 */
/* USER CODE END CAN2_MspDeInit 0 */
/* Peripheral clock disable */
__CAN2_CLK_DISABLE();
CAN1_CLK_ENABLED--;
if(CAN1_CLK_ENABLED==0){
__CAN1_CLK_DISABLE();
}
/**CAN2 GPIO Configuration
PB12 ------> CAN2_RX
PB13 ------> CAN2_TX
*/
HAL_GPIO_DeInit(GPIOB, GPIO_PIN_12|GPIO_PIN_13);
/* Peripheral interrupt Deinit*/
HAL_NVIC_DisableIRQ(CAN2_RX0_IRQn);
/* USER CODE BEGIN CAN2_MspDeInit 1 */
/* USER CODE END CAN2_MspDeInit 1 */
}
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
static CanTxMsgTypeDef Tx1Message; //·¢ËÍÏûÏ¢
static CanRxMsgTypeDef Rx1Message; //½ÓÊÕÏûÏ¢
static CanTxMsgTypeDef Tx2Message; //·¢ËÍÏûÏ¢
static CanRxMsgTypeDef Rx2Message; //½ÓÊÕÏûÏ¢
u8 can_2_buf[8];
u8 databuf[8]={0};
u8 databuf_PTZ[8]={0,0,0,0,0,0,0,0};
// hcan1.pTxMsg=&Tx1Message; //·¢ËÍÏûÏ¢
// hcan1.pRxMsg=&Rx1Message; //½ÓÊÕÏûÏ¢
// hcan2.pTxMsg=&Tx2Message; //·¢ËÍÏûÏ¢
// hcan2.pRxMsg=&Rx2Message; //½ÓÊÕÏûÏ¢
/* USER CODE END 0 */
CAN_HandleTypeDef hcan1;
CAN_HandleTypeDef hcan2;
/* CAN1 init function */
void MX_CAN1_Init(void)
{
hcan1.Instance = CAN1;
hcan1.pTxMsg=&Tx1Message; //·¢ËÍÏûÏ¢
hcan1.pRxMsg=&Rx1Message; //½ÓÊÕÏûÏ¢
hcan1.Init.Prescaler = 3;
hcan1.Init.Mode = CAN_MODE_NORMAL;
hcan1.Init.SJW = CAN_SJW_1TQ;
hcan1.Init.BS1 = CAN_BS1_7TQ;
hcan1.Init.BS2 = CAN_BS2_6TQ;
hcan1.Init.TTCM = DISABLE;
hcan1.Init.ABOM = DISABLE;
hcan1.Init.AWUM = DISABLE;
hcan1.Init.NART = DISABLE;
hcan1.Init.RFLM = DISABLE;
hcan1.Init.TXFP = ENABLE;
HAL_CAN_Init(&hcan1);
}
/* CAN2 init function */
void MX_CAN2_Init(void)
{
hcan2.Instance = CAN2;
hcan2.pTxMsg=&Tx2Message; //·¢ËÍÏûÏ¢
hcan2.pRxMsg=&Rx2Message; //½ÓÊÕÏûÏ¢
hcan2.Init.Prescaler = 3;
hcan2.Init.Mode = CAN_MODE_NORMAL;
hcan2.Init.SJW = CAN_SJW_1TQ;
hcan2.Init.BS1 = CAN_BS1_7TQ;
hcan2.Init.BS2 = CAN_BS2_6TQ;
hcan2.Init.TTCM = DISABLE;
hcan2.Init.ABOM = DISABLE;
hcan2.Init.AWUM = DISABLE;
hcan2.Init.NART = DISABLE;
hcan2.Init.RFLM = DISABLE;
hcan2.Init.TXFP = ENABLE;
HAL_CAN_Init(&hcan2);
}
static int CAN1_CLK_ENABLED=0;
void HAL_CAN_MspInit(CAN_HandleTypeDef* hcan)
{
GPIO_InitTypeDef GPIO_InitStruct;
if(hcan->Instance==CAN1)
{
/* USER CODE BEGIN CAN1_MspInit 0 */
/* USER CODE END CAN1_MspInit 0 */
/* Peripheral clock enable */
CAN1_CLK_ENABLED++;
if(CAN1_CLK_ENABLED==1){
__CAN1_CLK_ENABLE();
}
/**CAN1 GPIO Configuration
PA11 ------> CAN1_RX
PA12 ------> CAN1_TX
*/
GPIO_InitStruct.Pin = GPIO_PIN_11|GPIO_PIN_12;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF9_CAN1;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/* USER CODE BEGIN CAN1_MspInit 1 */
CAN_FilterConfTypeDef CAN1_FilerConf;
CAN1_FilerConf.FilterIdHigh=0X0000; //32λID
CAN1_FilerConf.FilterIdLow=0X0000;
CAN1_FilerConf.FilterMaskIdHigh=0X0000; //32λMASK
CAN1_FilerConf.FilterMaskIdLow=0X0000;
CAN1_FilerConf.FilterFIFOAssignment=CAN_FILTER_FIFO0;//¹ýÂËÆ÷0¹ØÁªµ½FIFO0
CAN1_FilerConf.FilterNumber=0; //¹ýÂËÆ÷0
CAN1_FilerConf.FilterMode=CAN_FILTERMODE_IDMASK;
CAN1_FilerConf.FilterScale=CAN_FILTERSCALE_32BIT;
CAN1_FilerConf.FilterActivation=ENABLE; //¼¤»îÂ˲¨Æ÷0
CAN1_FilerConf.BankNumber=14;
HAL_CAN_ConfigFilter(&hcan1,&CAN1_FilerConf);
__HAL_CAN_ENABLE_IT(&hcan1,CAN_IT_FMP0);
/* USER CODE END CAN1_MspInit 1 */
}
else if(hcan->Instance==CAN2)
{
/* USER CODE BEGIN CAN2_MspInit 0 */
/* USER CODE END CAN2_MspInit 0 */
/* Peripheral clock enable */
__CAN2_CLK_ENABLE();
CAN1_CLK_ENABLED++;
if(CAN1_CLK_ENABLED==1){
__CAN1_CLK_ENABLE();
}
/**CAN2 GPIO Configuration
PB12 ------> CAN2_RX
PB13 ------> CAN2_TX
*/
GPIO_InitStruct.Pin = GPIO_PIN_12|GPIO_PIN_13;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF9_CAN2;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
/* Peripheral interrupt init*/
HAL_NVIC_SetPriority(CAN2_RX0_IRQn, 1, 1);
HAL_NVIC_EnableIRQ(CAN2_RX0_IRQn);
/* USER CODE BEGIN CAN2_MspInit 1 */
CAN_FilterConfTypeDef CAN2_FilerConf;
CAN2_FilerConf.FilterIdHigh=0X0000; //32λID
CAN2_FilerConf.FilterIdLow=0X0000;
CAN2_FilerConf.FilterMaskIdHigh=0X0000; //32λMASK
CAN2_FilerConf.FilterMaskIdLow=0X0000;
CAN2_FilerConf.FilterFIFOAssignment=CAN_FILTER_FIFO0;//¹ýÂËÆ÷0¹ØÁªµ½FIFO0
CAN2_FilerConf.FilterNumber=14; //¹ýÂËÆ÷14
CAN2_FilerConf.FilterMode=CAN_FILTERMODE_IDMASK;
CAN2_FilerConf.FilterScale=CAN_FILTERSCALE_32BIT;
CAN2_FilerConf.FilterActivation=ENABLE; //¼¤»îÂ˲¨Æ÷0
CAN2_FilerConf.BankNumber=14;
HAL_CAN_ConfigFilter(&hcan1,&CAN2_FilerConf);
__HAL_CAN_ENABLE_IT(&hcan2,CAN_IT_FMP0);
/* USER CODE END CAN2_MspInit 1 */
}
}
void HAL_CAN_MspDeInit(CAN_HandleTypeDef* hcan)
{
if(hcan->Instance==CAN1)
{
/* USER CODE BEGIN CAN1_MspDeInit 0 */
/* USER CODE END CAN1_MspDeInit 0 */
/* Peripheral clock disable */
CAN1_CLK_ENABLED--;
if(CAN1_CLK_ENABLED==0){
__CAN1_CLK_DISABLE();
}
/**CAN1 GPIO Configuration
PA11 ------> CAN1_RX
PA12 ------> CAN1_TX
*/
HAL_GPIO_DeInit(GPIOA, GPIO_PIN_11|GPIO_PIN_12);
/* USER CODE BEGIN CAN1_MspDeInit 1 */
/* USER CODE END CAN1_MspDeInit 1 */
}
else if(hcan->Instance==CAN2)
{
/* USER CODE BEGIN CAN2_MspDeInit 0 */
/* USER CODE END CAN2_MspDeInit 0 */
/* Peripheral clock disable */
__CAN2_CLK_DISABLE();
CAN1_CLK_ENABLED--;
if(CAN1_CLK_ENABLED==0){
__CAN1_CLK_DISABLE();
}
/**CAN2 GPIO Configuration
PB12 ------> CAN2_RX
PB13 ------> CAN2_TX
*/
HAL_GPIO_DeInit(GPIOB, GPIO_PIN_12|GPIO_PIN_13);
/* Peripheral interrupt Deinit*/
HAL_NVIC_DisableIRQ(CAN2_RX0_IRQn);
/* USER CODE BEGIN CAN2_MspDeInit 1 */
/* USER CODE END CAN2_MspDeInit 1 */
}
}
这样配就能进啦
一周热门 更多>