0:09
hello and welcome to controllers Tech
0:12
this is the seventh video in the SDM 32
0:15
uart series and today we will continue
0:18
using the half duplex mode in the
0:21
previous video we saw how to communicate
0:23
between two SDM 32 MCU in half duplex
0:26
mode using a single wire today we will
0:30
extend this functionality to interface a
0:32
sensor that communicates using a single
0:35
wire yes I am talking about the famous
0:38
ds18 the 20 temperature sensor this
0:42
sensor uses the one wire protocol to
0:44
communicate with the MCU and since the
0:46
uart has the capability to handle the
0:48
communication using a single wire we
0:51
will use uart to interface this
0:53
sensor I have made a few videos in the
0:56
past covering this ds18 v20 sensor where
0:59
in in one of the videos we used the Poo
1:02
with microsc timing the other video did
1:05
Cover the sensor with u protocol but it
1:08
was rather how to make it work instead
1:12
works today in this video I will cover
1:15
everything in depth so you can
1:17
understand how it actually works at the
1:18
sensor level we will analyze the timings
1:22
on the logic analyzer and we will also
1:24
see how the sensor will be able to read
1:26
these bites as a single bit here I have
1:29
have the data sheet of the ds18 B20
1:33
sensor let's scroll down to the bit
1:36
timings here you can see the timing for
1:39
the zero slot to write a bit zero the
1:43
master needs to pull the line low for
1:48
microc and to write a bit one the master
1:51
needs to pull the line low for around 1
1:54
microc and then release it the pull-up
1:58
resistor will then pull the line High
2:01
the important thing here is that the
2:03
ds18 the2 will only sample 15 microsc
2:06
after the master pulled the line low so
2:10
to write a one we don't exactly need to
2:12
pull the line for 1 microsc we can
2:15
release it anytime within this 15 microc
2:18
frame now if we use the uat peripheral
2:21
with the board rate of
2:23
115200 each bit takes around 8.68 micros
2:28
time frame also we will use the standard
2:31
eight data bits so along with the start
2:34
bit we have a total of nine
2:36
bits these nine bits will take around 78
2:41
seconds this will be the time taken by
2:43
one data bite with the standard set up
2:46
of 115200 board and eight data bits with
2:50
parity the 78 microsc is within the
2:53
limits of the time requirement for the
2:57
bits we will understand it better on the
3:01
analyzer here is the ds18 v20 sensor
3:05
that I am going to use in today's
3:07
tutorial it has three wires the red is
3:11
for VCC black is the ground and the
3:14
yellow is the data wire I am not
3:17
connecting it for now but I have
3:19
connected the logic analyzer to the uart
3:23
board I have already created a project
3:26
on the cube IDE the U parameters are
3:29
conf configured with the board rate of
3:31
115200 eight data bits with one stop bit
3:35
and no parity the pin P9 is the uart TX
3:39
pin and is the only pin available in the
3:42
half duplex mode I have enabled the uart
3:45
global interrupt and also set up the dma
3:48
for both TX and RX the dma is used in
3:52
the normal mode and the data width is
3:55
set to bite the dma interrupts are
3:58
enabled automatically when you enable
4:00
the dma that is all the configuration we
4:04
need now we will see the uart B timings
4:11
analyzer let's Define a variable data
4:14
and store the value zero in it now we
4:17
will send this variable to the uart
4:20
basically we are sending the BTE zero so
4:23
that we can measure its time frame let's
4:26
build and Flash it to the
4:33
here you can see the data on the logic
4:36
analyzer we can see the bite zero and
4:39
the total time for which the line is low
4:43
microc this is the time taken by the
4:46
nine bits which contains one start bit
4:49
and eight data bits the start bit is the
4:52
first low bit and then we have eight
4:56
zeros this whole Arrangement satisfies
4:58
the criteria I for writing a zero slot
5:01
to the sensor in other words if the MCU
5:04
wants to send a zero bit to the sensor
5:07
we will send the bite zero now let's
5:10
check the timing for the bite
5:18
FFX here you can see the start bit which
5:21
pulls the line low for around 13 micros
5:25
and then we have all the ones we know
5:28
that ds18 20 samples the data 15 micros
5:31
seconds after detecting the first
5:33
transition and the line is always high
5:35
in that time frame so the ds18 B20 will
5:40
read this as a one actually the start
5:43
bit time is even smaller when we connect
5:45
an actual device on the other end so to
5:49
send a bit one to the sensor we will
5:53
FFX we now have an idea about how to
5:56
write a zero or a one to the sensor
5:59
let's see the reset timing now to reset
6:03
the sensor we need to pull the line low
6:06
for more than 480 micros and then
6:09
release it the problem we have is that
6:12
the uart with the board rate of
6:14
115200 won't be able to do this since
6:18
the total time taken by a bite is even
6:20
less than 80 micros we can't generate a
6:23
reset with this board rate so here we
6:27
will use the board rate of 9,000
6:30
600 the good thing about the whole
6:32
library is that we can simply change the
6:35
board rate during the uart
6:37
initialization and it will work each bit
6:42
9,600 takes around 104
6:46
micros we already have the low start bit
6:49
and if we send another four bits as low
6:52
we will have a total low time of 520
6:56
microc this satisfies the reset crit
6:59
criteria where we need to pull the line
7:04
microc we will again test it with the
7:09
first let's change the data to F0
7:13
hex the least significant bit is sent
7:16
first in the uart therefore the zero
7:18
will be sent first here let's build and
7:21
Flash it to the board there is an error
7:24
here and that is because the analyzer is
7:34
9,600 now you can see the bite F0 hex
7:39
here you can see the pulse is low for
7:43
microseconds basically after the start
7:46
bit we have four zeros which keeps the
7:49
line low for this much time let me add
7:52
the one wire analyzer to this channel
7:55
the one wire protocol can see this as
7:57
the reset condition so we are are good
7:59
here now let me connect the actual
8:02
sensor and we will see how it responds
8:05
to this reset condition here I have
8:08
connected the sensor to the MCU using
8:11
breadboard the green wire is connected
8:13
between the signal pin of the sensor and
8:16
the uart TX pin the orange wire connects
8:19
the sensor's VCC pin to the 5 volts of
8:22
the MCU and the gray wire connects the
8:25
ground to the ground I have connected a
8:27
4.7 kilo ohms resistor between the VCC
8:31
and the ground pin this will act as the
8:34
pull-up resistor the logic analyzer is
8:37
still connected to the TX pin so that we
8:40
can see the sensor's response all right
8:43
let's check the analyzer
8:46
again here you can see the pulse on the
8:50
analyzer the data is now c0 hex instead
8:54
of the F0 hex this is because this time
8:58
the sensor pulled the line low the one
9:01
wire protocol shows there is a reset
9:03
condition and it also detects the
9:05
presence of the sensor actually we sent
9:08
the bite F0 hex for the reset sequence
9:12
since the least significant bit is sent
9:14
first the MCU pulled the line low for
9:17
around 520 microsc and then released it
9:22
as soon as the line was released the
9:24
sensor pulled it back to low for
9:30
everything happened as per the
9:31
instruction given in the sensors data
9:33
sheet one thing to note here is that the
9:36
bite F0 hex was not transmitted fully
9:40
the sensor does not know what we are
9:42
sending it just sees that the line was
9:44
low for more than 480 microsc so it must
9:50
pulse and as soon as it realizes this it
9:53
will show its Presence by pulling the
9:55
line low for some time we will now write
9:58
an initial initialization sequence for
10:01
sensor we need to change the U board
10:04
rate during the runtime so let's define
10:06
a separate function to do so the
10:10
parameter of this function is the board
10:12
rate with which we want to initialize
10:15
UR here we will copy all the
10:17
initialization code and just pass the
10:20
parameter board to the board rate the
10:23
ds18 B20 start function will be used to
10:27
initialize the sensor
10:29
here we will send the reset sequence to
10:32
the sensor and then detect its
10:34
presence so first we need to initialize
10:37
the uart with the board rate of
10:40
9,600 then transmit the data bite F0
10:44
hex the sensor will pull the line low
10:47
while this bite is still transmitting so
10:50
we will have new data in the uart data
10:53
register this data bite could be
10:55
anything but F0 hex basically if there
10:59
is a presence pulse the data register
11:02
cannot contain the same data that we
11:04
sent we will receive one bite of data
11:07
via the uart and copy it in the data
11:10
variable now the rest of the uart
11:12
operations will take place at 115200
11:15
board rate so let's initialize the uart
11:18
again finally we will check if the
11:21
received data is the same as the
11:23
transmitted data if it is the same that
11:26
means no presence pulse was detected and
11:29
we will return an error otherwise return
11:33
success let's define a presence
11:39
variable inside the while loop we will
11:42
call the ds18 B20 start and store its
11:45
value in the presence variable Let's
11:50
first here I am adding the presence
11:52
variable to the live expression let's
11:55
run the debugger you can see the
11:58
presence value is one this means the
12:02
successfully we will now proceed with
12:05
the rest of the code now we will write
12:08
the data to the sensor the ds18 B20 WR
12:12
function takes the data bite to be
12:14
written as the parameter I have already
12:16
shown how to write a bit zero and bit
12:19
one to the sensor basically to write a
12:21
single bit we need to send an entire
12:24
bite and this is why I am defining a
12:26
buffer to hold eight bytes now we will
12:29
extract a single bit from the data bite
12:33
if this bit is a one we will store FFX
12:36
to that position in the buffer and if
12:39
the bit is a zero we will store zero in
12:41
the buffer this way we will prepare a
12:44
buffer with 8 bytes for a single data
12:47
byte now we will send the buffer to the
12:50
uart Let's test the right function
12:57
forward here we have the examples in the
13:01
data sheet which covers how to get data
13:03
from this sensor basically we send the
13:06
reset pulse and the sensor responds with
13:09
the presence pulse since I am only
13:12
connecting a single sensor to this pin I
13:15
will issue the skip ROM command after
13:18
that the convert T command will be used
13:20
to convert the temperature so let's
13:23
issue the skip ROM command first and
13:26
issue the convert T command let's build
13:29
and Flash the code to the board we will
13:32
observe these commands on the logic
13:39
analyzer you see this framing error
13:42
because the analyzer is configured with
13:46
9,600 let me change it to
13:49
115200 now you can see each datab by
13:52
sent by the MCU the one wire analyzer
13:56
can analyze this data you can clearly
13:59
see the skip ROM command CC hex the 44
14:04
hex is being recognized as the datab
14:06
bite by the analyzer so the MCU can
14:09
write the data properly we will now
14:12
proceed with the reading the ds18 B20
14:16
read function will read the data bytes
14:18
sent by the sensor this here is the
14:21
sequence for the master to read the Bit
14:23
Zero or bit one the master needs to pull
14:27
the line low for more than one micros
14:30
and then release the line if the sensor
14:33
pulls the line low again the bit is a
14:35
zero and if the sensor does not pull the
14:38
line low the line will be high because
14:40
of the pull-up resistor and this denotes
14:43
the bit as a one note here that the
14:45
master is the one pulling the line low
14:48
initially we can assume this as the
14:51
start bit of our data basically we need
14:54
to send the bite FF hex this will keep
14:57
the line low for only the per period of
14:59
the start bit and the line will be high
15:01
for the rest of the time as soon as the
15:04
line is released after the start bit the
15:07
sensor will either pull it low to
15:08
transmit Bit Zero or it will leave it
15:10
high to transmit bit one since we want
15:14
to read an entire bite from the sensor
15:16
we need to send 8 bytes of the data
15:19
let's define a buffer to store 8 bytes
15:22
to be sent the variable value will
15:25
contain the bite received from the
15:27
sensor now we will store the data to be
15:31
sensor we will send the data bite ffex
15:35
each time so that the line is pulled low
15:37
only for a period of the start bit now
15:41
we will transmit this data using the
15:43
dma the use of dma here is very
15:47
important the data using the dma is
15:50
transferred in the background so for
15:52
each bite transferred we can read the
15:55
sensor's response to that bite if we
15:58
send the data in the blocking mode all
16:00
the 8 bytes will be transferred before
16:02
the control can reach the reading
16:05
function so we will only be able to read
16:07
the sensor's response to the last by
16:10
sent and the first seven bytes will be
16:12
lost once all the 8 bytes have been read
16:15
by the dma the RX complete call back
16:19
called inside the call back we will
16:22
simply set the variable is received to
16:25
one this will help us identify if all
16:28
the responses have been received once we
16:31
have received the sensor's response to
16:33
the data bytes we sent we will start
16:36
extracting the bits from the
16:38
bytes we sent the bite FFX and if the
16:41
sensor did not pull the line low the
16:44
data will be the same and this means
16:47
that the bit must be one we do not care
16:50
what data we received or we care is if
16:53
it is different from
16:55
FFX if the data is different that means
16:58
means the sensor must have pulled the
17:00
line low and the bit is a zero we have
17:03
defined the value with an initial value
17:06
of Z so the Bit Zero is automatically
17:10
adjusted after extracting the bits we
17:13
will reset the variable to zero so that
17:15
this Loop does not run again then return
17:18
the value these are the only functions
17:21
we need to interface the sensor with the
17:24
MCU let's see the initialization pattern
17:29
so we send the reset command and the
17:31
sensor responds with the presence pulse
17:35
then we issue the skip ROM command
17:37
followed by the convert T command then
17:40
send the reset command again and the
17:42
sensor will respond with the presence
17:45
pulse the master will isue the skip ROM
17:48
command followed by the read scratchpad
17:51
command then we will read data bytes
17:59
we will read two bites of data so let's
18:04
variables we will read the least
18:06
significant bite first and then read the
18:09
most significant bite let's build and
18:12
test this part first we will convert it
18:15
to temperature in a while let's add all
18:18
the variables to the live expression
18:21
here you can see we have the data in the
18:23
least and most significant bytes here in
18:27
the RX data butt we either have the bite
18:30
FF or f8 this FF hex represents bit one
18:35
and the f8 hex represents Bit Zero let's
18:43
analyzer here you can see the entire
18:46
communication frame we send the reset
18:49
sequence first followed by the skip ROM
18:53
commands then send the reset sequence
18:56
again followed by the skip ROM and the
18:58
read scratch Pad then the sensor sends
19:02
the data the Bit Zero is represented by
19:05
the bite f8 hex where the sensor pulled
19:08
the line low for a while after detecting
19:10
the start bit the bit one is represented
19:13
by the bite FF hex where the sensor does
19:15
not pull the line low and the line
19:18
remains high for the entire bite now we
19:21
will convert these data bytes to the
19:23
temperature value by default the
19:26
sensor's resolution is 12bit and this is
19:29
how the data is stored in two different
19:31
bytes here letter s represents the sign
19:35
and for negative temperatures this sign
19:37
bit will be one we will combine these
19:40
two bytes and make a single 12 bit
19:45
data let's define a 16bit sign variable
19:49
where we will store the 12bit combined
19:52
data the temperature variable will
19:55
contain the actual temperature to
19:57
combine the data bites shift the most
20:00
significant bite to the left by eight
20:02
places and then add the least
20:03
significant bite to it to get the final
20:06
temperature we will divide this value by
20:09
16 since the resolution is 12bit each
20:13
bit represents the temperature of
20:17
0.0625 this is why we are dividing by 16
20:26
temperature let set the variables to the
20:29
live expression and run the code you can
20:33
see the temperature value is now being
20:35
stored in the temperature variable right
20:38
now the temperature is around 18° C here
20:42
I have a cup of cold water and I am
20:45
going to dip the sensor inside it you
20:48
can notice the change in the reading I
20:51
have fast forward this part but make
20:53
sure you give at least 2 seconds delay
20:56
before taking the reading again the
20:58
temperature has dropped to about 1°
21:01
C now I am holding the sensor in my fist
21:05
and you can see the increase in the
21:07
reading everything is working as
21:10
expected the sensor responds to the
21:13
change in temperature pretty well the
21:15
one wire protocol is working fine too
21:19
note that the dd11 sensor also uses the
21:22
one wire protocol but if you notice here
21:25
the bit timings are not constant for
21:28
example the Bit Zero indication means
21:31
that the sensor will pull the line low
21:33
for 50 micros seconds and then pull it
21:39
micros after this the sensor immediately
21:42
sends the next bit by pulling the line
21:44
low again similarly the bit one
21:47
indication is when the sensor will pull
21:49
the line low for 50 micros and then pull
21:53
it high for 70 micros
21:55
seconds the sensor immediately sends the
21:58
the next bit after this and hence we
22:00
have different time periods for Bit Zero
22:02
and bit one this is why the one wire
22:05
protocol using the uart cannot be used
22:07
for these sensors instead we can use the
22:10
regular gpio method to read the
22:13
timing my point is please do not ask me
22:16
to make videos on dhd1 or dd22 using the
22:22
protocol so I hope you understood how to
22:25
use the one wire protocol to interface
22:27
the DS 18 v20 temperature
22:30
sensor you can use this idea to
22:32
interface other devices which use the
22:36
protocol this is it for the
22:39
video you can download the code from the
22:43
description leave comments in case of
22:45
any doubt keep watching and have a nice