注册 登录
电子工程世界-论坛 返回首页 EEWORLD首页 频道 EE大学堂 下载中心 Datasheet 专题
lucky_star的个人空间 https://home.eeworld.com.cn/space-uid-487017.html [收藏] [复制] [分享] [RSS]
日志

stm32 CAN通信遇到问题

已有 6069 次阅读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\");
}

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

本文来自论坛,点击查看完整帖子内容。

全部作者的其他最新日志
评论 (0 个评论)

facelist doodle 涂鸦板

您需要登录后才可以评论 登录 | 注册

热门文章