0:09
everyone welcome to controllers tech
0:14
this is part 2 of the tutorial on can
0:16
peripheral and today we will see how to
0:19
two can devices i have already covered
0:22
the basics of the can protocol
0:24
and you must watch that video before
0:28
you can see the video on the top right
0:31
so let's start with this video now
0:34
i am using the blue pill and the f446re
0:39
and the connection is as shown here the
0:42
rx and the tx pins from the transceivers
0:45
are connected to the respective
0:48
then can high and can low are connected
0:53
note that there must be a 120 ohms
0:56
resistance connected at each node
0:58
just as it is shown in figure some
1:00
transceivers have this resistance on the
1:03
breakout board itself
1:04
and other don't so make sure you check
1:13
here is how the connection is i have
1:16
twisted the wire pair connected between
1:20
you could try with simple wires but if
1:24
try twisted pair here is the 120 ohms
1:28
resistance connected between the can
1:31
and low i have it connected on the other
1:35
p11 and p12 from blue pill are connected
1:38
to the first transceiver
1:40
and the same pins from nucleo are
1:42
connected to the second transceiver
1:45
the transceivers are powered with 5
1:47
volts from the microcontrollers
1:50
this is all about the connection now
1:52
let's start the project for the nucleo
1:59
so here we are in the cube mx
2:03
first of all i am selecting the external
2:06
crystal for the clock
2:08
i have eight megahertz crystal and i
2:10
want the system to run at maximum 180
2:18
notice here that the four four six f2
2:24
as mentioned in the reference manual the
2:27
can one is the master can
2:29
and the can 2 is the slave can
2:34
also they have 28 filter banks all
2:41
let's configure the can one
2:45
now here i am going to set the board
2:55
i am just modifying these parameters to
3:06
here i got the board rate
3:11
now make sure that the operating mode is
3:18
and i am going to use fifo zero to store
3:22
so in the nvic i am enabling the rx0
3:27
this is it for can setup now i am
3:30
setting the pa5 as output for the
3:35
also i am selecting the external
3:37
interrupt for the user button
3:39
which is connected to the pc13
3:43
this is it now click save to generate
3:52
i hope you remember things from previous
3:55
video because i am going to skip some
3:59
let's write the program now this is
4:02
where the transmit header data will be
4:08
rx header will store the header from the
4:13
let's create the arrays to store the tx
4:21
and at last the variable for the tx
4:29
now in the main function start the cam
4:37
activate the notification for the data
4:39
pending in the rx fifo
4:42
everything i am doing has already been
4:44
covered in the previous video
4:46
kindly watch it if you don't understand
5:11
after activating the notification we
5:13
will load data in the tx header
5:20
i am going to send 2 data bytes so dlc
5:29
we will be using standard id and the id
5:35
446 this is the id of f446re
5:40
whenever this controller will send the
5:44
will act as the id of the sender now
5:48
let me explain what i actually want to
5:52
this controller will send the two data
5:55
the first data byte will act as delay
5:58
for the led on the second
5:59
board and the second data byte will act
6:02
as the number of times
6:04
that led will blink when the second
6:07
controller receives this message
6:09
the led on it will blink according to
6:13
i am not going to send the data here but
6:16
instead let's send it in the xd callback
6:20
when the user button is pressed this
6:22
function will be called
6:23
and we will send the data here
6:30
the delay will be 100 milliseconds
6:34
and the loop will be repeated 10 times
6:43
now we will send the data using hull can
6:54
here we have activated the notification
6:58
when this controller will receive
7:00
message from the blue pill
7:02
a message pending callback will be
7:16
inside this callback we will receive the
7:20
zero the header will be stored in rx
7:24
and the data will be stored in the rx
7:38
we will just do one more check if the
7:40
data length is two bytes
7:42
then the flag will set
7:53
if the flag is set we will blink the led
8:10
the number of times it will blink will
8:12
be the second byte of the rx data
8:26
and the time delay will be the first
8:29
and finally reset the flag so that there
8:40
i think i forgot to enable the interrupt
9:01
next we have to configure the filters
9:06
we will configure inside the can
9:08
initialization function
9:14
here is the filter configuration that i
9:17
using enable the can filter
9:20
i am assigning 20 filter banks to the
9:26
out of these 20 i am using filter bank
9:33
this is the id that should pass through
9:36
this id will be assigned to the blue
9:40
and the same value in the mask id
9:44
i am using fifo 0 to store the incoming
9:49
this configuration is explained in the
9:53
so make sure you watch it let's build it
9:57
now since i am going to connect both the
9:59
controllers to the same computer
10:01
i don't want the sd links to mix up so i
10:04
am going to assign the respective sd
10:06
links to their controllers
10:09
right now i have connected only the f446
10:12
and if i scan the sd link here it will
10:17
so we will keep this sd link assigned to
10:29
that's all for this now let's create
10:33
another project for blue pill
10:44
i am doing the basic setup and running
10:47
the system at maximum clock
10:52
activate the can peripheral we have to
10:55
select the same board rate here
10:57
that is 500 000 bits per second
11:01
here i will use the fifo 1 for receiving
11:05
this is just to demonstrate pc 13 is the
11:10
so select it as output that's it
11:13
click save to generate the project
11:19
i am going to use same code that i used
11:24
so copy everything from here
11:33
we are not using the external interrupt
11:43
let's copy this part now
11:57
here we don't have can one or can two
12:01
can as i said i am using
12:04
fifo one here so replace all fifo zero
12:16
the transmit id for blue pill will be
12:26
now let's copy the while loop
12:34
here the led is pc13
12:42
now since we are not using the external
12:46
we will send the data in this loop
12:50
let's define the data that we will send
13:04
and after the led is finished blinking
13:07
we will send the data using
13:15
now copy the filter configuration
13:32
here the start slave bank does not
13:35
like i explained in the previous video
13:51
and the filter bank should be between 0
13:57
the id that will be allowed to pass is 0
14:02
the id of the f4 controller
14:13
ok this should be can and we are using
14:28
all right it's correct now so i have
14:31
removed the f4 sd link
14:33
and now only the blue pill is connected
14:39
if we scan the sd link only one shows up
14:42
and we will assign it to the blue pill
14:50
now the code has been loaded into both
14:52
the microcontrollers
14:54
let's see if it works as expected
14:58
i have also connected the analyzer to
15:03
channel 0 is connected to the tx of 446
15:06
and channel 1 is connected to the tx of
15:14
pay attention to the leds on both the
15:18
the communication will start only when
15:20
the button on the 446 is pressed
15:23
as you can see when i press the button
15:26
the led on blue pill blinks for some
15:29
and then the led on 446 starts blinking
15:33
and then everything stops until the
15:35
button is pressed again
15:37
this is exactly what we programmed it to
15:49
let's see the data this is the message
15:53
four four six you can see the id
15:56
the control field the two data bytes
16:00
once this message is received by the
16:02
blue pill the led will blink
16:04
and once the led finished blinking the
16:06
blue pill will send the message
16:09
and here you can see the message sent by
16:24
you can see the same process will repeat
16:26
every time we press the button
16:33
let's try changing the data for both of
17:21
you can see the led is blinking somewhat
17:39
also observe the message on the analyzer
17:42
there is almost a delay of four seconds
17:46
we are sending the 100 milliseconds
17:48
delay and 40 times blink to the blue
17:51
and that's why this 4 seconds so this
18:06
now if you don't want to press the
18:08
button every time you can just send this
18:11
after the blinking is finished just like
18:14
we are doing in the blue pill
18:16
this way the process will keep going
18:18
forever without any manual intervention
18:36
i will start the process by pressing the
18:41
you can see the led on blue pill is
18:45
and now the led on the 446
18:52
and this process is keep repeating by
18:59
this is how we can use the can for the
19:03
we can send up to 8 data bytes at a time
19:09
filters allow the message filtering at
19:11
the hardware level itself
19:13
so the cpu don't have to do that work
19:18
this is it for this video i hope you
19:21
guys understood the can protocol
19:23
and how to use it for communication
19:26
filter configuration is covered in the
19:29
so watch it if you haven't watched it
19:32
you can download the code from the link
19:36
keep watching and have a nice day ahead