STM32的CAN2口无法进入接收中断

2019-08-20 18:56发布

这个是我的程序,采用CAN2口能发送数据,can分析接收的数据,如果用CAN分析仪向控制板发送数据,控制板的接收中断无法进入,这是怎么回事,和别人程序对照没有发现问题呢,请大神指导,
can的配置
如下void Init_CAN2(void)
{
    GPIO_InitTypeDef  GPIO_InitStructure;
    CAN_InitTypeDef        CAN_InitStructure;
    CAN_FilterInitTypeDef  CAN_FilterInitStructure;    
    NVIC_InitTypeDef  NVIC_InitStructure;


    //使能GPIOD的时钟
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOB, ENABLE);

    /* Configure CAN2 RX pin */
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
    GPIO_Init(GPIOB, &GPIO_InitStructure);
    
    
    /* Configure CAN2 TX pin */
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOB, &GPIO_InitStructure);
    
    GPIO_PinRemapConfig(GPIO_Remap_CAN2, ENABLE);

    //使能CAN时钟
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1 | RCC_APB1Periph_CAN2, ENABLE);  
    
    


    //CAN重置所有参数
    CAN_DeInit(CAN2);

    //CAN结构体重置
    CAN_StructInit(&CAN_InitStructure);
    //CAN 初始化
    CAN_InitStructure.CAN_TTCM = DISABLE;
    CAN_InitStructure.CAN_ABOM = DISABLE;
    CAN_InitStructure.CAN_AWUM = DISABLE;
    CAN_InitStructure.CAN_NART = DISABLE;
    CAN_InitStructure.CAN_RFLM = DISABLE;//不使用接收FIFO锁定功能,这样报文溢出时,新报文将覆盖最后接收到的旧报文
    CAN_InitStructure.CAN_TXFP = DISABLE;
    CAN_InitStructure.CAN_Mode = CAN_Mode_Normal;
    
    //CAN波特率 =1MBps CAN (在时钟30Mhz)
    CAN_InitStructure.CAN_SJW = CAN_SJW_1tq;//重新同步跳跃宽度1个时间单位
    CAN_InitStructure.CAN_BS1 = CAN_BS1_13tq;//时间段1为8个时间单位
    CAN_InitStructure.CAN_BS2 = CAN_BS2_2tq;//时间段2为7个时间单位
    CAN_InitStructure.CAN_Prescaler =9;//(pclk1/((1+13+2)*9)) = 36Mhz/16/9 = 250Kbits设定了一个时间单位的长度9

    CAN_Init(CAN2, &CAN_InitStructure);

    //CAN2的过滤器初始化
    CAN_FilterInitStructure.CAN_FilterNumber =14;
    CAN_FilterInitStructure.CAN_FilterMode = CAN_FilterMode_IdMask;
    CAN_FilterInitStructure.CAN_FilterScale = CAN_FilterScale_32bit;
    CAN_FilterInitStructure.CAN_FilterIdHigh =0x0000;
    CAN_FilterInitStructure.CAN_FilterIdLow = 0x0000;
    CAN_FilterInitStructure.CAN_FilterMaskIdHigh = 0x0000;
    CAN_FilterInitStructure.CAN_FilterMaskIdLow = 0x0000;
    CAN_FilterInitStructure.CAN_FilterFIFOAssignment = CAN_FIFO0;
    CAN_FilterInitStructure.CAN_FilterActivation = ENABLE;
    CAN2_FilterInit(&CAN_FilterInitStructure);

    //传输
    TxMessage.StdId = 0x321;
    TxMessage.ExtId = 0x01;
    TxMessage.RTR = CAN_RTR_DATA;
    TxMessage.IDE = CAN_ID_STD;
    TxMessage.DLC = 1;

  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); 
  NVIC_InitStructure.NVIC_IRQChannel = CAN2_RX0_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);

  NVIC_InitStructure.NVIC_IRQChannel = CAN2_TX_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 4;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);

  NVIC_InitStructure.NVIC_IRQChannel = CAN2_SCE_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 5;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);  

  
  /*在操作系统下注册中断函数*/
//  BSP_IntVectSet(BSP_INT_ID_CAN2_TX, BSP_IntHandlerCAN2_TX);     /*Assign ISR handler.Interrupt for which vector will be set*/
//  BSP_IntEn(BSP_INT_ID_CAN2_TX);                              /*Interrupt to enable.*/ 
//  BSP_IntVectSet(BSP_INT_ID_CAN2_RX0, BSP_IntHandlerCAN2_RX0);     /*Assign ISR handler.Interrupt for which vector will be set*/
//  BSP_IntEn(BSP_INT_ID_CAN2_RX0);                              /*Interrupt to enable.*/   
//  BSP_IntVectSet(BSP_INT_ID_CAN2_SCE, BSP_IntHandlerCAN2_SCE);     /*Assign ISR handler.Interrupt for which vector will be set*/
//  BSP_IntEn(BSP_INT_ID_CAN2_SCE);                              /*Interrupt to enable.*/   

    CAN_ClearITPendingBit(CAN2, CAN_IT_FMP0);//清接收中断标志
    CAN_ClearITPendingBit(CAN2, CAN_IT_TME);//清发送中断标志    

//      CAN_ITConfig(CAN2, CAN_IT_FMP0 |/*!< FIFO 0 message pending Interrupt*/
// CAN_IT_FMP1 |/*!< FIFO 1 message pending Interrupt*/
// CAN_IT_FF0 |/*!< FIFO 0 full Interrupt*/
// CAN_IT_FF1 |/*!< FIFO 1 full Interrupt*/
// CAN_IT_TME |/*!< Transmit mailbox empty Interrupt*/
// CAN_IT_BOF |/*!< Bus-off Interrupt*/
// CAN_IT_EPV |/*!< Error passive Interrupt*/
// CAN_IT_EWG |/*!< Error warning Interrupt*/
// CAN_IT_ERR /*!< Error Interrupt*/
// , ENABLE);

    CAN_ITConfig(CAN2, CAN_IT_FMP0, ENABLE);
     // CAN_ITConfig(CAN2,CAN2_RX0_IRQn,ENABLE);
      
      
}

友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
该问题目前已经被作者或者管理员关闭, 无法添加新回复
7条回答
lxp89
1楼-- · 2019-08-20 19:42
问题解决,原来是自己的库函数有问题改成3.5版本的
lxp89
2楼-- · 2019-08-21 00:35
 精彩回答 2  元偷偷看……
漂泊的雨林
3楼-- · 2019-08-21 03:43
怎么发现的库的问题 ,我的也是进入不了接受中断
tjb83981242
4楼-- · 2019-08-21 06:43
我用的是STM32F427ZG做的,只要把过滤器修改为14就可以进入中断了,但是很奇怪 官网上给的是1~13 为什么会到14呢 很奇怪!
101yang
5楼-- · 2019-08-21 12:18
原子哥的战舰是3.5版本的库吗?
outman1302
6楼-- · 2019-08-21 14:18
请问怎么改版本啊

一周热门 更多>