0:09
hello and welcome to controllers Tech
0:12
this is the 10th video in the SDM 32
0:15
watt series and part three of the Lin
0:18
protocol in the previous video we saw
0:21
how to connect an actual Lin transceiver
0:23
to the MCU and analyze the data sent by
0:26
the master over the Lin bus we also
0:29
established a connection between two
0:31
transceivers connecting the slave to the
0:33
second transceiver programmed the slave
0:36
MCU and extracted the data from the
0:39
frame in today's tutorial we will use
0:42
the Lin protocol to transfer some
0:44
command from the master to the
0:46
slave the slave will then perform the
0:49
operation based on the received
0:52
command we will also program the master
0:55
to request some data from the slave so
0:57
that the slave shall send the data over
1:00
bus this data will be then received and
1:05
master the connection is still the same
1:08
that I have used in the previous video
1:10
but I have added a few additional things
1:12
to the setup let me explain the
1:15
connection again and I will also show
1:17
what I intend to achieve with this setup
1:21
the nucleo board will act as the master
1:23
and the blue pill will act as the slave
1:26
MCU I have connected four buttons to the
1:30
MCU the buttons are connected to the
1:33
pins PA a0 pa1 pa4 and
1:38
PC2 the other end of the buttons are
1:42
ground this setup will ensure that when
1:45
the button is pressed the respective pin
1:47
will be pulled down to the
1:49
ground I am going to use one more button
1:52
which comes preconnected on the nucleo
1:55
board so we have a total of five buttons
2:00
in the previous video I have already
2:03
explained the setup between the master
2:05
MCU and the transceiver IC the
2:08
connection between the two transceiver
2:09
ic's and the connection between the IC
2:13
slave on the slave MCU I have connected
2:18
LEDs they are connected between the pins
2:23
pb15 the positive terminals of the LEDs
2:26
are connected to these pins while the
2:28
negative terminals are connected to the
2:30
ground with 100 ohms resistance in
2:33
between the four buttons on the master
2:38
LEDs I have also connected a
2:40
potentiometer to the slave
2:42
MCU it is connected to the pin PA Z and
2:46
the MCU will use the ADC to read the
2:49
potentiometer value the fifth button on
2:52
the master will be used to fetch this
2:56
MCU basically when button one is pressed
3:00
it will turn on the LED 1 on the
3:02
slave when button two is pressed the LED
3:06
2 will turn on and other LEDs will turn
3:09
off similarly button 3 and button four
3:13
will turn on the respective LEDs on the
3:16
MCU when the button five is pressed all
3:19
the LEDs will turn off and the slave
3:22
will read the current value of the
3:23
potentiometer and send it to the master
3:26
via the Lin bus we need to reprogram
3:29
both the master and slave as per this
3:32
setup I will simply modify the projects
3:35
created in the previous
3:37
video here are the projects from the
3:40
previous video let's start with the
3:43
master first we need to modify the cube
3:46
MX configuration so as to add the
3:48
buttons to the project let's enable the
3:51
external interrupt for the pins P0 pa1
3:58
PC2 the reason I chose these pins is so
4:01
that we don't get the external interrupt
4:03
conflicts between them let's also enable
4:06
the external interrupt for the pin
4:09
pc13 now go to the gpio tab and enable
4:14
the pullup for all the selected pins go
4:17
to the nvic tab and enable the external
4:21
interrupt for all the pins since the
4:24
master is also going to receive the data
4:26
make sure that the pull-up is enabled
4:28
for the artrx pin that is all the
4:31
configuration we need click save to
4:37
project let's define all the button
4:40
variables and initialize them with the
4:42
value zero we will also Define the
4:45
variable transmit which will be used to
4:48
transmit the data when either of the
4:51
buttons is pressed the interrupt will
4:53
trigger and the XD call back will be
4:56
called here we will check if the
4:58
interrupt is called by the pin
5:01
pa0 if it is then we will set the
5:04
variable B1 and also set the variable
5:07
transmit we will also reset the
5:09
variables B2 B3 B4 and
5:13
B5 else if the interrupt is called by
5:16
the pin PA A1 then we will set the
5:19
variable B2 along with the transmit
5:22
variable we will reset the rest of the
5:25
variables the same process will be
5:27
repeated for the other pins as well
5:35
inside the while loop we will protect
5:37
the transmission part by checking if the
5:39
transmit variable is set the variable is
5:42
set when either of the buttons is
5:44
pressed and only then we want to
5:46
transmit the data let's add a delay of
5:49
300 milliseconds to prevent the button
5:52
debouncing the TX data buffer contains
5:55
the Lin frame that the master will send
5:57
via the Lin bus the initial data in the
6:01
buffer Remains the Same we will just
6:03
modify the actual datab byes the actual
6:07
data bite will contain the current value
6:10
variables this is how the actual data
6:13
bite of the TX data buffer looks like
6:16
the first five bits of this bite
6:18
contains the values of the button
6:20
variables the next bite of this buffer
6:23
will contain the checkum bite now we
6:26
have the entire Lin frame so we will
6:28
send it via the Lin bus we will only set
6:32
the Cs pin when the master is
6:33
transmitting the data so let's remove
6:36
this statement from here and we will
6:38
copy it here give some delay to make
6:41
sure the pin is set before the master
6:43
can start transmitting the data now the
6:46
master will send the Lin brake followed
6:48
by the TX data buffer once the data is
6:52
sent pull the Cs pin low to disable the
6:55
transmitter and finally reset the
6:57
transmit variable so so that this Loop
7:00
does not run again the four buttons
7:03
connected to the master will send the
7:04
data to the slave via the Lin bus but
7:08
when the fifth button is pressed the
7:10
master shall be ready to receive the
7:12
data from the slave the slave will
7:15
transmit the data in this format we will
7:18
have the header the sync field the P ID
7:21
two bytes of the ADC data and finally
7:24
the check sum so we can expect a total
7:27
of six bytes from the slave
7:30
we will call the function Hol you out
7:32
receive to receive six bytes of data in
7:34
the Blocking Mode make sure to not give
7:38
a very small or a very large timeout
7:40
here a smaller timeout might not be able
7:43
to receive data and a large timeout can
7:46
prevent the other functions from
7:48
executing if all six bytes are received
7:51
the function will return Hollow K and we
7:54
can proceed with processing the data we
7:57
will first calculate the check sum of
7:59
the received data the P ID is by two of
8:02
the received data buffer the actual data
8:05
starts from the B three and we have a
8:09
bytes then compare the calculated check
8:12
sum with the received check sum if they
8:15
are the same that means we have received
8:18
valid data we will combine the two data
8:21
bytes to make a single 16bit ADC
8:28
data that is all all we need for the
8:31
MCU in the slave program we call the
8:34
function received to idle in the
8:36
interrupt mode this always keeps the
8:39
slave MCU in the receive mode and an
8:41
interrupt is triggered when the slave
8:43
receives some data from the master we
8:46
cannot use this idea for the master even
8:49
though we want the master to receive the
8:51
data this is because the master might
8:54
receive back the data transmitted by it
8:56
and the ADC readings will fail you can
9:00
give it a try but do use the ID of the
9:02
slave to filter out the received data
9:06
this will prevent the master from using
9:08
its own data to calculate the ADC value
9:11
the master program is complete so build
9:14
and Flash it to the board let's modify
9:17
the slave configuration now we need to
9:20
enable the pins for the LEDs so let's
9:25
configuration the LEDs are connected to
9:30
so let's set the pins as output the
9:33
potentiometer is also connected to the
9:35
slave MCU so let's enable the ADC
9:37
Channel Zero leave the ADC configuration
9:41
to default there seems to be some issue
9:44
with the clock setup modify the ADC
9:48
prescaler to reduce the ADC clock within
9:51
the range all right that's all the
9:53
configuration we need so click save to
9:58
project so we start with receiving the
10:01
data in the idle mode using the
10:04
interrupt when the required data has
10:06
been received an interrupt will trigger
10:08
and the RX event call back is called
10:12
inside the call back we process the
10:14
received data and check its validity if
10:17
the data is valid the variable is data
10:20
valid is set to one now in the while
10:24
loop we check the state of this variable
10:27
and if it is set we will start
10:28
extracting in the actual data bytes from
10:31
the received data the master sends the
10:34
data in the following format the BTE
10:37
zero is the header field followed by the
10:40
sync field and the protected ID then we
10:44
have the actual data at the third
10:45
position followed by the check sum bite
10:48
in the end since we only have five bytes
10:51
of data incoming let's set the function
10:54
to receive five bytes all right now we
10:57
have the data bite so will set the LEDs
11:00
according to the values of the button
11:02
bits the pin pb12 will be controlled by
11:06
the bit B1 similarly the pins pb13 14
11:11
and 15 will be controlled by the bits B2
11:14
B3 and before the bit B5 requests the
11:18
ADC data from the slave so we will check
11:21
if the bit B5 is set if it is we will
11:25
read the potentiometer value start the
11:28
ADC pole for the conversion get the ADC
11:32
value and store it in the ADC valve
11:34
variable finally stop the
11:39
ADC now we will prepare the TX data
11:42
buffer to be sent via the Lin
11:49
bus the first bite will be the sink
11:52
field and then we will have the P
11:57
ID we didn't use the p ID calculator in
12:00
the previous video so let's copy it here
12:04
here I am using the actual ID of the
12:06
slave as 35 hex the third and fourth
12:10
bite of the TX data buffer will contain
12:12
the ADC data the ADC data is 12bit in
12:16
size so we need to split it into two
12:19
bytes with the least significant by sent
12:22
first finally the last bite will be the
12:25
checkum bite before transmitting the
12:28
data we need to to enable the
12:29
transmitter so pull the pin pb1 High
12:33
let's give some delay Now call the
12:36
function Lin send brake followed by the
12:41
transmission finally disable the
12:43
transmitter by pulling the Cs pin low
12:46
now we will reset the variable as data
12:49
valid so that this Loop does not run
12:51
again so that is all for the slave let's
12:54
Flash the code to the
12:58
board there are few Corrections needed
13:01
in the code the first one is here in the
13:04
master the B5 bit must be shifted by
13:07
four places and the second one is in the
13:10
slave the sink bite is 55 hex let's
13:15
flash it again to the slave
13:19
MCU we will monitor the received ADC
13:21
data in the master so let's open the
13:24
debugger for the master
13:31
here I have added the required variables
13:34
to the live expression the ADC data
13:37
variable will show the final ADC value
13:40
sent by the slave let's press the button
13:43
one on the master the LED one on the
13:46
slave is turned on you can see the
13:49
variable B1 is set on the live
13:51
expression on pressing the second button
13:54
the variable B2 is set and B1 is reset
13:58
the the LED 2 on the slave is turned on
14:02
similarly other buttons turn on the
14:05
LEDs so the buttons are working fine and
14:09
the LEDs are responding
14:13
accordingly now let's rotate the
14:15
potentiometer and then press the button
14:17
five the variable be five and the live
14:20
expression is one all the LEDs are off
14:23
but we don't have the data in the ADC
14:25
data variable let's check the RX dat
14:29
array to confirm if the master has
14:31
received the data here you can see the
14:34
data is indeed received by the master
14:37
but instead of one header bite it has
14:40
received two of them and because of that
14:42
the entire data is shifted by one place
14:45
we requested the master to receive only
14:47
six bytes of data but it was based on
14:50
the assumption that there will be only
14:51
one header bite now that there are two
14:54
of them we will receive one extra bite
14:57
from the slave or also shift all the
15:00
offsets by one position let's build and
15:08
again let's press the button five to
15:11
request the data again you can see the
15:14
ADC data variable now has the ADC value
15:18
the slave is sending the raw values so
15:21
here the value will range from0 to
15:25
4,095 now I am rotating the
15:27
potentiometer to the extreme end and
15:29
reading the value here you can see the
15:32
value reported by the
15:34
ADC now I am rotating it to the other
15:37
end and pressing the button again the
15:40
ADC now reports the value zero so we can
15:44
now read the potentiometer value sent by
15:47
the slave the buttons and LEDs are also
15:51
working well let's see this entire
15:54
working on the logic analyzer
16:02
I am pressing all the buttons one by one
16:05
and you can see the data being recorded
16:15
analyzer here is the first Lin frame
16:17
sent by the master the actual data value
16:21
in this Frame is one the data value in
16:24
the next frame is two then the data
16:27
value is 4 hex followed by the value 8
16:30
hex then the button B5 sends the data 1
16:35
hex when the slave receives this data it
16:38
sends the ADC value after around 100
16:42
milliseconds here you can see the slave
16:44
sends the entire linf frame with the
16:47
protected id35 hex followed by the two
16:50
data bytes when the communication is
16:53
both ways we need to make sure that
16:55
there is only one device transmitting
16:59
this is why the master is set to receive
17:01
data in the blocking mode so that there
17:03
is no chance of any conjunction on the
17:05
Lin bus so this is it for the video we
17:09
were able to program the master to send
17:12
the commands to the slave on receiving
17:15
the command the slave performed the
17:17
respective actions by controlling the
17:20
LEDs the slave also sent the data via
17:23
the Lin bus when requested by the master
17:27
this is it for today I hope you
17:29
understood everything explained in the
17:32
video you can download the code from the
17:34
link in the description leave comments
17:38
in case of any doubt keep watching and
17:41
have a nice day ahead