野火电子论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 11496|回复: 1

CUBEMX使用CAN1收发正常,CAN2发送只有一小段波形并且只有上电后一次有

[复制链接]
发表于 2017-9-12 19:12:18 | 显示全部楼层 |阅读模式
芯片:STM32F429IGTx
开发板:野火挑战者F429开发板
开发环境:KEIL 5.24.2
CUBEMX库:STM32F4 1.16.0
现象:配置的CAN1可以和周立功的USBCAN-I-MINI进行正常的收发,CAN2在发送的时候只有很短的一小段波形,而且只在上电后第一次发送有波形,后边在怎么发送都没有波形(通过示波器直接观察PB13引脚).两个CAN外设波特率设置都是500k.
  1. CAN_FilterConfTypeDef sFilterConfig;
  2. static CanTxMsgTypeDef TxMessage1;
  3. static CanRxMsgTypeDef RxMessage1;
  4. static CanTxMsgTypeDef TxMessage2;
  5. static CanRxMsgTypeDef RxMessage2;
  6. void MX_CAN1_Init(void)
  7. {

  8.   hcan1.Instance = CAN1;
  9.   hcan1.Init.Prescaler = 10;
  10.   hcan1.Init.Mode = CAN_MODE_NORMAL;
  11.   hcan1.Init.SJW = CAN_SJW_2TQ;
  12.   hcan1.Init.BS1 = CAN_BS1_5TQ;
  13.   hcan1.Init.BS2 = CAN_BS2_3TQ;
  14.   hcan1.Init.TTCM = DISABLE;
  15.   hcan1.Init.ABOM = DISABLE;
  16.   hcan1.Init.AWUM = DISABLE;
  17.   hcan1.Init.NART = DISABLE;
  18.   hcan1.Init.RFLM = DISABLE;
  19.   hcan1.Init.TXFP = DISABLE;
  20.   if (HAL_CAN_Init(&hcan1) != HAL_OK)
  21.   {
  22.     _Error_Handler(__FILE__, __LINE__);
  23.   }

  24. }
  25. /* CAN2 init function */
  26. void MX_CAN2_Init(void)
  27. {

  28.   hcan2.Instance = CAN2;
  29.   hcan2.Init.Prescaler = 10;
  30.   hcan2.Init.Mode = CAN_MODE_NORMAL;
  31.   hcan2.Init.SJW = CAN_SJW_2TQ;
  32.   hcan2.Init.BS1 = CAN_BS1_5TQ;
  33.   hcan2.Init.BS2 = CAN_BS2_3TQ;
  34.   hcan2.Init.TTCM = DISABLE;
  35.   hcan2.Init.ABOM = DISABLE;
  36.   hcan2.Init.AWUM = DISABLE;
  37.   hcan2.Init.NART = DISABLE;
  38.   hcan2.Init.RFLM = DISABLE;
  39.   hcan2.Init.TXFP = DISABLE;
  40.   if (HAL_CAN_Init(&hcan2) != HAL_OK)
  41.   {
  42.     _Error_Handler(__FILE__, __LINE__);
  43.   }

  44. }

  45. hcan1.pTxMsg = &TxMessage1;
  46. hcan1.pRxMsg = &RxMessage1;
  47. hcan2.pTxMsg = &TxMessage2;
  48. hcan2.pRxMsg = &RxMessage2;

  49. sFilterConfig.FilterNumber = 0;
  50.   sFilterConfig.FilterMode = CAN_FILTERMODE_IDLIST;
  51.   sFilterConfig.FilterScale = CAN_FILTERSCALE_16BIT;
  52.   sFilterConfig.FilterIdHigh = (uint32_t)0x0123<<5;
  53.   sFilterConfig.FilterIdLow = CAN_ID_STD;
  54.   sFilterConfig.FilterMaskIdHigh = 0xFFFF;
  55.   sFilterConfig.FilterMaskIdLow = 0xFFFF;
  56.   sFilterConfig.FilterFIFOAssignment = CAN_FILTER_FIFO0;
  57.   sFilterConfig.FilterActivation = ENABLE;
  58.   sFilterConfig.BankNumber = 14;
  59.    
  60.   if(HAL_CAN_ConfigFilter(&hcan1, &sFilterConfig) != HAL_OK)
  61.   {
  62.         DEBUG("HAL_CAN_ConfigFilter ERR\r\n");
  63.   }

  64.     sFilterConfig.FilterNumber = 14;
  65.     sFilterConfig.FilterFIFOAssignment = CAN_FILTER_FIFO1;
  66.      
  67.     if(HAL_CAN_ConfigFilter(&hcan2, &sFilterConfig) != HAL_OK)
  68.   {
  69.         DEBUG("HAL_CAN_ConfigFilter ERR\r\n");
  70.   }
  71.      
  72.   hcan1.pTxMsg->StdId = 0x123;
  73.   hcan1.pTxMsg->RTR = CAN_RTR_DATA;
  74.   hcan1.pTxMsg->IDE = CAN_ID_STD;
  75.   hcan1.pTxMsg->DLC = 8;
  76.     hcan1.pTxMsg->Data[0] = 0x01;
  77.     hcan1.pTxMsg->Data[1] = 0x02;
  78.     hcan1.pTxMsg->Data[2] = 0x03;
  79.     hcan1.pTxMsg->Data[3] = 0x04;
  80.     hcan1.pTxMsg->Data[4] = 0x05;
  81.     hcan1.pTxMsg->Data[5] = 0x06;
  82.     hcan1.pTxMsg->Data[6] = 0x07;
  83.     hcan1.pTxMsg->Data[7] = 0x08;
  84.      
  85.     hcan2.pTxMsg->StdId = 0x123;
  86.   hcan2.pTxMsg->RTR = CAN_RTR_DATA;
  87.   hcan2.pTxMsg->IDE = CAN_ID_STD;
  88.   hcan2.pTxMsg->DLC = 8;
  89.     hcan2.pTxMsg->Data[0] = 0x01;
  90.     hcan2.pTxMsg->Data[1] = 0x02;
  91.     hcan2.pTxMsg->Data[2] = 0x03;
  92.     hcan2.pTxMsg->Data[3] = 0x04;
  93.     hcan2.pTxMsg->Data[4] = 0x05;
  94.     hcan2.pTxMsg->Data[5] = 0x06;
  95.     hcan2.pTxMsg->Data[6] = 0x07;
  96.     hcan2.pTxMsg->Data[7] = 0x08;

  97.     if(HAL_CAN_Receive_IT(&hcan1, CAN_FIFO0) != HAL_OK)
  98.   {
  99.         DEBUG("HAL_CAN1_Receive_IT ERR\r\n");
  100.   }

  101.     if(HAL_CAN_Receive_IT(&hcan2, CAN_FIFO1) != HAL_OK)
  102.   {
  103.         DEBUG("HAL_CAN2_Receive_IT ERR\r\n");
  104.   }
  105.    
  106. /*******************************
  107. main循环
  108. *******************************/

  109. CAN2_FLAG = HAL_CAN_Transmit(&hcan2, 10);
  110. if(CAN2_FLAG == HAL_OK)
  111. {
  112.     DEBUG("5AL_CAN_Transmit HAL_OK\r\n");
  113. }
  114. else if(CAN2_FLAG == HAL_ERROR)
  115. {
  116.     DEBUG("6AL_CAN_Transmit HAL_ERROR\r\n");
  117. }
  118. else if(CAN2_FLAG == HAL_BUSY)
  119. {
  120.     DEBUG("7AL_CAN_Transmit HAL_BUSY\r\n");
  121. }
  122. else if(CAN2_FLAG == HAL_TIMEOUT)
  123. {
  124.     DEBUG("8AL_CAN_Transmit HAL_TIMEOUT\r\n");
  125. }
  126. CAN2_FLAG = HAL_OK;
  127.             
  128. CAN1_FLAG = HAL_CAN_Transmit(&hcan1, 10);
  129. if(CAN1_FLAG == HAL_OK)
  130. {
  131.     DEBUG("1AL_CAN_Transmit HAL_OK\r\n");
  132. }
  133. else if(CAN1_FLAG == HAL_ERROR)
  134. {
  135.     DEBUG("2AL_CAN_Transmit HAL_ERROR\r\n");
  136. }
  137. else if(CAN1_FLAG == HAL_BUSY)
  138. {
  139.     DEBUG("3AL_CAN_Transmit HAL_BUSY\r\n");
  140. }
  141. else if(CAN1_FLAG == HAL_TIMEOUT)
  142. {
  143.     DEBUG("4AL_CAN_Transmit HAL_TIMEOUT\r\n");
  144. }
  145. CAN1_FLAG = HAL_OK;

  146. void HAL_CAN_RxCpltCallback(CAN_HandleTypeDef* CanHandle)
  147. {
  148.     int i=0;
  149.      
  150.     if(CanHandle ==&hcan1)
  151.     {
  152.         __HAL_CAN_ENABLE_IT(&hcan1,CAN_IT_FMP0);      
  153.         printf("1pRxMsg->StdId : 0x%x\r\n",hcan1.pRxMsg->StdId);
  154.         printf("1pRxMsg->DLC   : 0x%x\r\n",hcan1.pRxMsg->DLC);
  155.         printf("1DATA :");
  156.         for(i=0;i<hcan1.pRxMsg->DLC;i++)
  157.         {
  158.             printf(" 0x%x ",hcan1.pRxMsg->Data[i]);
  159.         }
  160.         printf("\r\n\r\n");
  161.     }
  162.      
  163.     if(CanHandle ==&hcan2)
  164.     {
  165.         __HAL_CAN_ENABLE_IT(&hcan2,CAN_IT_FMP1);      
  166.         printf("2pRxMsg->StdId : 0x%x\r\n",hcan2.pRxMsg->StdId);
  167.         printf("2pRxMsg->DLC   : 0x%x\r\n",hcan2.pRxMsg->DLC);
  168.         printf("2DATA :");
  169.         for(i=0;i<hcan2.pRxMsg->DLC;i++)
  170.         {
  171.             printf(" 0x%x ",hcan2.pRxMsg->Data[i]);
  172.         }
  173.         printf("\r\n\r\n");
  174.     }
  175. }
复制代码
当更改sFilterConfig.FilterNumber = 14;时网上又说仍然要执行HAL_CAN_ConfigFilter(&hcan1, &sFilterConfig)而不能执行HAL_CAN_ConfigFilter(&hcan2, &sFilterConfig),这种方法我也试过,仍旧不行,并且同样尝试过两个CAN使用同一个CAN_FILTER_FIFO0的情况,依然不可行.

示波器上能看到CAN2发送管脚PB13发出的波形大概只有两三位然后就没有了,后边在怎么执行CAN2发送命令也不会再有任何波形.请各位帮忙看一下究竟是哪里出了问题?困扰我三天了死活找不到结症所在

CAN_BUS_CAN2.zip

2.36 MB, 下载次数: 12

程序源码

回复

使用道具 举报

发表于 2017-9-13 08:59:50 | 显示全部楼层
can1和can2没什么区别,主要区别是用can2的时候必须使能Can1的时钟
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

联系站长|手机版|野火电子官网|野火淘宝店铺|野火电子论坛 ( 粤ICP备14069197号 ) 大学生ARM嵌入式2群

GMT+8, 2024-11-22 13:25 , Processed in 0.060212 second(s), 26 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表