7. CAN bus communication 7.1. Purpose of the experiment 7.2. Configuration pin information 7.3. Analysis of the experimental flow chart 7.4. core code explanation 7.5. Hardware connection 7.6. Experimental effect
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.
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.
Find CAN in Connectivity and tick Activated to enable the CAN peripheral.
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).
Can_Init(): Initialize the CAN peripheral related content, set the CAN receive filter, and open the CAN bus communication.
Since the loopback mode is used, the CAN interface may not be connected to external devices.
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.