0:09
hello and welcome to controllers Tech as
0:12
you have already seen the title this
0:15
video is the start of a new series on
0:17
the uart peripheral I have covered a lot
0:20
of videos on the uart in the past few
0:22
years but none of them were very
0:24
detailed and also they were very random
0:27
the purpose of the series is to give you
0:29
enough information on the peripheral and
0:31
in an organized manner I will use the
0:35
stm32f4 series controller for this
0:37
series as the U peripheral in it is not
0:40
very complicated and it has enough
0:43
features I will also cover more
0:45
sophisticated features used on the
0:47
higher end mcus like the cortex M7
0:50
series but later in the
0:52
series in today's video we will see how
0:55
to connect devices how to configure the
0:57
uart and how to send data via the
1:00
art we will also understand the
1:03
parameters used in the
1:05
configuration if we want to send and
1:07
receive the data from a computer using
1:09
the uart we need to use a USB to TTL
1:14
module the module converts the USB
1:16
signal to TTL level and hence can be
1:21
microcontroller this is how the
1:23
connection should be made with the
1:25
module here for demonstration I am using
1:31
module the TX pin from the MCU connects
1:34
to the RX pin of the module and the RX
1:37
pin from the MCU connects to the TX pin
1:40
of the module the uart connection is
1:43
always made in the cross connecting the
1:45
TX pin with the RX and RX pin with the
1:49
TX this is how the connection should be
1:53
device as I mentioned the TX pin of the
1:56
first device connects with the RX pin of
1:58
the second device and vice versa if you
2:02
own a nucleo or a discovery board from
2:05
St you might not need the additional
2:08
module to communicate with the
2:10
computer some of these devices have the
2:13
feature called virtual comport where the
2:15
USB used for the SD link can also be
2:18
used to communicate with the
2:20
computer here is the schematics for the
2:23
nucle F 446 re development board from St
2:28
scroll down to the St link section
2:31
generally SD uses the f103 C8 MCU for
2:35
the SD link the SD link TX pin is
2:38
connected to the pin pa2 of the SD link
2:41
and the SD link RX is connected to the
2:44
pin pa3 of the SD link these SD link
2:48
pins are connected to the nucleo board
2:50
in a cross connection and the SD link
2:52
USB can act as the virtual
2:55
comport actually this one is not that
2:58
well documented we will see another
3:00
schematics in a while as I mentioned
3:03
some of the discovery and nuclear boards
3:05
do support the virtual comport but not
3:08
all of them support it here is another
3:11
schematic of the SDM 32 F4 Discovery
3:15
board here at the SD link section you
3:19
can see that there are no TX and RX pins
3:21
connected to the SD link which simply
3:24
means that the uart virtual comp Port is
3:27
not available with this discovery board
3:30
but this certainly does not mean that
3:32
all discovery boards do not support the
3:36
comport here I have another schematic of
3:41
f412 Discovery board and in the SD link
3:45
section you can see the virtual comp
3:47
Port this one is relatively a new
3:50
development board from St and it is
3:53
documented pretty well the usart 2tx pin
3:57
is connected to the SD link RX and the
3:59
usar 2 RX is connected to the SD link TX
4:03
to transfer the data via the virtual
4:06
comport we need to use the usart 2 here
4:10
in the MCU section you can see the usart
4:13
2 pins are actually the pins pa2 and
4:17
pa3 I am using the nucleo F 446 R
4:21
development board and it does support
4:25
comport I am going to use this serial
4:27
software to communicate with the board
4:30
here you can see right now there are
4:33
only two ports available now I am
4:36
connecting the nucleo board to the
4:38
computer here it popped up as a storage
4:42
device and here you can see an extra
4:44
Port available this is the virtual com
4:47
Port created by the nucleo board and we
4:50
can use this port to send data to the
4:52
board via the uart so if you own a
4:55
nucleo or Discovery board it might or
4:58
might not support the virtual comp Port
5:01
you can check the schematic of the board
5:03
in the SD's website itself after opening
5:07
the board's page go to the cad resource
5:10
tab here you can see the schematic for
5:13
the respective board so I guess this is
5:16
enough information on the connection
5:18
between the computer and the board let's
5:21
start the cube ID and create a new
5:26
project as I mentioned I am using the
5:28
nucleo F 446 re development board give
5:33
some name to the project and click
5:38
finish let's start with the clock setup
5:42
here I am enabling the high-speed
5:44
external Crystal to provide the clock to
5:46
the MCU the board has 8 MHz Crystal on
5:50
it and we will run the system at maximum
5:57
MHz go to CIS d bug and enable serial
6:01
wire now go to the connectivity Tab and
6:05
here you can see a lot of uart instances
6:07
are available on this board note that
6:10
some of them are named as you art and
6:12
the rest are named as you sart uart
6:15
stands for universal asynchronous
6:18
receiver and transmitter whereas the
6:20
usart is universal synchronous
6:22
asynchronous receiver and
6:24
transmitter basically the uart is only
6:27
capable of asynchronous transmission
6:29
where as the usat supports both
6:33
asynchronous other than asynchronous
6:35
mode the uart also supports half duplex
6:38
mode the multi-processor communication
6:44
protocols all these modes and protocols
6:47
do not require the synchronizing clock
6:50
whereas the usart supports all the uart
6:52
features and also the synchronous mode
6:55
along with the smart card
6:57
communication these modes require
6:59
require a synchronizing clock and hence
7:01
they are supported only by
7:03
usart in today's video we will use the
7:06
asynchronous mode for communicating with
7:09
the computer I am using usart 2 because
7:12
it is connected to the virtual comp port
7:15
and hence I do not need to connect any
7:17
additional Hardware to transfer the data
7:20
let's check the parameter settings now
7:23
the board rate of the transmitter and
7:25
receiver must be the same in order to
7:27
receive the correct data transm
7:29
Ed here I am keeping it at
7:32
115200 and later we will set the same
7:35
rate for the receiver
7:37
also the word length includes the data
7:40
bits and the parity bit the parity bit
7:44
can be used to check for the errors
7:47
transmission here the picture shows a
7:51
frame we have the details for different
7:56
lengths the uart frame consists of the
7:59
start bit followed by the eight data
8:01
bits which may or may not include the
8:04
parity and finally the stop bit we can
8:07
use half stop bits or one or 1.5 or even
8:14
bits we can use different word lengths
8:17
depending on the requirement of the
8:19
transmission I am going to use the 8bit
8:23
length there will be no parity bit and
8:26
the stop bit will be one note here that
8:29
we don't have all the options shown in
8:31
the picture this may vary depending on
8:34
the microcontroller you are using the
8:37
data direction will be receive and
8:39
transmit as we want to do both now comes
8:44
oversampling oversampling is used to
8:47
increase the receiver's tolerance for
8:50
deviation basically if there is some
8:53
deviation during the transmission which
8:55
may arise due to some fluctuation in the
8:57
board rate the over sampling is used to
9:00
get the correct data oh sampling of 16
9:04
means that each serial bit is sampled 16
9:06
times and hence increases the
9:09
accuracy but it also reduces the maximum
9:12
board rate of the receiver as shown in
9:15
this picture so there is a tradeoff
9:18
between the maximum board rate support
9:20
and the accuracy of the data received I
9:24
will leave it to 16 as I am not using a
9:27
very high board rate you can see the pin
9:30
pa2 is configured as the TX pin and the
9:33
pin pa3 is configured as the RX pin if
9:37
you are using a converter module or
9:40
another MCU as the receiver you need to
9:43
connect these pins in Cross
9:45
connection I am using the virtual comp
9:48
Port of the SD link so these pins are
9:50
already connected in Cross to the
9:52
respective pins of the SD
9:54
link that is all we need to
9:57
configure let's generate the project
10:00
now here is our main
10:03
file let's define a data buffer that we
10:08
uart the sln is used for the new line so
10:11
the next data will be printed on a new
10:15
console here in the main function uart
10:18
is pre-initialized by the generated code
10:20
so we will simply transmit the data
10:24
let's write the code in the while
10:26
loop here we will call the function art
10:30
transmit the parameters are the uart
10:32
instance uart 2 in our case the data
10:35
pointer the buffer we defined above the
10:38
size of the data in bytes so 12 in our
10:42
timeout the function will time out in 1
10:45
second if the data is not
10:48
transmitted let's give a delay of 1
10:50
second between each call to this
10:55
function all right we do not have any
10:57
errors so let's Flash the program to the
11:01
board let's open the serial terminal now
11:05
we need to use the same configuration
11:07
that we used for the
11:09
uart so the board rate will be
11:12
115200 there are eight data bits with no
11:15
parity and one stop bit let's open the
11:18
port now here you can see the data is
11:22
being received in the console and it is
11:26
second this is exactly how we programmed
11:30
MCU so our code is working fine so
11:35
far let's try to send a number via the
11:50
uart this shouldn't be an array
11:55
here we need to pass the address here
11:58
and let's change the size to two
12:05
bytes all right let's flash it to the
12:11
now here you can see that we are not
12:14
quite receiving the number 1 2
12:16
3 instead we are receiving the curly
12:20
brackets but in reality we are receiving
12:22
the correct data to understand it let's
12:26
check the asy table here you can see the
12:30
character defined for the number 123 is
12:32
the same as what we received via
12:35
tart the UR transfers the data in the
12:38
ASI format so we need to pass the data
12:41
to it in the similar form if we want to
12:44
transmit any number it must be first
12:47
converted to the asy character form
12:50
let's define an array to store the asy
12:52
character form of the number the number
12:55
of bytes in the array should at least be
12:57
equal to the number of
12:59
digits now we will use the S printf
13:04
conversion the format specifier D is
13:07
used because we are converting an
13:09
integer here the converted characters
13:12
will be stored in the number array so we
13:15
will send the array to the
13:17
uart we need to include the standard IO
13:20
header file for the S print
13:32
we still have a warning and that is
13:34
because the S print F takes the
13:36
character pointer as the
13:38
parameter you can type case it here but
13:41
I anyway it won't change anything all
13:44
right let's Flash the program to the
13:46
board now we are receiving the numbers
13:49
on the console so everything works as
13:52
expected we were able to transmit the
13:55
data via the uart and receive it on the
13:57
serial console on the computer in the
14:00
next video we will see how to use the
14:03
interrupt and dma to do the same then we
14:06
will move towards the receiving part
14:09
which is comparatively harder this is it
14:12
for the video I hope everything was
14:15
clear you can download the code from the
14:18
link in the description leave comments
14:21
in case of any doubt keep watching and
14:24
have a nice day ahead