[求助] stm32 CAN 不能进入发送中断

jacksoon   2011-10-30 14:39 楼主
器件型号:STM32F103VE
问题:单步调试发现没有进入中断程序,请高手指教!
代码:    void NVIC_Configuration(void)
    {
        NVIC_InitTypeDef NVIC_InitStructure;//定义NVIC_InitTypeDef类型变量NVIC_InitStructure

        #ifdef  VECT_TAB_RAM 
              /* Set the Vector Table base location at 0x20000000 --设置量表的基地位置在0x20000000(在RAM上)*/
              NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
        #else  /* VECT_TAB_FLASH  */
              /* Set the Vector Table base location at 0x08000000 --设置量表的基地位置在0x08000000(在FLASH上)*/
              NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);  
        #endif
          
        /* Enable and configure IRQ channel */
          NVIC_InitStructure.NVIC_IRQChannel = USB_LP_CAN1_RX0_IRQn; //中断通道
          NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //主优先级0
          NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;        //次优先级0
          NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;              //中断使能
          NVIC_Init(&NVIC_InitStructure);
    }

TestStatus CAN_Interrupt(void)
{
  CAN_InitTypeDef        CAN_InitStructure;
  CAN_FilterInitTypeDef  CAN_FilterInitStructure;
  CanTxMsg TxMessage;
  u32 i = 0;
  //u8 TransmitMailbox;

  /* CAN register init */
  CAN_DeInit(CAN1);
  CAN_StructInit(&CAN_InitStructure);

  /* CAN cell init */
  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;
  CAN_InitStructure.CAN_TXFP=DISABLE;
  CAN_InitStructure.CAN_Mode=CAN_Mode_LoopBack;
  CAN_InitStructure.CAN_SJW=CAN_SJW_1tq;
  CAN_InitStructure.CAN_BS1=CAN_BS1_7tq;
  CAN_InitStructure.CAN_BS2=CAN_BS2_8tq;
  CAN_InitStructure.CAN_Prescaler=9;
  CAN_Init(CAN1,&CAN_InitStructure);

  /* CAN filter init */
//  CAN_FilterInitStructure.CAN_FilterNumber=1;
//  CAN_FilterInitStructure.CAN_FilterMode=CAN_FilterMode_IdMask;
//  CAN_FilterInitStructure.CAN_FilterScale=CAN_FilterScale_32bit;
//  i=(0x11<<21)|(0x34<<3);
//  CAN_FilterInitStructure.CAN_FilterIdHigh=(i>>16);
//  CAN_FilterInitStructure.CAN_FilterIdLow=(i&0xffff);
//  CAN_FilterInitStructure.CAN_FilterMaskIdHigh=0x0;
//  CAN_FilterInitStructure.CAN_FilterMaskIdLow=0x0;
//  CAN_FilterInitStructure.CAN_FilterFIFOAssignment=CAN_FIFO0;
//  CAN_FilterInitStructure.CAN_FilterActivation=ENABLE;
//  CAN_FilterInit(&CAN_FilterInitStructure);

  CAN_FilterInitStructure.CAN_FilterNumber=0;
  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=0;
  CAN_FilterInitStructure.CAN_FilterActivation=ENABLE;
  CAN_FilterInit(&CAN_FilterInitStructure);

  /* CAN FIFO0 message pending interrupt enable */
  CAN_ITConfig(CAN1,CAN_IT_FMP0, ENABLE);

  /* initialize the value that will be returned */
  ret = 0xFF;

  /* transmit 1 message */
  TxMessage.StdId=0x12;
  TxMessage.ExtId=0x34;
  TxMessage.IDE=CAN_ID_EXT;
  TxMessage.RTR=CAN_RTR_DATA;
  TxMessage.DLC=2;
  TxMessage.Data[0]=0xDE;
  TxMessage.Data[1]=0xCA;
  CAN_Transmit(CAN1,&TxMessage);

  /* receive message with interrupt handling */
  i=0;
  while((ret == 0xFF) && (i < 0xFF))
  {
    i++;
  }
 
  if (i == 0xFF)
  {
    ret=0; 
  }

  /* disable interrupt handling */
  CAN_ITConfig(CAN1,CAN_IT_FMP0, DISABLE);

  return (TestStatus)ret;
}
void USB_LP_CAN1_RX0_IRQHandler(void)
{
  CanRxMsg RxMessage;

  RxMessage.StdId=0x00;
  RxMessage.ExtId=0x00;
  RxMessage.IDE=0;
  RxMessage.DLC=0;
  RxMessage.FMI=0;
  RxMessage.Data[0]=0x00;
  RxMessage.Data[1]=0x00;

  CAN_Receive(CAN1,CAN_FIFO0, &RxMessage);

  if((RxMessage.StdId==0x12) && (RxMessage.ExtId==0x34) && (RxMessage.IDE==CAN_ID_EXT)
     && (RxMessage.DLC==2) && ((RxMessage.Data[1]|RxMessage.Data[0]<<8)==0xDECA))
  {
    ret = 1;
  }
  else
  {
    ret = 0;
  }
}

回复评论

暂无评论,赶紧抢沙发吧
电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 京公网安备 11010802033920号
    写回复