4. CAN Interface

2020-07-08  本文已影响0人  T_K_233

首先配置时钟,需要将 FLCK 配置为 36 MHz

image.png

之后配置针脚,图为 500kHz CAN 的配置

image.png

发送信息:

if (HAL_CAN_AddTxMessage(&hcan, &pHeader, &a, &TxMailbox) != HAL_OK) {
    /* Transmission Error */
    Error_Handler();
   }

接收信息:


      int a = HAL_CAN_GetRxFifoFillLevel(&hcan, CAN_RX_FIFO0);
【未完待续】

  /* USER CODE BEGIN 2 */


    pHeader.DLC=1; //give message size of 1 byte
    pHeader.IDE=CAN_ID_STD; //set identifier to standard
    pHeader.RTR=CAN_RTR_DATA; //set data type to remote transmission request?
    pHeader.StdId=0x003; //define a standard identifier, used for message identification by filters (switch this for the other microcontroller)

    //filter one (stack light blink)
    sFilterConfig.FilterFIFOAssignment=CAN_FILTER_FIFO0; //set fifo assignment
    sFilterConfig.FilterIdHigh=0xFFFF; //the ID that the filter looks for (switch this for the other microcontroller)
    sFilterConfig.FilterIdLow=0;
    sFilterConfig.FilterMaskIdHigh=0;
    sFilterConfig.FilterMaskIdLow=0;
    sFilterConfig.FilterScale=CAN_FILTERSCALE_32BIT; //set filter scale
    sFilterConfig.FilterActivation=DISABLE;

    HAL_CAN_ConfigFilter(&hcan, &sFilterConfig); //configure CAN filter


    HAL_CAN_Start(&hcan); //start CAN
    HAL_CAN_ActivateNotification(&hcan, CAN_IT_RX_FIFO0_MSG_PENDING); //enable interrupts

  /* USER CODE END 2 */
 
 

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
    HAL_Delay(1000);

    int arr[8];
    arr[0] = 0xff;
   if (HAL_CAN_AddTxMessage(&hcan, &pHeader, &a, &TxMailbox) != HAL_OK) {
    /* Transmission Error */
    Error_Handler();
   }
    char str[] = "Listening...\r\n";

      HAL_UART_Transmit(&huart2, (uint8_t *) str, strlen(str), 10);

      int a = HAL_CAN_GetRxFifoFillLevel(&hcan, CAN_RX_FIFO0);
      if (a > 0) {
          HAL_UART_Transmit(&huart2, (uint8_t *) "msgpending", strlen("msgpending"), 10);
      }
      HAL_CAN_StateTypeDef state = HAL_CAN_GetState(&hcan);
      if (state == HAL_CAN_STATE_RESET) {
          HAL_UART_Transmit(&huart2, (uint8_t *) "HAL_CAN_STATE_RESET", strlen("HAL_CAN_STATE_RESET"), 10);
      }
      if (state == HAL_CAN_STATE_READY) {
          HAL_UART_Transmit(&huart2, (uint8_t *) "HAL_CAN_STATE_READY", strlen("HAL_CAN_STATE_READY"), 10);
      }
      if (state == HAL_CAN_STATE_LISTENING) {
          HAL_UART_Transmit(&huart2, (uint8_t *) "HAL_CAN_STATE_LISTENING", strlen("HAL_CAN_STATE_LISTENING"), 10);
      }
      if (state == HAL_CAN_STATE_ERROR) {
          HAL_UART_Transmit(&huart2, (uint8_t *) "HAL_CAN_STATE_ERROR", strlen("HAL_CAN_STATE_ERROR"), 10);
      }
      if (state == HAL_CAN_STATE_RESET) {
          HAL_UART_Transmit(&huart2, (uint8_t *) "HAL_CAN_STATE_RESET", strlen("HAL_CAN_STATE_RESET"), 10);
      }


  }
  /* USER CODE END 3 */
}

https://community.st.com/sfc/servlet.shepherd/version/renditionDownload?rendition=THUMB720BY480&versionId=0680X000006qSFR&operationContext=CHATTER&contentId=05T0X00000NPTae&page=0

上一篇下一篇

猜你喜欢

热点阅读