STM32 CAN Communication || NORMAL Mode

ControllersTech
21 May 202120:00

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

00:00

🔌 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.

05:11

🛠 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.

10:13

📶 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.

15:14

🔄 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

The CAN (Controller Area Network) Protocol is a communication protocol designed for reliable real-time communication in vehicle and industrial environments. It allows microcontrollers and devices to communicate with each other using a single twisted pair cable. In the video, the CAN protocol is the central theme as the tutorial explains how to set up and use it for communication between two CAN devices.

💡Transceiver

A transceiver is a device that can both transmit and receive signals. In the context of the video, the transceiver is used to interface the CAN devices with the physical CAN bus. The script mentions connecting the RX and TX pins of the transceiver to the respective microcontrollers, which is essential for setting up the CAN communication.

💡120 Ohms Resistance

The 120-ohm resistance is a term used in the video to describe the termination resistors that are necessary on a CAN bus at each end of the network. These resistors help to dampen the signal reflections and maintain signal integrity. The script specifies that this resistance must be connected at each node of the CAN bus.

💡Twisted Pair

A twisted pair refers to a type of wiring in which two conductors are twisted together for the purpose of improving electromagnetic compatibility and reducing crosstalk. In the script, the instructor recommends twisting the wire pair connected between CAN high and CAN low to potentially improve communication reliability.

💡CubeMX

CubeMX is a configuration tool for STM32 microcontrollers that allows users to configure the microcontroller's peripherals and generate code. In the video, CubeMX is used to set up the CAN peripherals, configure the clock, and generate the initial project setup for the STM32F446RE and Blue Pill microcontrollers.

💡FIFO (First In, First Out)

FIFO, standing for First In, First Out, is a method of organizing data in a queue where the first data to be stored is the first to be retrieved. In the context of the video, the CAN peripherals use FIFO to store incoming messages, which is configured in CubeMX and utilized in the program to handle received data.

💡NVIC (Nested Vectored Interrupt Controller)

The NVIC is a part of the ARM Cortex microcontroller architecture that manages and prioritizes interrupts. In the video, the NVIC is configured to enable the RX0 interrupt, which is used to handle incoming messages in the CAN communication setup.

💡CAN Filter

A CAN filter is used to selectively allow certain messages to be received by a CAN device based on their identifiers. In the script, the instructor configures filters to allow only specific messages to pass through to the CAN peripherals, demonstrating how message filtering can be done at the hardware level.

💡SD Link

The SD (Serial Debug) Link is a feature that allows a debugger to connect to a microcontroller for debugging purposes. In the video, the instructor discusses assigning the SD link to the respective controllers to avoid confusion when connecting both controllers to the same computer.

💡Data Bytes

Data bytes refer to the actual payload of a CAN message, which can be up to 8 bytes long. In the video, the script describes how the first data byte is used as a delay for the LED on the second board, and the second data byte determines the number of times the LED will blink, illustrating the use of data bytes in a CAN message.

💡CAN Analyzer

A CAN Analyzer is a tool used to monitor and analyze CAN bus traffic. In the video, the CAN Analyzer is connected to observe the data flow between the two microcontrollers, providing a visual representation of the messages being sent and received.

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

play00:09

hello

play00:09

everyone welcome to controllers tech

play00:14

this is part 2 of the tutorial on can

play00:16

peripheral and today we will see how to

play00:18

communicate between

play00:19

two can devices i have already covered

play00:22

the basics of the can protocol

play00:24

and you must watch that video before

play00:26

this one

play00:28

you can see the video on the top right

play00:31

so let's start with this video now

play00:34

i am using the blue pill and the f446re

play00:38

controllers

play00:39

and the connection is as shown here the

play00:42

rx and the tx pins from the transceivers

play00:45

are connected to the respective

play00:47

microcontrollers

play00:48

then can high and can low are connected

play00:51

with each others

play00:53

note that there must be a 120 ohms

play00:56

resistance connected at each node

play00:58

just as it is shown in figure some

play01:00

transceivers have this resistance on the

play01:03

breakout board itself

play01:04

and other don't so make sure you check

play01:07

the resistance

play01:13

here is how the connection is i have

play01:16

twisted the wire pair connected between

play01:18

can high and can low

play01:20

you could try with simple wires but if

play01:23

doesn't work

play01:24

try twisted pair here is the 120 ohms

play01:28

resistance connected between the can

play01:30

high

play01:31

and low i have it connected on the other

play01:34

node too

play01:35

p11 and p12 from blue pill are connected

play01:38

to the first transceiver

play01:40

and the same pins from nucleo are

play01:42

connected to the second transceiver

play01:45

the transceivers are powered with 5

play01:47

volts from the microcontrollers

play01:50

this is all about the connection now

play01:52

let's start the project for the nucleo

play01:54

first

play01:59

so here we are in the cube mx

play02:03

first of all i am selecting the external

play02:06

crystal for the clock

play02:08

i have eight megahertz crystal and i

play02:10

want the system to run at maximum 180

play02:13

megahertz

play02:18

notice here that the four four six f2

play02:21

can peripherals

play02:24

as mentioned in the reference manual the

play02:27

can one is the master can

play02:29

and the can 2 is the slave can

play02:34

also they have 28 filter banks all

play02:37

together

play02:41

let's configure the can one

play02:45

now here i am going to set the board

play02:47

rate to 500

play02:48

000 bits per second

play02:55

i am just modifying these parameters to

play02:57

get that

play03:06

here i got the board rate

play03:11

now make sure that the operating mode is

play03:13

set to normal

play03:18

and i am going to use fifo zero to store

play03:20

the incoming message

play03:22

so in the nvic i am enabling the rx0

play03:26

interrupt

play03:27

this is it for can setup now i am

play03:30

setting the pa5 as output for the

play03:32

onboard led

play03:35

also i am selecting the external

play03:37

interrupt for the user button

play03:39

which is connected to the pc13

play03:43

this is it now click save to generate

play03:46

the project

play03:52

i hope you remember things from previous

play03:55

video because i am going to skip some

play03:57

explanation here

play03:59

let's write the program now this is

play04:02

where the transmit header data will be

play04:04

stored

play04:08

rx header will store the header from the

play04:11

incoming message

play04:13

let's create the arrays to store the tx

play04:16

data

play04:16

and the rx data

play04:21

and at last the variable for the tx

play04:26

mailbox

play04:29

now in the main function start the cam

play04:37

activate the notification for the data

play04:39

pending in the rx fifo

play04:42

everything i am doing has already been

play04:44

covered in the previous video

play04:46

kindly watch it if you don't understand

play04:48

something here

play05:11

after activating the notification we

play05:13

will load data in the tx header

play05:20

i am going to send 2 data bytes so dlc

play05:24

is 2.

play05:29

we will be using standard id and the id

play05:33

will be 0 cross

play05:35

446 this is the id of f446re

play05:40

whenever this controller will send the

play05:42

message this id

play05:44

will act as the id of the sender now

play05:47

before i send data

play05:48

let me explain what i actually want to

play05:50

achieve here

play05:52

this controller will send the two data

play05:54

bytes

play05:55

the first data byte will act as delay

play05:58

for the led on the second

play05:59

board and the second data byte will act

play06:02

as the number of times

play06:04

that led will blink when the second

play06:07

controller receives this message

play06:09

the led on it will blink according to

play06:11

these instructions

play06:13

i am not going to send the data here but

play06:16

instead let's send it in the xd callback

play06:18

function

play06:20

when the user button is pressed this

play06:22

function will be called

play06:23

and we will send the data here

play06:30

the delay will be 100 milliseconds

play06:34

and the loop will be repeated 10 times

play06:43

now we will send the data using hull can

play06:45

add tx message

play06:54

here we have activated the notification

play06:57

for the receive

play06:58

when this controller will receive

play07:00

message from the blue pill

play07:02

a message pending callback will be

play07:14

called

play07:16

inside this callback we will receive the

play07:19

data from the fifo

play07:20

zero the header will be stored in rx

play07:23

header

play07:24

and the data will be stored in the rx

play07:28

data

play07:34

let's create a flag

play07:38

we will just do one more check if the

play07:40

data length is two bytes

play07:42

then the flag will set

play07:53

if the flag is set we will blink the led

play08:10

the number of times it will blink will

play08:12

be the second byte of the rx data

play08:26

and the time delay will be the first

play08:28

byte

play08:29

and finally reset the flag so that there

play08:32

is no false blink

play08:34

let's build it once

play08:40

i think i forgot to enable the interrupt

play08:42

for the xd line

play08:44

let's enable it

play09:01

next we have to configure the filters

play09:06

we will configure inside the can

play09:08

initialization function

play09:14

here is the filter configuration that i

play09:16

am

play09:17

using enable the can filter

play09:20

i am assigning 20 filter banks to the

play09:22

can one

play09:26

out of these 20 i am using filter bank

play09:29

number 18.

play09:33

this is the id that should pass through

play09:36

this id will be assigned to the blue

play09:39

pill later

play09:40

and the same value in the mask id

play09:43

register also

play09:44

i am using fifo 0 to store the incoming

play09:48

message

play09:49

this configuration is explained in the

play09:51

previous video

play09:53

so make sure you watch it let's build it

play09:56

now

play09:57

now since i am going to connect both the

play09:59

controllers to the same computer

play10:01

i don't want the sd links to mix up so i

play10:04

am going to assign the respective sd

play10:06

links to their controllers

play10:09

right now i have connected only the f446

play10:12

and if i scan the sd link here it will

play10:15

only show one

play10:17

so we will keep this sd link assigned to

play10:19

this board

play10:29

that's all for this now let's create

play10:33

another project for blue pill

play10:44

i am doing the basic setup and running

play10:47

the system at maximum clock

play10:52

activate the can peripheral we have to

play10:55

select the same board rate here

play10:57

that is 500 000 bits per second

play11:01

here i will use the fifo 1 for receiving

play11:04

the message

play11:05

this is just to demonstrate pc 13 is the

play11:09

led on board

play11:10

so select it as output that's it

play11:13

click save to generate the project

play11:19

i am going to use same code that i used

play11:22

in 446

play11:24

so copy everything from here

play11:33

we are not using the external interrupt

play11:35

in blue pill

play11:43

let's copy this part now

play11:57

here we don't have can one or can two

play11:59

instead it's just

play12:01

can as i said i am using

play12:04

fifo one here so replace all fifo zero

play12:08

with fifo one

play12:16

the transmit id for blue pill will be

play12:19

zero cross one

play12:20

zero three

play12:26

now let's copy the while loop

play12:34

here the led is pc13

play12:42

now since we are not using the external

play12:45

interrupt here

play12:46

we will send the data in this loop

play12:48

itself

play12:50

let's define the data that we will send

play13:04

and after the led is finished blinking

play13:07

we will send the data using

play13:09

hollard tx message

play13:15

now copy the filter configuration

play13:32

here the start slave bank does not

play13:34

matter

play13:35

like i explained in the previous video

play13:51

and the filter bank should be between 0

play13:53

to 13.

play13:57

the id that will be allowed to pass is 0

play14:00

cross 4 446

play14:02

the id of the f4 controller

play14:13

ok this should be can and we are using

play14:16

fifo one

play14:28

all right it's correct now so i have

play14:31

removed the f4 sd link

play14:33

and now only the blue pill is connected

play14:39

if we scan the sd link only one shows up

play14:42

and we will assign it to the blue pill

play14:50

now the code has been loaded into both

play14:52

the microcontrollers

play14:54

let's see if it works as expected

play14:58

i have also connected the analyzer to

play15:01

see the data flow

play15:03

channel 0 is connected to the tx of 446

play15:06

and channel 1 is connected to the tx of

play15:09

blue pill

play15:14

pay attention to the leds on both the

play15:16

microcontrollers

play15:18

the communication will start only when

play15:20

the button on the 446 is pressed

play15:23

as you can see when i press the button

play15:26

the led on blue pill blinks for some

play15:28

time

play15:29

and then the led on 446 starts blinking

play15:33

and then everything stops until the

play15:35

button is pressed again

play15:37

this is exactly what we programmed it to

play15:39

do

play15:49

let's see the data this is the message

play15:52

sent by

play15:53

four four six you can see the id

play15:56

the control field the two data bytes

play16:00

once this message is received by the

play16:02

blue pill the led will blink

play16:04

and once the led finished blinking the

play16:06

blue pill will send the message

play16:09

and here you can see the message sent by

play16:12

blue pill

play16:24

you can see the same process will repeat

play16:26

every time we press the button

play16:33

let's try changing the data for both of

play16:48

them

play17:21

you can see the led is blinking somewhat

play17:23

longer now

play17:39

also observe the message on the analyzer

play17:42

there is almost a delay of four seconds

play17:45

here

play17:46

we are sending the 100 milliseconds

play17:48

delay and 40 times blink to the blue

play17:51

pill

play17:51

and that's why this 4 seconds so this

play17:55

works pretty good

play18:06

now if you don't want to press the

play18:08

button every time you can just send this

play18:10

data

play18:11

after the blinking is finished just like

play18:14

we are doing in the blue pill

play18:16

this way the process will keep going

play18:18

forever without any manual intervention

play18:25

let's run it now

play18:36

i will start the process by pressing the

play18:39

button once

play18:41

you can see the led on blue pill is

play18:43

blinking

play18:45

and now the led on the 446

play18:52

and this process is keep repeating by

play18:55

itself

play18:59

this is how we can use the can for the

play19:01

communication

play19:03

we can send up to 8 data bytes at a time

play19:09

filters allow the message filtering at

play19:11

the hardware level itself

play19:13

so the cpu don't have to do that work

play19:18

this is it for this video i hope you

play19:21

guys understood the can protocol

play19:23

and how to use it for communication

play19:26

filter configuration is covered in the

play19:28

previous video

play19:29

so watch it if you haven't watched it

play19:31

yet

play19:32

you can download the code from the link

play19:34

in the description

play19:36

keep watching and have a nice day ahead

Rate This

5.0 / 5 (0 votes)

相关标签
CAN ProtocolMicrocontrollerCommunicationBlue PillF446RETransceiverBoard RateInterruptsFilter BanksLED Blinking
您是否需要英文摘要?