Skip to playerSkip to main content
Learn how to implement **CAN protocol communication between two STM32 boards** (Blue Pill and Nucleo F446RE) step by step. This is **Part 2 of the STM32 CAN tutorial**, where we go beyond the basics and actually set up real hardware communication using **MCP2551 CAN transceivers**.

This tutorial helps you understand **STM32 CAN bus communication** in practice and is ideal if you are working on automotive, robotics, or embedded networking projects.

► Download Source Code : [https://controllerstech.com/can-protocol-in-stm32/]

► Watch Related Videos :
- Part 1 : [STM32 CAN Filter Configuration](https://youtu.be/JfWlIY0zAIc)
- Part 3 : [STM32 CAN Multiple Devices](https://youtu.be/-lcrrRrKdFg)
- FDCAN Tutorial : [FDCAN in STM32](https://youtu.be/kXyzaaSk6Qs)
- STM32 CAN Playlist : https://www.youtube.com/playlist?list=PLfIJKC1ud8gjzwOPq9fvQt38Ut7EjsgwI

#stm32, #canprotocol, #stm32tutorial, #bluepill, #nucleo, #embeddedc, #armmbed, #embeddedprogramming, #stm32cubemx, #stm32f4, #canbus, #automotive, #iot, #controllerstech

________________________________________________________________________________________

Facebook Page : https://www.facebook.com/controllerstech

Telegram Group : https://t.me/controllerstechdiscuss

Instagram : https://www.instagram.com/controllerstech/

For more info, visit https://controllerstech.com

Category

🤖
Tech
Transcript
00:00Hello everyone. Welcome to Controllers Tech.
00:13This is part 2 of the tutorial on CAN peripheral, and today we will see how to communicate between two CAN devices.
00:21I have already covered the basics of the CAN protocol, and you must watch that video before this one.
00:27You can see the video on the top right. So let's start with this video now.
00:35I am using the blue pill, and the F446RE controllers, and the connection is as shown here.
00:42The RX and the TX pins from the transceivers, are connected to the respective microcontrollers.
00:49Then CAN high, and CAN low are connected with each others.
00:52Note that there must be a 120 ohms resistance connected at each node, just as it is shown in figure.
01:00Some transceivers have this resistance on the breakout board itself, and other don't.
01:06So make sure you check the resistance.
01:08Here is how the connection is.
01:16I have twisted the wire pair connected between CAN high and CAN low.
01:20You could try with simple wires, but if doesn't work, try twisted pair.
01:26Here is the 120 ohms resistance connected between the CAN high and low.
01:31So, I have it connected on the other node too.
01:35P11, and P12 from blue pill are connected to the first transceiver, and the same pins from
01:41nucleo are connected to the second transceiver.
01:45The transceivers are powered with 5 volts from the microcontrollers.
01:50This is all about the connection, now let's start the project for the nucleo first.
01:55So here we are in the cube Mx.
02:03First of all, I am selecting the external crystal for the clock.
02:08I have 8 MHz crystal, and I want the system to run at maximum 180 MHz.
02:18Notice here that the 446 have two CAN peripherals.
02:22As mentioned in the reference manual, the CAN 1 is the master CAN, and the CAN 2 is the slave CAN.
02:34Also they have 28 filter banks all together.
02:41Let's configure the CAN 1.
02:45Now here I am going to set the board rate to 500,000 bits per second.
02:52I am just modifying these parameters to get that.
03:06Here I got the board rate.
03:10Now make sure that the operating mode is set to normal.
03:14And I am going to use FIFO 0 to store the incoming message, so in the NVIC, I am enabling the RX0 interrupt.
03:27This is it for CAN setup.
03:30Now I am setting the PA5 as output for the onboard LED.
03:34Also, I am selecting the external interrupt for the user button, which is connected to the PC-13.
03:43This is it, now click save to generate the project.
03:47I hope you remember things from previous video, because I am going to skip some explanation here.
03:59Let's write the program now.
04:02This is where the transmit header data will be stored.
04:08RX header will store the header from the incoming message.
04:12Let's create the arrays to store the TX data, and the RX data.
04:21And at last the variable for the TX mailbox.
04:29Now in the main function, start the CAN.
04:32Activate the notification for the data pending in the RX FIFO.
04:42Everything I am doing has already been covered in the previous video.
04:46Kindly watch it if you don't understand something here.
05:02After activating the notification, we will load data in the TX header.
05:20I am going to send two data bytes, so DLC is two.
05:25We will be using standard ID.
05:32And the ID will be 0x446.
05:37This is the ID of F446RE.
05:40Whenever this controller will send the message, this ID will act as the ID of the sender.
05:46Now before I send data, let me explain what I actually want to achieve here.
05:51This controller will send the two data bytes.
05:56The first data byte will act as delay for the LED on the second board.
06:00And the second data byte will act as the number of times, that LED will blink.
06:06When the second controller receives this message, the LED on it will blink according to these instructions.
06:13I am not going to send the data here, but instead let's send it in the XTCallback function.
06:20When the user button is pressed, this function will be called, and we will send the data here.
06:30The delay will be 100 milliseconds.
06:34And the loop will be repeated 10 times.
06:36Now we will send the data using Holc and add TX message.
06:50Here we have activated the notification for the receive.
06:59When this controller will receive message from the blue pill, a message pending callback will be called.
07:05Inside this callback, we will receive the data from the FIFO 0.
07:21The header will be stored in RX header, and the data will be stored in the RX data.
07:27Let's create a flag.
07:38We will just do one more check, if the data length is 2 bytes, then the flag will set.
07:43If the flag is set, we will blink the LED.
08:04The number of times it will blink, will be the second byte of the RX data.
08:18And the time delay will be the first byte.
08:29And finally reset the flag, so that there is no false blink.
08:33Let's build it once.
08:40I think I forgot to enable the interrupt for the XD line.
08:44Let's enable it.
08:53Next we have to configure the filters.
09:06We will configure inside the CAN initialization function.
09:14Here is the filter configuration that I am using.
09:18Enable the CAN filter.
09:20I am assigning 20 filter banks to the CAN 1.
09:23Out of these 20, I am using filter bank number 18.
09:33This is the ID that should pass through.
09:37This ID will be assigned to the blue pill later.
09:40And the same value in the mask ID register also.
09:45I am using FIFO 0, to store the incoming message.
09:49This configuration is explained in the previous video,
09:53so make sure you watch it.
09:55Let's build it now.
09:57Now since I am going to connect both the controllers to the same computer,
10:01I don't want the SD links to mix up.
10:04So I am going to assign the respective SD links to their controllers.
10:09Right now I have connected only the F446, and if I scan the SD link here, it will only show one.
10:17So we will keep this SD link assigned to this board.
10:29That's all for this.
10:30Now let's create another project for blue pill.
10:45I am doing the basic setup, and running the system at maximum clock.
10:49Activate the CAN peripheral.
10:55We have to select the same board rate here, that is 500,000 bits per second.
11:01Here I will use the FIFO 1 for receiving the message, this is just to demonstrate.
11:07PC13 is the LED on board, so select it as output.
11:12That's it, click save to generate the project.
11:15I am going to use the same code, that I used in 446.
11:24So copy everything from here.
11:33We are not using the external interrupt in blue pill.
11:36Let's copy this part now.
11:57Here we don't have CAN 1, or CAN 2, instead it's just CAN.
12:02As I said, I am using FIFO 1 here.
12:06So replace all FIFO 0 with FIFO 1.
12:17The transmit ID for blue pill will be 0x103.
12:26Now let's copy the while loop.
12:28Here the LED is PC13.
12:43Now since we are not using the external interrupt here, we will send the data in this loop itself.
12:48Let's define the data, that we will send.
12:53And after the LED is finished blinking, we will send the data using, holad tx message.
13:10Now copy the filter configuration.
13:24Here the start slave bank does not matter, like I explained in the previous video.
13:37And the filter bank should be between 0 to 13.
13:54The ID that will be allowed to pass is, 0x446, the ID of the F4 controller.
14:04Okay, this should be CAN.
14:14And we are using FIFO 1.
14:17All right, it's correct now.
14:30So I have removed the F4 SD link, and now only the blue pill is connected.
14:36If we scan the SD link, only one shows up, and we will assign it to the blue pill.
14:50Now the code has been loaded into both the microcontrollers.
14:54Let's see if it works as expected.
14:56I have also connected the analyzer, to see the data flow.
15:03Channel 0 is connected to the TX of 446, and channel 1 is connected to the TX of blue pill.
15:14Pay attention to the LEDs on both the microcontrollers.
15:18The communication will start only when the button on the 446 is pressed.
15:23As you can see, when I press the button, the LED on blue pill blinks for some time,
15:29and then the LED on 446 starts blinking.
15:33And then everything stops, until the button is pressed again.
15:37This is exactly what we programmed it to do.
15:40Let's see the data.
15:51This is the message sent by 446.
15:55You can see the ID, the control field, the two data bytes.
16:00Once this message is received by the blue pill, the LED will blink,
16:04and once the LED finished blinking, the blue pill will send the message.
16:08And here you can see the message sent by blue pill.
16:24You can see the same process will repeat every time we press the button.
16:28Let's try changing the data for both of them.
16:45It isen vélezilles.
16:47For the moment, the LED cuts the data for both of those two and five times.
16:50There are a lower הוא companies from five times the key sources.
16:52Here
16:55if it outlines the video probably in the��object agency,
16:58you can see the audio cut off the button.
17:02Here you can see the display on the right value of the LED button.
17:04You can see the audio cut off in there.
17:05You can see the camera on the right-click on theahu cover...
17:07You can see the camera on the left android calls and the new connection link for the endpoints.
17:10Now there is a ad lib kabin.
17:11You can see the LED is blinking somewhat longer now.
17:24Also observe the message on the analyzer.
17:43There is almost a delay of 4 seconds here.
17:46We are sending the 100ms delay, and 40 times blink to the blue pill, and that's why this
17:524 seconds.
17:54So this works pretty good.
18:06Now if you don't want to press the button every time, you can just send this data, after
18:11the blinking is finished.
18:14Just like we are doing in the blue pill.
18:17This way the process will keep going forever, without any manual intervention.
18:25Let's run it now.
18:37I will start the process, by pressing the button once.
18:41You can see the LED on blue pill is blinking.
18:45And now the LED on the 446.
18:53And this process is keep repeating by itself.
19:00This is how we can use the CAN for the communication.
19:03We can send up to 8 data bytes at a time.
19:09Filters allow the message filtering at the hardware level itself, so the CPU don't have
19:14to do that work.
19:18This is it for this video.
19:20I hope you guys understood the CAN protocol, and how to use it for communication.
19:26Filter configuration is covered in the previous video, so watch it if you haven't watched it
19:31yet.
19:32You can download the code from the link in the description.
19:36Keep watching, and have a nice day ahead.
Be the first to comment
Add your comment

Recommended