打印
[STM32F2]

stm32f205rG,CAN2可以接收,不能发送,CAN1正常

[复制链接]
1990|5
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
john78|  楼主 | 2016-9-19 10:40 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
本帖最后由 john78 于 2016-9-19 10:43 编辑

void CANinit()
{
  
  GPIO_InitTypeDef  GPIO_InitStructure;
  NVIC_InitTypeDef   NVIC_InitStructure;
  CAN_InitTypeDef        CAN_InitStructure;
  CAN_FilterInitTypeDef  CAN_FilterInitStructure;
  
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1,ENABLE);  //一定要打开RCC_APB1Periph_CAN1?|,
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN2,ENABLE);  //一定要打开RCC_APB1Periph_CAN1?|,
  
  NVIC_InitStructure.NVIC_IRQChannel = CAN1_RX0_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);  
  

  // CAN GPIOs configuration *************************************************

  // Enable GPIO clock
  //RCC_AHB1PeriphClockCmd(CAN_GPIO_CLK, ENABLE);

  //Connect CAN pins to AF9
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource11, GPIO_AF_CAN1);
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource12, GPIO_AF_CAN1);
  
  // Configure CAN RX and TX pins
  
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11 | GPIO_Pin_12;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_UP;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
  
   CAN_DeInit(CAN1);
  // CAN cell init
  
  CAN_InitStructure.CAN_TTCM=DISABLE; //禁止时间除法通讯方式
  CAN_InitStructure.CAN_ABOM=DISABLE; //软件对CAN_MCR寄存器的INRQ位进行置1后清零
  CAN_InitStructure.CAN_AWUM=DISABLE; //睡眠迷失通过清楚CAN_MCR的SLEEP位,右软件唤醒
  CAN_InitStructure.CAN_NART=DISABLE;//DISABLE; //CAN报文执法一次,不管是否成功
  CAN_InitStructure.CAN_RFLM=DISABLE;  //接收FIFO锁定
  CAN_InitStructure.CAN_TXFP=DISABLE;  //标识别符小的报文具有优先级
  

  CAN_InitStructure.CAN_Mode = CAN_Mode_Normal;//CAN_Mode_LoopBack;// CAN_Mode_Normal;
  CAN_InitStructure.CAN_SJW = CAN_SJW_1tq;
   
  // CAN Baudrate = 1MBps (CAN clocked at 30 MHz)
  CAN_InitStructure.CAN_BS1 = CAN_BS1_6tq;
  CAN_InitStructure.CAN_BS2 = CAN_BS2_8tq;
  CAN_InitStructure.CAN_Prescaler = 2;
  //CAN_Init(CAN1, &CAN_InitStructure);
  CAN_Init(CAN1, &CAN_InitStructure);
  
  // CAN filter init
  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);



  // Enable FIFO 0 message pending Interrupt
  CAN_ITConfig(CAN1, CAN_IT_FMP0, ENABLE);
  //CAN_ITConfig(CAN2, CAN_IT_TME, ENABLE);  //传送中断
  //CAN_ITConfig(CAN1, CAN_IT_EWG|CAN_IT_EPV|CAN_IT_BOF|CAN_IT_LEC|CAN_IT_ERR, ENABLE);
  
  
//CN2---------------------------------------------------------------------------------------------
  NVIC_InitStructure.NVIC_IRQChannel = CAN2_RX0_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority =3;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);  
  

  // CAN GPIOs configuration *************************************************

  // Enable GPIO clock
   RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);

  //Connect CAN pins to AF9
  GPIO_PinAFConfig(GPIOB, GPIO_PinSource5, GPIO_AF_CAN2);
  GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_CAN2);
  
  // Configure CAN RX and TX pins
  
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_UP;
  GPIO_Init(GPIOB, &GPIO_InitStructure);

  // CAN configuration *******************************************************
  // Enable CAN clock
  CAN_DeInit(CAN2);

  CAN_StructInit(&CAN_InitStructure);
  
  CAN_InitStructure.CAN_TTCM=DISABLE; //禁止时间除法通讯方式
  CAN_InitStructure.CAN_ABOM=DISABLE; //软件对CAN_MCR寄存器的INRQ位进行置1后清零
  CAN_InitStructure.CAN_AWUM=DISABLE; //睡眠迷失通过清楚CAN_MCR的SLEEP位,右软件唤醒
  CAN_InitStructure.CAN_NART=DISABLE;//DISABLE; //CAN报文执法一次,不管是否成功
  CAN_InitStructure.CAN_RFLM=DISABLE;  //接收FIFO锁定
  CAN_InitStructure.CAN_TXFP=DISABLE;  //标识别符小的报文具有优先级
  

  CAN_InitStructure.CAN_Mode = CAN_Mode_Normal;//CAN_Mode_LoopBack;// CAN_Mode_Normal;
  CAN_InitStructure.CAN_SJW = CAN_SJW_1tq;
   
  // CAN Baudrate = 1MBps (CAN clocked at 30 MHz)
  CAN_InitStructure.CAN_BS1 = CAN_BS1_6tq;
  CAN_InitStructure.CAN_BS2 = CAN_BS2_8tq;
  CAN_InitStructure.CAN_Prescaler = 2;
  //CAN_Init(CAN1, &CAN_InitStructure);
  CAN_Init(CAN2, &CAN_InitStructure);
  
//  CAN_SlaveStartBank(14);
  // CAN filter init
  CAN_FilterInitStructure.CAN_FilterNumber = 27;
  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);



  // Enable FIFO 0 message pending Interrupt
  CAN_ITConfig(CAN2, CAN_IT_FMP0, ENABLE);
  //CAN_ITConfig(CAN2, CAN_IT_TME, ENABLE);  //传送中断
// CAN_ITConfig(CAN2, CAN_IT_EWG|CAN_IT_EPV|CAN_IT_BOF|CAN_IT_LEC|CAN_IT_ERR, ENABLE);

}

补充
CAN_TransmitStatus(CAN2, TransmitMailbox);-----返回值是0x2

沙发
NE5532| | 2016-9-19 11:15 | 只看该作者
建议检查引脚第二功能设置是否正确,另可检查硬件波形。

使用特权

评论回复
板凳
mmuuss586| | 2016-9-19 11:17 | 只看该作者
看了半天没看出什么问题;
你再看看程序,硬件应该没啥问题;

使用特权

评论回复
地板
john78|  楼主 | 2016-9-19 14:18 | 只看该作者
晕,用示波器看,发送和接收都有波形的啊,换了个测试从机也这样,CAN1 OK,CAN2 能收,但发不出数据 ,CAN芯片,都换过了啊

使用特权

评论回复
5
john78|  楼主 | 2016-9-20 11:34 | 只看该作者
顶一下,看看有没有人帮忙

使用特权

评论回复
6
xmshao| | 2016-9-20 12:12 | 只看该作者
你回环测试结果怎么样呢?
比较怀疑你硬件问题,

使用特权

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

本版积分规则

70

主题

220

帖子

1

粉丝