0:09
hello and welcome to controllers Tech
0:12
this is the third video in the AVR
0:14
series using the explained mini board
0:17
and today we will see how to use the
0:19
uart the uart peripheral is a big topic
0:22
to cover so we will cover it in multiple
0:25
videos this video will cover the
0:27
initialization of the uart the board
0:30
rate configuration and finally we will
0:32
send the data using the uart the
0:35
reception of the data will be covered in
0:37
the next video and later we will develop
0:40
a more robust library to send and
0:42
receive data via the uart just like the
0:44
Arduino have let's check the at tiny 817
0:50
sheet here you can see the usart
0:54
features it supports the full duplex
0:57
operation and the one wi mode and rs 485
1:00
mode in the half duplex
1:02
operation we can use asynchronous or
1:07
operations it supports up to nine data
1:09
bits with one or two stop
1:12
bits the board R generator can generate
1:15
any board rate without the need for an
1:18
oscillator it has the built-in error
1:21
detectors and the separate interrupts
1:25
receive it supports other modes as well
1:28
but we will cover them later in the
1:30
series we will see the rest of the
1:33
description later in the video when we
1:35
will cover the specific
1:37
register let's check the register
1:44
description we have the receiver data
1:47
registers in the beginning we will cover
1:49
these in the next video the transmit
1:52
data register is used to store the data
1:55
that needs to be sent via the
1:57
uart the low byte Reg register holds the
2:00
first 8 Bits of the data if the data
2:04
length is 9 bits we can use the highb
2:07
register to store the ninth bit of the
2:09
data the status register can be used to
2:12
check various Flags like receive
2:15
complete transmit complete data register
2:19
Etc we will cover this later in the
2:22
video the control a register is used to
2:26
enable or disable various
2:28
interrupts we are not going to use the
2:31
interrupt to transmit the data so let's
2:33
skip this this is the register we are
2:37
going to start with the controlb
2:39
register before we go into the details
2:42
here let's create a new project in the
2:46
Studio I am using a virtual machine on
2:49
Mac so things might be a little
2:51
different from how they would appear on
2:54
Windows give some name to the project
2:57
and click okay I am am using the at tiny
3:02
817 here is the code from the previous
3:05
project and I will use the same clock
3:07
initialization that I used in the
3:13
projects initialize the clock in the
3:16
function now we will write a separate
3:19
function to initialize the
3:23
uart here we will start with the
3:26
register the sixth and seventh bits of
3:29
this register enables the transmitter
3:34
uart I will leave the rest of the bits
3:36
to their default States so start of
3:38
frame detection is disabled open drain
3:41
is disabled which sets the receiver in
3:43
the normal operation mode and also the
3:46
multi-processor communication is
3:48
disabled so basically we are just going
3:51
to set the sixth and seventh bits of
4:02
the next register is contrl
4:04
C the seventh and sixth bits of this
4:07
register control the communication mode
4:11
we will leave this to a synchronous mode
4:14
the fifth and fourth bits control the
4:17
mode we will disable the parity here the
4:21
third bit controls the stop bit mode we
4:25
will use one stop bit the first three
4:29
bits control the character size
4:31
basically the data length we will set it
4:36
bits basically we will use the standard
4:39
8 data bits with one stop bit and no
4:42
parity so we need to set the control C
4:52
hex next we have the board
4:55
register it's a 16bit register and the
4:58
value stored in this this register
5:00
generates the board rate at a specific
5:03
frequency the board rate calculation is
5:06
shown in the uart description above as
5:09
you can see here the formula to
5:11
calculate the board register value
5:13
includes the peripheral clock the S
5:16
value and the desired board
5:21
frequency the S value for the
5:23
synchronous normal mode is 16 and let's
5:26
say I want the board rate of 1152
5:31
100 we still need the peripheral clock
5:35
formula let's check the clock
5:38
controller in the main code we have
5:40
defined the CPU clock at 5
5:44
mahz this is because the oscillator of
5:46
20 mahz is being used along with the
5:49
prescaler of four you can see in the
5:52
clock controller after the main clock
5:54
prescaler the CPU clock and the
5:56
peripheral clock both are at the same
6:00
frequency so the peripheral clock should
6:02
also be the same 5 mahz in our
6:05
project here we got the value for the
6:12
173.50 we can't use the decimal value so
6:15
I will just use the value
6:21
174 the rest of the registers are not
6:26
tutorial let's go back to the status
6:34
here we are interested in the transmit
6:36
complete interrupt flag this bit is set
6:40
when the entire data is transmitted and
6:42
there is no data in the transmit data
6:45
register this bit is cleared by writing
6:47
a one to the bit note that even these
6:50
bits are interrupt Flags but they are
6:53
set irrespective of whether you have
6:55
enabled the interrupt or
6:57
not the interrupt is triggered when the
6:59
respective bit is set in the status
7:01
register and the respective interrupt is
7:04
enabled in the control register
7:06
a since we haven't enabled the TX
7:09
interrupt in this register the interrupt
7:12
will not generate when the data has been
7:14
transmitted only the status register bit
7:17
will be set and we can use it to monitor
7:20
if the data has been transmitted or not
7:23
we will clear the TX interrupt flag
7:27
initialization that is it for the UR
7:29
initialization now we will configure the
7:32
pins the explained minib board supports
7:35
the virtual comp Port which means we
7:38
don't need external hardware to
7:41
computer simply connect the USB cable
7:44
and we can communicate to the computer
7:48
uart here is the schematics of the board
7:52
you can see the pins pb2 and pb3 are the
7:55
usart TX and RX pins respectively if you
8:00
are using an external hardware to
8:02
connect to the computer the connection
8:04
will be as shown here connect the TX pin
8:07
of the MCU to the RX pin of the module
8:10
and vice versa to configure the pins for
8:14
the usart we need to set the TX pin as
8:17
the output and the RX pin as the input
8:20
we will set the port B Direction
8:22
register as 4 hex and this will set the
8:25
pin pb2 as the output and the rest of
8:30
now let's write a new function to send a
8:34
uart since we are only using eight data
8:36
bits for the uart we will load the data
8:39
into the transmit data low
8:41
register now we will wait for the TX
8:44
complete interrupt flag to set in the
8:47
register this bit will be set when the
8:50
data has been shifted out and the data
8:54
empty now clear the bit by writing a one
8:57
into it this complete the transfer of a
9:02
uart similarly we will write another
9:05
function to send the entire
9:07
string here we will call the bike
9:09
transfer function as long as the string
9:12
valid that's all the functions we need
9:16
tutorial let's initialize the uart in
9:20
function this is the data that I am
9:23
going to send to the terminal on the
9:25
computer in the while loop we will send
9:28
the string we just defined every 1
9:32
second as I mentioned earlier the
9:34
explained mini supports the virtual comp
9:37
Port so there is no need for an external
9:41
uart you can simply connect the USB
9:44
cable used for the programming also for
9:48
communication let's build the code now
9:51
this is a little different output from
9:55
Windows anyway the code builds fine so
9:58
let's flash it to the
10:03
board the code flashed successfully so
10:06
let's see if we are receiving the data
10:09
computer let's check the device
10:12
manager here you can see the comp Port 4
10:15
is connected and this is the port used
10:17
by the board actually you will see it
10:20
differently on a Windows PC but this is
10:23
how it is on a virtual machine I am
10:26
going to use the real term for the
10:28
serial communication on the computer the
10:31
port 4 is already opened let's change
10:36
115200 you can see we are receiving the
10:48
second let me modify the string
10:52
MCU now let's build and Flash the code
11:04
here you can see the modified string is
11:07
being received on the monitor so the u
11:10
transmission is working fine I hope you
11:13
understood the process of configuring
11:15
the uart and transmitting the data via
11:17
the uart this is it for this video in
11:21
the next video we will cover how to
11:23
receive the data from the uart and store
11:26
it in a buffer the link to download the
11:29
code is in the description below leave
11:32
comments in case of any doubt keep
11:35
watching and have a nice day