关于使用STM32F103ZET6单片机CAN通讯无法正常发送问题

2019-07-14 13:10发布

通过STLINK硬件调试,程序每次运行到HAL_CAN_AddTxMessage(&hcan,&TxMessage,TData,(uint32_t*)CAN_TX_MAILBOX0);这个函数的
if (pHeader->IDE == CAN_ID_STD)
这一行的时候就会跳转到HardFault_Handler()中。

哪位大神知道为什么啊?


下面是我的代码
  1. int main(void)
  2. {
  3.   /* USER CODE BEGIN 1 */

  4.   /* USER CODE END 1 */

  5.   /* MCU Configuration--------------------------------------------------------*/

  6.   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  7.   HAL_Init();

  8.   /* USER CODE BEGIN Init */

  9.   /* USER CODE END Init */

  10.   /* Configure the system clock */
  11.   SystemClock_Config();

  12.   /* USER CODE BEGIN SysInit */

  13.   /* USER CODE END SysInit */

  14.   /* Initialize all configured peripherals */
  15.   MX_GPIO_Init();
  16.   MX_CAN_Init();
  17.   MX_USART1_UART_Init();
  18.   /* USER CODE BEGIN 2 */
  19.         Init_CANFilter();
  20.         CAN_SetTxMsg();
  21.         HAL_CAN_Start(&hcan);
  22.         HAL_CAN_ActivateNotification(&hcan,CAN_IT_RX_FIFO0_MSG_PENDING);
  23.   /* USER CODE END 2 */

  24.   /* Infinite loop */
  25.   /* USER CODE BEGIN WHILE */
  26.   while (1)
  27.   {
  28.                 HAL_CAN_AddTxMessage(&hcan,&TxMessage,TData,(uint32_t*)CAN_TX_MAILBOX0);
  29.                 HAL_Delay(1000);
  30.                 HAL_GPIO_WritePin(GPIOC,GPIO_PIN_13,GPIO_PIN_RESET);
  31.                 HAL_Delay(1000);
  32.                 HAL_GPIO_WritePin(GPIOC,GPIO_PIN_13,GPIO_PIN_SET);
  33.     /* USER CODE END WHILE */

  34.     /* USER CODE BEGIN 3 */
  35.   }
  36.   /* USER CODE END 3 */
  37. }

  38. CAN_HandleTypeDef hcan;

  39. /* CAN init function */
  40. void MX_CAN_Init(void)
  41. {

  42.   hcan.Instance = CAN1;
  43.   hcan.Init.Prescaler = 9;
  44.   hcan.Init.Mode = CAN_MODE_NORMAL;
  45.   hcan.Init.SyncJumpWidth = CAN_SJW_1TQ;
  46.   hcan.Init.TimeSeg1 = CAN_BS1_5TQ;
  47.   hcan.Init.TimeSeg2 = CAN_BS2_2TQ;
  48.   hcan.Init.TimeTriggeredMode = DISABLE;
  49.   hcan.Init.AutoBusOff = DISABLE;
  50.   hcan.Init.AutoWakeUp = DISABLE;
  51.   hcan.Init.AutoRetransmission = DISABLE;
  52.   hcan.Init.ReceiveFifoLocked = DISABLE;
  53.   hcan.Init.TransmitFifoPriority = DISABLE;
  54.   if (HAL_CAN_Init(&hcan) != HAL_OK)
  55.   {
  56.     Error_Handler();
  57.   }

  58. }

  59. void Init_CANFilter(void)
  60. {
  61.         CAN_FilterTypeDef hFilterConfig;
  62.         hFilterConfig.FilterBank = 0;
  63.         hFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK; //工作在标识符屏蔽位模式
  64.         hFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT;//过滤器位宽为单个32位。
  65.         hFilterConfig.FilterIdHigh = (((uint32_t)0x1314<<3)&0xFFFF0000)>>16; //要过滤的ID高位
  66.         hFilterConfig.FilterIdLow = (((uint32_t)0x1314<<3)|CAN_ID_EXT|CAN_RTR_DATA)&0xFFFF; //要过滤的ID低位
  67.         hFilterConfig.FilterMaskIdHigh = 0x0; //过滤器高16位每位必须匹配
  68.         hFilterConfig.FilterMaskIdLow = 0x0; //过滤器低16位每位必须匹配
  69.         hFilterConfig.FilterFIFOAssignment = CAN_FILTER_FIFO0; //过滤器被关联到FIFO 0
  70.         hFilterConfig.SlaveStartFilterBank = 14;
  71.         hFilterConfig.FilterActivation = ENABLE; //使能过滤器
  72.         
  73.         HAL_CAN_ConfigFilter(&hcan, &hFilterConfig);
  74.         //HAL_CAN_Start(&hcan);
  75. }

  76. void CAN_SetTxMsg(void)
  77. {
  78.         TxMessage.StdId = 0;
  79.         TxMessage.ExtId = 0X1314;
  80.         TxMessage.DLC = 2;
  81.         TxMessage.IDE = CAN_ID_EXT;
  82.         TxMessage.RTR = CAN_RTR_DATA;
  83.         TData[0] = 0xAA;
  84.         TData[1] = 0xAA;
  85. }
复制代码

友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
该问题目前已经被作者或者管理员关闭, 无法添加新回复
2条回答
ZiiDo_K
1楼-- · 2019-07-14 18:14
 精彩回答 2  元偷偷看……
♂落葉歸根
2楼-- · 2019-07-14 19:45
用ST的HAL库,吃不透很苦恼的,还是喜欢原来的库

一周热门 更多>