STM32 CAN Communication || NORMAL Mode
Summary
TLDRThis tutorial delves into the intricacies of CAN communication between two devices, using the Blue Pill and F446RE microcontrollers. It covers the setup, including connections and resistors, and explains configuring the CAN peripherals in CubeMX. The script guides through coding, including setting transmission parameters, handling interrupts, and filtering. The practical demonstration shows how to send and receive messages, control LEDs based on received data, and ensure continuous communication without manual intervention, highlighting the efficiency of CAN for embedded systems.
Takeaways
- 😀 The tutorial is a continuation of a series on the CAN (Controller Area Network) protocol, focusing on communication between two CAN devices.
- 🔌 The video demonstrates the hardware setup required for CAN communication, including connections for RX, TX, CAN high, CAN low, and the importance of a 120 ohms resistor at each node.
- 🔩 It's crucial to check if the transceiver breakout board has the necessary 120 ohms resistance, as some do not and require manual connection.
- 🔄 The script explains the software setup using STM32 microcontrollers, specifically the Blue Pill and the F446RE, and the use of CubeMX for configuration.
- 📈 The video covers configuring the CAN peripherals, setting the baud rate to 500,000 bits per second, and the distinction between CAN1 as master and CAN2 as slave.
- 🛠️ The tutorial includes programming steps涉及for transmitting data, such as setting up an interrupt for the RX0, using FIFO for message storage, and handling notifications for data pending in the RX FIFO.
- 💡 The purpose of the project is outlined: the F446RE sends data bytes that control the delay and blink count of an LED on the Blue Pill, which is then mirrored on the F446RE after reception.
- 🔄 The script details the process of sending and receiving messages, including the use of callback functions for handling transmission and reception of data.
- 🛡️ The importance of filter configuration for message passing is emphasized, with specific settings demonstrated for allowing certain IDs through the filter banks.
- 🔧 The video provides practical advice for avoiding confusion with SD card links when connecting multiple controllers to the same computer.
- 🔧 The tutorial concludes with a demonstration of the working system, showing the LED blinking according to the received CAN messages and the data flow captured by an analyzer.
Q & A
What is the purpose of this tutorial video?
-The purpose of this tutorial video is to demonstrate how to communicate between two CAN (Controller Area Network) devices using microcontrollers.
What should viewers do before watching this video?
-Viewers should watch the previous video on the basics of the CAN protocol before watching this one, as it provides necessary foundational knowledge.
What microcontrollers are used in this tutorial?
-The tutorial uses the Blue Pill and the F446RE microcontrollers for the demonstration.
What is the significance of the 120 ohms resistance in the CAN connection?
-The 120 ohms resistance is crucial in the CAN connection as it is connected at each node to ensure proper termination and signal integrity.
How are the CAN high and CAN low wires connected in the setup?
-The CAN high and CAN low wires are connected with each other using a twisted pair to minimize electromagnetic interference.
What is the board rate set to in the CAN configuration?
-The board rate is set to 500,000 bits per second in the CAN configuration.
Which CAN peripheral is configured as the master and which as the slave in the video?
-CAN1 is configured as the master CAN, and CAN2 is configured as the slave CAN in the video.
What is the purpose of the onboard LED and user button in the project setup?
-The onboard LED is used for visual feedback, and the user button is used to trigger the sending of CAN messages when pressed.
How many data bytes can be sent in a single CAN message according to the video?
-Up to 8 data bytes can be sent in a single CAN message.
What is the role of filters in the CAN communication setup shown in the video?
-Filters allow message filtering at the hardware level, reducing the CPU workload by only allowing specific messages to be processed.
How can the communication process be automated without manual button presses?
-The process can be automated by sending data after the LED blinking is finished, as demonstrated with the Blue Pill setup, allowing continuous communication without manual intervention.
Outlines
🔌 Setting Up CAN Communication with Microcontrollers
This paragraph introduces the tutorial on establishing CAN (Controller Area Network) communication between two devices, using the Blue Pill and the F446RE microcontrollers. It emphasizes the importance of watching the previous video for understanding the basics of the CAN protocol. The connection setup is detailed, including the use of transceivers, the necessity of 120-ohm resistance at each node, and the correct wiring for CAN high and low. The video also mentions checking the presence of the resistance on the breakout board and setting up the microcontroller pins for communication.
🛠 Configuring CAN Peripherals and Project Setup
The second paragraph focuses on configuring the CAN peripherals using STM32CubeMX for the F446RE microcontroller. It explains the selection of an external crystal for the clock, setting the system to run at 180 MHz, and configuring CAN1 as the master CAN with a baud rate of 500,000 bits per second. The tutorial also covers setting the operating mode to normal, using FIFO zero for storing incoming messages, and enabling the RX0 interrupt in the NVIC. Additionally, it describes setting up the onboard LED and external interrupt for a user button, and generating the project with the necessary configurations.
📶 CAN Data Transmission and Reception Process
This paragraph delves into the process of data transmission and reception in CAN communication. It describes how the F446RE will send two data bytes, one for delay and the other for the number of LED blinks on the second board. The transmission is handled in the XD callback function, triggered by a user button press. Upon receiving a message, the F446RE processes it, and if the data length is two bytes, it sets a flag to blink the LED accordingly. The paragraph also covers configuring CAN filters to allow specific IDs to pass through and storing incoming messages in FIFO zero.
🔄 Demonstrating CAN Communication with Microcontrollers
The final paragraph demonstrates the practical implementation of the CAN communication setup. It shows the creation of another project for the Blue Pill, configuring it similarly to the F446RE but using FIFO one for message reception. The tutorial explains copying and adapting the code from the F446RE project, including the transmit ID and the filter configuration. It also discusses assigning the correct SD links to each microcontroller to avoid confusion and concludes with a live demonstration of the LED blinking according to the CAN messages exchanged between the microcontrollers, showcasing the successful implementation of CAN communication.
Mindmap
Keywords
💡CAN Protocol
💡Transceiver
💡120 Ohms Resistance
💡Twisted Pair
💡CubeMX
💡FIFO (First In, First Out)
💡NVIC (Nested Vectored Interrupt Controller)
💡CAN Filter
💡SD Link
💡Data Bytes
💡CAN Analyzer
Highlights
Introduction to part 2 of the CAN protocol tutorial focusing on communication between two CAN devices.
Prerequisite knowledge of the basics of the CAN protocol from a previous video.
Hardware setup using the Blue Pill and F446RE controllers with specific pin connections.
Explanation of the importance of 120 ohms resistance at each CAN node.
Demonstration of twisted pair wiring for CAN high and low connections.
Powering the transceivers with 5 volts from the microcontrollers.
Configuration of the CAN1 as the master and CAN2 as the slave in CubeMX.
Setting the baud rate to 500,000 bits per second for CAN communication.
Enabling the RX0 interrupt and configuring the onboard LED and user button in the Nucleo project.
Programming the transmit and receive data arrays and variables for the TX mailbox.
Explanation of the data transmission process and the role of the DLC, Standard ID, and the ID of the sender.
Implementation of the XD callback function for data transmission upon user button press.
Reception of CAN messages and the use of a message pending callback function.
Filter configuration within the CAN initialization function for message filtering.
Assignment of SD links to their respective controllers to avoid confusion.
Creation of a project for the Blue Pill with similar settings and code structure.
Demonstration of the communication process with LED blinking according to received data.
Use of an analyzer to observe the data flow and message structure in real-time.
Adjustment of data bytes to change the LED blinking behavior and observation of the effects.
Continuous communication process without manual intervention by sending data after blinking completion.
Conclusion summarizing the use of CAN for communication, benefits of filters, and availability of code download.
Transcripts
hello
everyone welcome to controllers tech
this is part 2 of the tutorial on can
peripheral and today we will see how to
communicate between
two can devices i have already covered
the basics of the can protocol
and you must watch that video before
this one
you can see the video on the top right
so let's start with this video now
i am using the blue pill and the f446re
controllers
and the connection is as shown here the
rx and the tx pins from the transceivers
are connected to the respective
microcontrollers
then can high and can low are connected
with each others
note that there must be a 120 ohms
resistance connected at each node
just as it is shown in figure some
transceivers have this resistance on the
breakout board itself
and other don't so make sure you check
the resistance
here is how the connection is i have
twisted the wire pair connected between
can high and can low
you could try with simple wires but if
doesn't work
try twisted pair here is the 120 ohms
resistance connected between the can
high
and low i have it connected on the other
node too
p11 and p12 from blue pill are connected
to the first transceiver
and the same pins from nucleo are
connected to the second transceiver
the transceivers are powered with 5
volts from the microcontrollers
this is all about the connection now
let's start the project for the nucleo
first
so here we are in the cube mx
first of all i am selecting the external
crystal for the clock
i have eight megahertz crystal and i
want the system to run at maximum 180
megahertz
notice here that the four four six f2
can peripherals
as mentioned in the reference manual the
can one is the master can
and the can 2 is the slave can
also they have 28 filter banks all
together
let's configure the can one
now here i am going to set the board
rate to 500
000 bits per second
i am just modifying these parameters to
get that
here i got the board rate
now make sure that the operating mode is
set to normal
and i am going to use fifo zero to store
the incoming message
so in the nvic i am enabling the rx0
interrupt
this is it for can setup now i am
setting the pa5 as output for the
onboard led
also i am selecting the external
interrupt for the user button
which is connected to the pc13
this is it now click save to generate
the project
i hope you remember things from previous
video because i am going to skip some
explanation here
let's write the program now this is
where the transmit header data will be
stored
rx header will store the header from the
incoming message
let's create the arrays to store the tx
data
and the rx data
and at last the variable for the tx
mailbox
now in the main function start the cam
activate the notification for the data
pending in the rx fifo
everything i am doing has already been
covered in the previous video
kindly watch it if you don't understand
something here
after activating the notification we
will load data in the tx header
i am going to send 2 data bytes so dlc
is 2.
we will be using standard id and the id
will be 0 cross
446 this is the id of f446re
whenever this controller will send the
message this id
will act as the id of the sender now
before i send data
let me explain what i actually want to
achieve here
this controller will send the two data
bytes
the first data byte will act as delay
for the led on the second
board and the second data byte will act
as the number of times
that led will blink when the second
controller receives this message
the led on it will blink according to
these instructions
i am not going to send the data here but
instead let's send it in the xd callback
function
when the user button is pressed this
function will be called
and we will send the data here
the delay will be 100 milliseconds
and the loop will be repeated 10 times
now we will send the data using hull can
add tx message
here we have activated the notification
for the receive
when this controller will receive
message from the blue pill
a message pending callback will be
called
inside this callback we will receive the
data from the fifo
zero the header will be stored in rx
header
and the data will be stored in the rx
data
let's create a flag
we will just do one more check if the
data length is two bytes
then the flag will set
if the flag is set we will blink the led
the number of times it will blink will
be the second byte of the rx data
and the time delay will be the first
byte
and finally reset the flag so that there
is no false blink
let's build it once
i think i forgot to enable the interrupt
for the xd line
let's enable it
next we have to configure the filters
we will configure inside the can
initialization function
here is the filter configuration that i
am
using enable the can filter
i am assigning 20 filter banks to the
can one
out of these 20 i am using filter bank
number 18.
this is the id that should pass through
this id will be assigned to the blue
pill later
and the same value in the mask id
register also
i am using fifo 0 to store the incoming
message
this configuration is explained in the
previous video
so make sure you watch it let's build it
now
now since i am going to connect both the
controllers to the same computer
i don't want the sd links to mix up so i
am going to assign the respective sd
links to their controllers
right now i have connected only the f446
and if i scan the sd link here it will
only show one
so we will keep this sd link assigned to
this board
that's all for this now let's create
another project for blue pill
i am doing the basic setup and running
the system at maximum clock
activate the can peripheral we have to
select the same board rate here
that is 500 000 bits per second
here i will use the fifo 1 for receiving
the message
this is just to demonstrate pc 13 is the
led on board
so select it as output that's it
click save to generate the project
i am going to use same code that i used
in 446
so copy everything from here
we are not using the external interrupt
in blue pill
let's copy this part now
here we don't have can one or can two
instead it's just
can as i said i am using
fifo one here so replace all fifo zero
with fifo one
the transmit id for blue pill will be
zero cross one
zero three
now let's copy the while loop
here the led is pc13
now since we are not using the external
interrupt here
we will send the data in this loop
itself
let's define the data that we will send
and after the led is finished blinking
we will send the data using
hollard tx message
now copy the filter configuration
here the start slave bank does not
matter
like i explained in the previous video
and the filter bank should be between 0
to 13.
the id that will be allowed to pass is 0
cross 4 446
the id of the f4 controller
ok this should be can and we are using
fifo one
all right it's correct now so i have
removed the f4 sd link
and now only the blue pill is connected
if we scan the sd link only one shows up
and we will assign it to the blue pill
now the code has been loaded into both
the microcontrollers
let's see if it works as expected
i have also connected the analyzer to
see the data flow
channel 0 is connected to the tx of 446
and channel 1 is connected to the tx of
blue pill
pay attention to the leds on both the
microcontrollers
the communication will start only when
the button on the 446 is pressed
as you can see when i press the button
the led on blue pill blinks for some
time
and then the led on 446 starts blinking
and then everything stops until the
button is pressed again
this is exactly what we programmed it to
do
let's see the data this is the message
sent by
four four six you can see the id
the control field the two data bytes
once this message is received by the
blue pill the led will blink
and once the led finished blinking the
blue pill will send the message
and here you can see the message sent by
blue pill
you can see the same process will repeat
every time we press the button
let's try changing the data for both of
them
you can see the led is blinking somewhat
longer now
also observe the message on the analyzer
there is almost a delay of four seconds
here
we are sending the 100 milliseconds
delay and 40 times blink to the blue
pill
and that's why this 4 seconds so this
works pretty good
now if you don't want to press the
button every time you can just send this
data
after the blinking is finished just like
we are doing in the blue pill
this way the process will keep going
forever without any manual intervention
let's run it now
i will start the process by pressing the
button once
you can see the led on blue pill is
blinking
and now the led on the 446
and this process is keep repeating by
itself
this is how we can use the can for the
communication
we can send up to 8 data bytes at a time
filters allow the message filtering at
the hardware level itself
so the cpu don't have to do that work
this is it for this video i hope you
guys understood the can protocol
and how to use it for communication
filter configuration is covered in the
previous video
so watch it if you haven't watched it
yet
you can download the code from the link
in the description
keep watching and have a nice day ahead
Посмотреть больше похожих видео
5.0 / 5 (0 votes)