0:18
welcome to controllers tech this is the
0:21
third video in the series of register
0:24
based programming for stm32
0:26
and today we will see how to use uart in
0:32
i will cover the basic sending and
0:36
interrupts or dma will be covered later
0:41
let's start by creating the project in
0:53
i am only going to include the core and
1:07
first of all let's add a main file to
1:16
i will create a main function and a
1:29
next i am going to include the clock
1:34
i have already covered this you can
1:36
check the first video in this playlist
1:49
let's add those files in our project
2:05
this configuration is to run my mcu at
2:07
maximum 180 megahertz clock frequency
2:23
let's include the rcc config.h in the
2:33
some of you guys also requested me to
2:35
cover how to write the header files
2:38
and the source files well
2:41
i will also show that in this video
2:44
in my previous video i blinked the led
2:47
with the precise delay using the timer
2:50
this here is the main file from the code
2:54
so i am going to use this file to write
2:57
a library for the delay
3:04
first create a new header file and let's
3:17
header files basically contains the
3:19
functions that you can use globally
3:27
so i am going to include these functions
3:30
into the delay.h file
3:32
so that later i will be able to call
3:34
them from the main file
3:37
you see we have error with the integer
3:41
this is because we haven't included any
3:45
let's include std into.h and the error
3:50
now let's create a source file and call
4:02
source file will contain the details
4:06
let's include the delay.h here
4:12
i am also including the rcc config.h
4:16
as it already contains the device header
4:20
you can also include the device headers
4:24
we need the device headers because we
4:26
are using the registers
4:28
which are defined in the device header
4:31
that's all we have created the delay
4:41
now let's call the delay.h in the main
4:59
we don't have any errors so we are good
5:03
now we will start with the uart
5:10
let's take a look at the reference
5:30
here is the procedure to configure the
5:34
so first we need to enable the uart
5:37
then define the word length
5:41
then select the board rate and so on
5:52
here are the detailed steps that we need
5:56
first of all we need to enable the
6:00
then configure the pins for alternate
6:04
and then we will configure the uart as
6:06
per the reference manual
6:23
so let's start with the first step
6:34
i am using uart too so the pins used up
6:54
to enable the clocks we will look into
7:08
rcc apb1 peripheral clock enable
7:18
as you can see here the 17th bit of this
7:22
enables the uart 2 clock
7:32
let's write a 1 in the 17th position
7:42
we also need to enable the gpio clock
7:47
pins this can be done in the ahb1 enable
7:57
here zeroth bit controls the gpioa
8:07
let's write a one in the zeroth position
8:15
now configure the alternate function for
8:24
let's see the gpio registers
8:32
gpio mode register is used to set up
8:35
different roles for the gpio pins
8:39
and as you can see here we can set the
8:42
alternate functions also
8:44
i am using pins pa2 and pa3 for the
8:50
so i need to modify the mode r2 and mode
8:57
basically i need to write a 2 in the 4th
9:00
and the 6th positions
9:05
writer 2 in the fourth position and
9:09
in the sixth position
9:38
next we need to configure the speed for
9:42
and we should select the highest
9:46
so this time we need to write a 3 in the
9:49
and the 6th positions
10:24
we don't need to set up any of these
10:28
we will directly go to the alternate
10:37
there are two alternate function
10:41
and af high they both have the same
10:44
alternate functions defined
10:53
we will come back to these but first
10:57
what setup do we need for the uart
11:09
as you can see here from uart 1 to 3
11:12
i need to select af7
11:19
also note that airflow is used to
11:22
control the pins 0 to 7
11:24
and afi is used to control pins 8 to 15.
11:34
as i have pin 2 and 3 for the uart 2
11:37
i need to select af7 in alternate
11:40
function low register
11:51
so i need to write 7 in the 8th and the
12:04
here a f 0 represents the alternate
12:07
function low register
12:09
and afr1 would represent alternate
12:11
function high register
12:18
that's all the setup needed for the pins
12:22
now let's see the uart configuration
12:26
first of all we need to enable the uart
12:29
let's see the uart registers now
12:43
control register 1 have the 13th bit as
12:49
and we need to write a 1 here
13:00
let's first reset all
13:07
now write a 1 in the 13th position to
13:16
next we need to program the m bit to
13:19
define the word length
13:25
it's the 12th bit in the cr1 register
13:28
and we will keep it zero to select eight
13:42
this should be in here not the ore
13:49
now comes the important part that is
13:52
selecting the board rate
13:58
board rate register is divided into two
14:02
and the fractional parts fractional part
14:08
so maximum value it can accommodate is
14:12
mantissa on the other hand have 12 bits
14:15
and can have larger values also
14:18
there are some examples given in this
14:20
manual and we will try to understand the
14:22
setup using those examples
14:31
here is formula to calculate the baud
14:35
this over eight here means the over
14:37
sampling and it should be kept zero for
14:46
we will try to understand the usart div
14:55
suppose the use active value is 25.62
14:59
we need to multiply the 0.62 with 16 to
15:03
get the fractional part of board rate
15:10
so here the fractional part becomes 10
15:13
and mantissa is anyway equal to 25
15:20
let's see one more example this time the
15:23
fractional part is 15.85
15:26
so nearest integer is 16.
15:29
now remember that the fractional part
15:33
so the maximum value it can have is 15.
15:37
and here we got 16 so this one is the
15:40
and it will be added to the mantissa and
15:43
the fraction will become
15:44
zero this makes up mantissa's 51.
15:49
i hope you understood the process for
15:53
now let's do the calculation for our
15:57
the formula here requires the clock
16:07
this is the timer diagram of f4
16:11
here you can see uart 2 is connected to
16:15
which is running at 45 megahertz
16:18
i have already covered the clock section
16:20
in my first video and there you saw that
16:23
all the clocks are running at their
16:34
so the uart two clock is also running at
16:40
and i want the board rate of 11 5 200.
16:50
so i got the use octave as 24.414063
16:56
out of this let's first do the
16:58
calculation for fractional part to check
17:00
if we get some carry or not
17:03
otherwise we need to add that carry to
17:07
multiply the decimal value with 16
17:13
the nearest integer is 7 in this case
17:16
which i need to feed
17:18
to the fractional part
17:27
so i need to write a 7 in the zeroth
17:30
24 in the fourth position
17:41
now the last step is to enable the
17:43
transmitter and the receiver
17:56
this can be done again in the control
18:13
you can see here the bit2 enables the
18:16
and the bit 3 enables the transmitter
18:30
this completes the configuration for the
18:33
now let's write a function to send the
18:35
character to the uart
18:43
to do so we need to copy the data into
18:46
the usart dr register
18:48
and then wait for the tc bit to set
18:51
transmission complete bit gets set once
18:54
the data is transferred to the shift
19:12
you can see the steps are as per given
19:14
in the reference manual
19:23
let's copy the data into the data
19:45
t c is the sixth bit of the status
19:48
which becomes one when the transmission
19:54
so we will wait until the tc is one
19:57
let's test this much part of the code
20:02
in the main function we will call the
20:04
system clock config first
20:07
then i am going to call the timer config
20:10
and at last uart config
20:15
timer here is just to provide
20:19
in the while loop i am going to send a g
20:22
and this should happen every one second
20:25
we don't have any errors so let's go
20:29
before flashing we need to go to options
20:32
and change the mcu frequency here
20:36
mine is running at 180 megahertz
20:41
change to sd link debugger
20:48
let's open the console note the settings
20:51
are as per the configuration
20:54
that is board rate is eleven five two
20:57
data length is eight and no parity
21:01
let's upload the code you can see that
21:05
is being transmitted every one second
21:08
so things are all right till now
21:12
let's proceed forward let's create a
21:23
now i am going to send the hello every
21:29
let's build and upload this one also
21:38
this is also working all right so the
21:41
transmission part is pretty good
21:44
let's try to receive data now
21:56
you uart get char will be used to
22:03
here first we will wait for the rxne bit
22:07
which indicates that we have a data
22:11
now we will read the data from the data
22:20
you can see the rxne is the fifth bit in
22:26
we will wait for this bit to set
22:49
and now read the data from the data
23:08
now in the while loop let's read the
23:10
data and then send the data back to the
23:44
as you can see the hello got transmitted
23:49
here pink color is the one i am sending
23:52
and black is the one i am receiving
24:02
it works all right for some parts but
24:05
this is not the solution to receive
24:09
as you can see here the d is missing
24:14
this can happen as we can't do the
24:16
sending and receiving at the same time
24:18
in the blocking mode
24:20
to handle these scenarios we need to use
24:25
but you can receive data of whatever
24:27
length you want and store that data into
24:31
later process that data and do whatever
24:36
so here both the transmission and
24:38
reception works properly
24:41
i will go back to transmitting some
24:59
and it works as expected this is it for
25:04
the clock setup and the timer setup
25:07
videos can be found in this playlist
25:10
you can download the code from the link
25:14
next video will cover the i2c
25:18
keep watching be safe and have a nice