打印

请高手帮忙看下CAN无接收中断

[复制链接]
2476|11
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
coolkee_wang|  楼主 | 2013-5-25 16:20 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
本人用的是STM32F10X_CL,两个CAN,均没有重映射;
CAN1_RXD : PA11
CAN1_TXD : PA12
CAN2_RXD : PB12
CAN2_TXD : PB13

相关代码如下:
void CAN_Config(void)
{
  GPIO_InitTypeDef  GPIO_InitStructure;

  /* Configure CAN1 and CAN2 IOs **********************************************/
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC, ENABLE);
  
  /* Configure CAN1 RX pin */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
   
  /* Configure CAN2 RX pin */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;  
  GPIO_Init(GPIOB, &GPIO_InitStructure);

  /* Configure CAN1 TX pin */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;  
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GPIOA, &GPIO_InitStructure);  

  /* Configure CAN2 TX pin */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
  GPIO_Init(GPIOB, &GPIO_InitStructure);

  /* Remap CAN1 and CAN2 GPIOs */
  //GPIO_PinRemapConfig(GPIO_Remap2_CAN1 , ENABLE);
  //GPIO_PinRemapConfig(GPIO_Remap_CAN2, ENABLE);

  /* Configure CAN1 and CAN2 **************************************************/  
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1 | RCC_APB1Periph_CAN2, ENABLE);  
  
   /* CAN1 and CAN2 register init */
  CAN_DeInit(CAN1);
  CAN_DeInit(CAN2);

  /* Struct init*/
  CAN_StructInit(&CAN_InitStructure);

  /* CAN1 and CAN2  cell init */
  CAN_InitStructure.CAN_TTCM = DISABLE;             //禁止时间触发通信模式
  CAN_InitStructure.CAN_ABOM = ENABLE;             //通过软件请求使CAN退出离线状态
  CAN_InitStructure.CAN_AWUM = DISABLE;             //自动唤醒:由软件清除CAN_MCR寄存器的Sleep位唤醒
  CAN_InitStructure.CAN_NART = DISABLE;
  CAN_InitStructure.CAN_RFLM = DISABLE;             //FIFO未被锁定,溢出时新报文覆盖未被读走的报文
  CAN_InitStructure.CAN_TXFP = ENABLE;
  CAN_InitStructure.CAN_Mode = CAN_Mode_Normal;
  
  CAN_InitStructure.CAN_SJW = CAN_SJW_1tq;  
  CAN_InitStructure.CAN_BS1 = CAN_BS1_3tq;
  CAN_InitStructure.CAN_BS2 = CAN_BS2_2tq;
   
#if CAN_BAUDRATE == 1000 /* 1MBps */
    CAN_InitStructure.CAN_Prescaler =6;
#elif CAN_BAUDRATE == 500 /* 500KBps */
    CAN_InitStructure.CAN_Prescaler =12;
#elif CAN_BAUDRATE == 250 /* 250KBps */
    CAN_InitStructure.CAN_Prescaler =24;
#elif CAN_BAUDRATE == 125 /* 125KBps */
    CAN_InitStructure.CAN_Prescaler =48;
#elif  CAN_BAUDRATE == 100 /* 100KBps */
    CAN_InitStructure.CAN_Prescaler =60;
#elif  CAN_BAUDRATE == 50 /* 50KBps */
    CAN_InitStructure.CAN_Prescaler =120;
#elif  CAN_BAUDRATE == 20 /* 20KBps */
    CAN_InitStructure.CAN_Prescaler =300;
#elif  CAN_BAUDRATE == 10 /* 10KBps */
    CAN_InitStructure.CAN_Prescaler =600;
#else
#endif  /* CAN_BAUDRATE == 1000 */

  /*Initializes the CAN1  and CAN2 */
  CAN_Init(CAN1, &CAN_InitStructure);
  CAN_Init(CAN2, &CAN_InitStructure);

  /* CAN1 filter init */
  CAN_FilterInitStructure.CAN_FilterNumber = 1;     // 0 ~ 27
  CAN_FilterInitStructure.CAN_FilterMode = CAN_FilterMode_IdMask;
  CAN_FilterInitStructure.CAN_FilterScale = CAN_FilterScale_32bit;
  CAN_FilterInitStructure.CAN_FilterIdHigh = 0x6420;
  CAN_FilterInitStructure.CAN_FilterIdLow = 0x0000;
  CAN_FilterInitStructure.CAN_FilterMaskIdHigh = 0x0000;
  CAN_FilterInitStructure.CAN_FilterMaskIdLow = 0x0000;
  CAN_FilterInitStructure.CAN_FilterFIFOAssignment = 0;
  CAN_FilterInitStructure.CAN_FilterActivation = DISABLE;
  CAN_FilterInit(&CAN_FilterInitStructure);
  
  /* CAN2 filter init */
  CAN_FilterInitStructure.CAN_FilterIdHigh =0x2460;
  CAN_FilterInitStructure.CAN_FilterNumber = 15;
  CAN_FilterInit(&CAN_FilterInitStructure);
  
  /* Transmit */
  TxMessage.StdId = 0x321;
  TxMessage.ExtId = 0x01;
  TxMessage.RTR = CAN_RTR_DATA;
  TxMessage.IDE = CAN_ID_STD;
  TxMessage.DLC = 8;  
}
void NVIC_Config(void)
{
  NVIC_InitTypeDef  NVIC_InitStructure;

  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);

  NVIC_InitStructure.NVIC_IRQChannel = CAN1_RX0_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0;   //抢占式优先级
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0;          //响应式优先级
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);

  NVIC_InitStructure.NVIC_IRQChannel = CAN2_RX0_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x1;   //抢占式优先级
  NVIC_Init(&NVIC_InitStructure);
}

void CAN1_RX0_IRQHandler(void)
{
  CAN_Receive(CAN1, CAN_FIFO0, &RxMessage);
}

/**
  * @brief  This function handles CAN2 RX0 Handler.
  * @param  None
  * @retval None
  */

void CAN2_RX0_IRQHandler(void)
{
  CAN_Receive(CAN2, CAN_FIFO0, &RxMessage);
}

沙发
coolkee_wang|  楼主 | 2013-5-25 16:22 | 只看该作者
因为我得到的例子均对CAN进行了重映射,我是在这些例子的基础上改的,使用默认IO口,没有重映射,是不是哪里修改的不彻底啊

使用特权

评论回复
板凳
coolkee_wang|  楼主 | 2013-5-25 20:59 | 只看该作者
C:\Documents and Settings\Administrator\桌面\QQ图片20130525180351

使用特权

评论回复
地板
coolkee_wang|  楼主 | 2013-5-26 13:17 | 只看该作者
看了下错误寄存器
EWGF和EPVF都被置1,LEC = 0x02,REC = 0xff

使用特权

评论回复
5
hawksabre| | 2013-5-26 15:09 | 只看该作者
不是很了解   楼主   帮你顶一个   呵呵   看看别人的意见

使用特权

评论回复
6
coolkee_wang|  楼主 | 2013-5-26 17:48 | 只看该作者
谢谢啊,自己搞了好几天了,还没有搞定
目前发现的现象:CAN1_RXD : PA11管脚有波形,说明有数据到MCU了,但是从错误寄存器EWGF和EPVF都被置1,LEC = 0x02,REC = 0xff
来看,是有错误。
明天上班打算用示波器仔细看下,PA11管脚上的波形是否正确;

使用特权

评论回复
7
coolkee_wang|  楼主 | 2013-5-26 17:53 | 只看该作者
我用的CAN收发器是TJA1055。
没有接收中断肯定是接收错误导致的,所以首先要把这几个错误原因找到。
我已经用CAN分析仪以100K的速率发数据给TJA1055了,满足了CAN总线上两个节点的条件;
会不会是CAN的波特率不匹配导致接收错误??

使用特权

评论回复
8
coolkee_wang|  楼主 | 2013-5-26 17:59 | 只看该作者
还有的就是,我用环回的例子测试是OK的

使用特权

评论回复
9
xiaolanghahaha| | 2013-5-28 10:00 | 只看该作者
听楼主的描述,最有可能就是波特率设置的问题,两机波特率必须要保持完全一致,不然肯定不能进入接收中断,即使你能检测到rx有信号

使用特权

评论回复
10
song19881218| | 2013-5-28 12:58 | 只看该作者
是不是终端电阻没接呢?

使用特权

评论回复
11
haitao10086| | 2013-5-29 16:19 | 只看该作者
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_12;            //CAN1 Tx Pin
        GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
        GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_Init(GPIOA,&GPIO_InitStruct);
        GPIO_InitStruct.GPIO_Pin = GPIO_Pin_11;            //CAN1 Rx Pin
        GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IPU;
        GPIO_Init(GPIOA,&GPIO_InitStruct);
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/*
*****************************************************************************
* @@FuncName::
* @@Desc::
* @@Input::
* @@Output::
* @@Return::
******************************************************************************
*/
void stm32f10x_can_init(void)
{
        CAN_InitTypeDef CAN_InitStructure;
        CAN_FilterInitTypeDef CAN_FilterInitStructure;

        CAN_DeInit(CAN1);

         /* 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_Normal;
        CAN_InitStructure.CAN_SJW = CAN_SJW_1tq;
          
        /* CAN Baudrate = 125KBps (CAN clocked at 36 MHz) */
        CAN_InitStructure.CAN_BS1 = CAN_BS1_13tq;
        CAN_InitStructure.CAN_BS2 = CAN_BS2_2tq;
        CAN_InitStructure.CAN_Prescaler = 18;       //can bps= (fclk/pre)/(SJW+BS1+BS2)
        CAN_Init(CAN1, &CAN_InitStructure);

        CAN_FilterInitStructure.CAN_FilterNumber = 0;
        CAN_FilterInitStructure.CAN_FilterMode = CAN_FilterMode_IdMask;
          CAN_FilterInitStructure.CAN_FilterScale = CAN_FilterScale_32bit;
          CAN_FilterInitStructure.CAN_FilterIdHigh = 0;
          CAN_FilterInitStructure.CAN_FilterIdLow = 0;
          CAN_FilterInitStructure.CAN_FilterMaskIdHigh = 0;
          CAN_FilterInitStructure.CAN_FilterMaskIdLow = 0;
          CAN_FilterInitStructure.CAN_FilterFIFOAssignment = CAN_FilterFIFO0;
          CAN_FilterInitStructure.CAN_FilterActivation = ENABLE;
          CAN_FilterInit(&CAN_FilterInitStructure);

        /* Enable FIFO 0 message pending Interrupt */
        CAN_ITConfig(CAN1, CAN_IT_FMP0, ENABLE);
       
#if 0
          /* Transmit */
          can_tx_msg.StdId = 0x321;
          can_tx_msg.ExtId = 0x01;
          can_tx_msg.RTR = CAN_RTR_DATA;
          can_tx_msg.IDE = CAN_ID_STD;
          can_tx_msg.DLC = 1;
#endif
}

使用特权

评论回复
12
忧忧| | 2014-10-2 16:30 | 只看该作者
你的接收中断没有打开
CAN_ITConfig(CAN1, CAN_IT_FMP0, ENABLE);
同楼上

使用特权

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

本版积分规则

1

主题

12

帖子

0

粉丝