STM32 HAL CAN

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/weifengdq/article/details/80585460

STM32_CAN

CAN Project on STM32

CAN Analyzer use case

Taobao bought by sales of CAN analyzer , test products are Benewake the TF02 , a serial port and CAN interface, here, of course, CAN interface.

CAN information given on the data sheet TF02 follows:
tf02_1
tf02_2
That is 1Mbps, ID is 0x00090002, the frame format for the extended frame, Byte0 DIST high as 8, Byte1 DIST is the lower 8 bits.

TF02 to the 5V power supply, respectively, then the CANH and CANL H and L CAN interface CAN1 analyzer, connected to a terminal resistor 120Ω, as shown:
can1

Open CAN Analyzer PC USB-CAN Tool V2.12, equipment operators -> boot device:
can2

OK, you can see the output of the data:
can3

The frame rate can be seen in FIG 103.8, ID number 0x009002, extended frame format of the frame, the length of 8 bytes, then the 8-byte hexadecimal data.


STM32 CAN read

Reference Github can2uart engineering, the STM32 TF02 CAN interface to read the data, it transmits to the host computer through the serial port.
tf02_3

STM32F103TBU6, VP230 (received PA11 CAN_RX, PA12 CAN_TX), USB to serial port (PA9, PA10).
Open STM32CubeMX, Pinout tab to configure the following:
cube1

Clock Configuration tab to configure the following:
cube2

Configuration tab configuration is as follows:
cube3

cube4

APB1 clock hanging on CAN, CAN baud rate 36M / Pre / (BS1 + BS2 + SJW) = 36M / 3 / (5 + 6 + 1) = 1Mbps, exactly corresponding to FIG 1000ns Time for one Bit in. Use FIFO0 receive interrupt, so the USB low priority or CAN RX0 interrupts the hook.

The main code is added main.c follows:

/* USER CODE BEGIN PV */
/* Private variables ---------------------------------------------------------*/
CanRxMsgTypeDef RxMessage;
/* USER CODE END PV */

/* USER CODE BEGIN PFP */
/* Private function prototypes -----------------------------------------------*/
uint8_t CAN_Init() {
    hcan.pRxMsg = &RxMessage;

    CAN_FilterConfTypeDef filterConfig;

    filterConfig.FilterNumber = 0;
    filterConfig.FilterMode = CAN_FILTERMODE_IDMASK;
    filterConfig.FilterScale = CAN_FILTERSCALE_32BIT;
    filterConfig.FilterIdHigh = 0x0000;
    filterConfig.FilterIdLow = 0x0000;
    filterConfig.FilterMaskIdHigh = 0x0000;
    filterConfig.FilterMaskIdLow = 0x0000;
    filterConfig.FilterFIFOAssignment = CAN_FILTER_FIFO0;
    filterConfig.FilterActivation = ENABLE;
    filterConfig.BankNumber = 14;

    if(HAL_CAN_ConfigFilter(&hcan,&filterConfig) != HAL_OK) return 1;
    return 0;
}

void HAL_CAN_RxCpltCallback(CAN_HandleTypeDef* hcan1) {
    __HAL_CAN_ENABLE_IT(&hcan, CAN_IT_FMP0);
    printf("(0x%08x, %d,", hcan.pRxMsg->ExtId, hcan.pRxMsg->DLC);
    for(int i = 0; i < hcan.pRxMsg->DLC; i++) {
        printf(" %02x", hcan.pRxMsg->Data[i]);
    }
    if(hcan.pRxMsg->DLC >= 2) {
        printf(", %d", (hcan.pRxMsg->Data[0]<<8) + hcan.pRxMsg->Data[1]);   //distance
    }
    printf(")\r\n");
}
/* USER CODE END PFP */

    //main函数中
    /* USER CODE BEGIN 2 */
    printf("Hello, world!\r\n");
    CAN_Init();
    __HAL_CAN_ENABLE_IT(&hcan, CAN_IT_FMP0);
    /* USER CODE END 2 */

After the program is finished open the serial debugging assistant can see the information sent is:
sscom1

Data line (extended frame ID, Number of bytes, the raw hexadecimal data, the calculated distance value TF02).

Guess you like

Origin blog.csdn.net/weifengdq/article/details/80585460