打印

stm32 CAN 不能进入发送中断

[复制链接]
7123|3
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
albertjacksoon|  楼主 | 2011-10-30 14:22 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
器件型号: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;
  }
}
沙发
xhtxzxw| | 2011-10-30 17:18 | 只看该作者
CAN没给工作时钟??

使用特权

评论回复
板凳
airwill| | 2011-10-31 08:17 | 只看该作者
真没有看到 RCC 模块的初始化部分. 没有看到CAN 开工作时钟的部分.
先用示波器看看 tx 引脚有没有波形出来.

使用特权

评论回复
地板
albertjacksoon|  楼主 | 2011-10-31 15:01 | 只看该作者
RCC如下:   
void RCC_Configuration(CSS_State newState)
    {   
        ErrorStatus HSEStartUpStatus;

          /* RCC system reset(for debug purpose) --RCC系统复位(用于调试目的)*/
          RCC_DeInit();

          /* Enable HSE --启用HSE(外部高速时钟)*/
          RCC_HSEConfig(RCC_HSE_ON);

          /* Wait till HSE is ready --等待HSE就绪*/
          HSEStartUpStatus = RCC_WaitForHSEStartUp();

          if(HSEStartUpStatus == SUCCESS)
          {
            /* Enable Prefetch Buffer --启用预取缓冲器*/
            FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);

            /* Flash 2 wait state --Flash2等待状态*/
            FLASH_SetLatency(FLASH_Latency_2);

            /* HCLK = SYSCLK */
            RCC_HCLKConfig(RCC_SYSCLK_Div1);
  
            /* PCLK2 = HCLK */
            RCC_PCLK2Config(RCC_HCLK_Div1);

            /* PCLK1 = HCLK/2 */
            RCC_PCLK1Config(RCC_HCLK_Div2);

            /* PLLCLK = 8MHz * 9 = 72 MHz */
            RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);

            /* Enable PLL --启用PLL*/
            RCC_PLLCmd(ENABLE);

            /* Wait till PLL is ready --等待PLL就绪*/
            while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
            {
            }

            /* Select PLL as system clock source --选定PLL作为系统时钟源*/
            RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);

            /* Wait till PLL is used as system clock source --等待,直到PLL作为系统时钟源*/
            while(RCC_GetSYSCLKSource() != 0x08)
            {
            }

           RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1,ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);    /*AFIO时钟*/        
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);    /*GPIOA时钟*/        
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);    /*GPIOB时钟*/        
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);    /*GPIOC时钟*/
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD,ENABLE);
          }
          else
          {
            /* Add here some code to deal with this error 在这里添加一些代码来处理这个错误*/        
          }
    }

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

0

主题

2

帖子

1

粉丝