7. CAN bus communication

7.1. Purpose of the experiment

Using the CAN communication of STM32, using the loopback mode, the key control sends CAN data, interrupts the received CAN data and prints it out through the serial port assistant.

7.2. Configuration pin information

Since each new project needs configuration information, it is more troublesome. Fortunately, STM32CubeIDE provides the function of importing .ioc files, which can help us save time.

  1. Import the ioc file from the Serial project and name it CAN.

Find CAN in Connectivity and tick Activated to enable the CAN peripheral.

image-20220326121240215

  1. According to the schematic diagram, the pins connected to the CAN bus are PB8 and PB9, while the default CAN bus pins are PA11 and PA12, so it is necessary to manually modify the CAN bus pins to PB8 and PB9.

image-20220326122605409

image-20220326122759412

image-20220326122910978

  1. Set the parameters of the CAN peripheral, here we set the baud rate to 1000kbps and the mode to Loopback.

image-20220326121651092

Since this is only used to test communication, choose Loopback mode (data is sent and received automatically); if you need to connect a third-party CAN device, please choose Normal mode (data receiving/sending independent).

image-20220326121936036

  1. Turn on the CAN RX0 interrupt in the interrupt setting. If the interrupt is not turned on, the data cannot be received.

image-20220326122355699

7.3. Analysis of the experimental flow chart

image-20220519101843527

7.4. core code explanation

  1. Create new buzzer driver library bsp_can.h and bsp_can.c files in BSP. Add the following to bsp_can.h:

image-20220326140058393

  1. Add the following in bsp_can.c:

Can_Init(): Initialize the CAN peripheral related content, set the CAN receive filter, and open the CAN bus communication.

image-20220326140418820

  1. In order to test the sending data, the new Can_Test_Send() function sends the data through CAN and prints it to the serial port assistant. If you need to modify the sent data, you can modify the TxData array before sending.

image-20220326140721179

  1. The CAN receive interrupt callback function prints the received CAN data through the serial port. The name of this function cannot be modified, otherwise the function cannot be called.

image-20220326140956603

  1. In BSP initialization, call the Can_Init() function to initialize the CAN peripherals.

image-20220326141309119

  1. After the button is pressed, the function of sending CAN data is added.

image-20220326141424603

7.5. Hardware connection

Since the loopback mode is used, the CAN interface may not be connected to external devices.

image-20220326173544238

7.6. Experimental effect

After programming the program, the LED light flashes once every 200 milliseconds. After connecting the expansion board to the computer through the micro-USB data cable, open the serial port assistant (the specific parameters are shown in the figure below), and the buzzer will sound every time the button is pressed. 50 milliseconds, you can see that the serial port assistant will display the data sent by CAN and the data received by CAN.

image-20220519101952043