[求助] stm32 CAN通信遇到问题

lucky_star   2013-8-30 23:14 楼主
    我用的stm32F103VC控制器外接一个TJA1050CAN收发器,加一个CAN分析仪(确定可用USBCAN-II(iTEK公司的))进行测试,至于stm32的CAN测试程序,首先采用回环模式,使能CAN接收中断,调试发现,可以进入接收中断,而且接收的数据与发送的数据相同,故自发自收没有问题了,然后将回环模式改为正常模式,stm32发送,分析仪监听,没有监测到数据,而且stm32的错误寄存器指示显性位错误(bit dom error),CAN初始化及配置程序如下:
void CAN_INIT(void)
{
   GPIO_InitTypeDef GPIO_InitStructure;
   CAN_InitTypeDef        CAN_InitStructure;
   CAN_FilterInitTypeDef  CAN_FilterInitStructure;

  RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1 | RCC_APB2Periph_GPIOB, ENABLE);//时钟使能
  GPIO_PinRemapConfig(GPIO_Remap1_CAN1,ENABLE);//CAN管脚重映射到PB8-9

  /* Configure CAN pin: RX =  PB8 */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  GPIO_Init(GPIOB, &GPIO_InitStructure);

  /* Configure CAN pin: TX =  PB9 */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  GPIO_Init(GPIOB, &GPIO_InitStructure);

  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_Mode_Normal;//
  CAN_InitStructure.CAN_SJW=CAN_SJW_1tq;
  CAN_InitStructure.CAN_BS1=CAN_BS1_8tq;
  CAN_InitStructure.CAN_BS2=CAN_BS2_7tq;
  CAN_InitStructure.CAN_Prescaler=18;
  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;
  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;
  CAN_FilterInit(&CAN_FilterInitStructure);

  /* CAN FIFO0 message pending interrupt enable */
  CAN_ITConfig(CAN1, CAN_IT_FMP0, ENABLE);
}
//CAN发送函数
void can_write(uint16_t id, uint8_t *data_buf, uint8_t len)   
{
  /* transmit 1 message */
  TxMessage.StdId = id;
  TxMessage.ExtId = 0x0000;
  TxMessage.IDE = CAN_ID_STD;
  TxMessage.RTR = CAN_RTR_DATA;
  TxMessage.DLC = len;
  memcpy(TxMessage.Data, data_buf, len);

  CAN_Transmit(CAN1, &TxMessage);
}

CAN中断处理函数
void USB_LP_CAN1_RX0_IRQHandler(void)
{
    CAN_Receive(CAN1, CAN_FIFO0, &RxMessage);
    printf("recv a frame\r\n");
}

敢问大神问题出在什么地方?忘指点一二,小弟不胜感激!

回复评论 (2)

回复 楼主lucky_star 的帖子

呵呵!问题解决了,上面程序有两处错误:
1).RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1 | RCC_APB2Periph_GPIOB, ENABLE);应该改为
RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);
2).RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOB, ENABLE);//RCC_APB2Periph_AFIO复用时钟不能少;
问题解决了,先高兴一下,呵呵!
点赞  2013-8-31 00:15

谢谢,楼主分享

http://shop34182318.taobao.com/ https://shop436095304.taobao.com/?spm=a230r.7195193.1997079397.37.69fe60dfT705yr
点赞  2013-8-31 05:26
电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 京公网安备 11010802033920号
    写回复