0:08
hello and welcome to controllers Tech
0:12
this video is the start of a new series
0:14
of tutorials on the AVR microcontrollers
0:18
I know I have already made a few videos
0:20
using the at tiny 85 controller but I am
0:24
not planning on continuing those videos
0:26
for now actually I wanted to continue
0:29
the AVR series with a board that has
0:33
this helps us to understand the issue if
0:36
something is not working as expected
0:38
for that reason I bought this a t
0:41
tiny817 explained mini board
0:44
this comes from the microchip itself it
0:47
is cheaper compared to other official
0:48
AVR so it is better to use for the
0:53
I am going to continue the AVR series
0:57
in today's video we will see how to
1:00
create our first project and blink the
1:03
we will also use the debugger a little
1:06
set some breakpoints and understand how
1:10
let's see the documentation available
1:13
for the explained Mini
1:15
the board features an 80 tiny 817
1:18
microcontroller with one user LED one
1:21
user button and two Q touch buttons
1:24
it supports programming and debugging
1:27
and also has a virtual com port to send
1:30
the data directly to the computer using
1:33
it can be powered by the USB itself so
1:36
we don't need to connect anything else
1:38
to power and program this board
1:41
this is the pin out of the board
1:43
today we are only blinking the LED which
1:46
is connected to the PIN pc0
1:49
just keep this in mind as we will need
1:52
it while we write the code
1:54
let's see the manual for the at tiny817
2:02
this microcontroller features a 24-pin
2:05
Soc with 512 bytes of SRAM 8 kilobytes
2:09
of Flash and 128 bytes of eprom it can
2:14
run at a maximum clock of 20 megahertz
2:16
and have three different timers or
2:20
it supports all the common peripherals
2:22
like uart SBI i2c ADC DAC
2:29
we will cover all of them in this series
2:33
let's see the clock controller there are
2:36
different clock options available
2:39
we can use the internal oscillators of
2:41
20 megahertz or 32 kilohertz
2:45
or we can also run the system with some
2:47
external clocks if needed
2:50
I will use the internal oscillators
2:52
throughout this series
2:54
here is the clock diagram
2:57
the internal oscillator of 20 megahertz
2:59
can clock the main CPU along with the
3:02
non-volatile memory and the ram
3:05
the same clock can also be used for the
3:08
different peripherals available on the
3:11
for RTC Watchdog and the Browner out
3:14
detection we need to either use the
3:17
internal or external oscillator of 32
3:21
whatever oscillator we use we can use
3:24
the prescaler to divide the clock
3:25
further to our requirement
3:28
we will now see the clock control
3:30
registers but first let's create a new
3:33
project in microchip Studio
3:37
I have the xc8 compiler installed which
3:41
comes by default in the microchip Studio
3:44
give some name to the project choose the
3:47
project folder and click ok
3:50
I have the at tiny817
3:54
all right the project has been created
3:57
let's define a new function to
4:00
the main clock control register a is
4:03
used to select which oscillator we are
4:06
as I mentioned I am going to use the
4:09
internal 20 megahertz oscillator
4:12
so we need to write 0 to the first two
4:15
bits of this register the seventh bit of
4:18
this register enables the system clock
4:20
output to the respective pin in case you
4:23
need the clock output for some other use
4:25
write a 1 to this position
4:28
I don't need the clock in the output so
4:31
I am leaving this to zero now one
4:34
important thing about these registers is
4:36
that they have the change protection
4:38
which means that we cannot simply change
4:41
the default configuration of these
4:44
the AVR has a special way to update
4:48
we need to write the data into these
4:50
registers using the protected right
4:54
let's write the value 0 to the main
4:56
clock control a register
5:03
next is the main clock control V
5:06
register this register controls the
5:09
pre-scaling of the main clock
5:11
the first bit is used to enable or
5:13
disable the prescaler and the next four
5:15
bits are used to set the prescaler value
5:19
since we do want to use the prescaler we
5:22
will write a 1 to this zeroth bit
5:24
the main clock is at 20 megahertz right
5:28
since we are only blinking the led the
5:31
clock is not the priority for today's
5:34
so I will use the prescaler of 4 which
5:37
will bring the clock down to 5 megahertz
5:41
so basically we need to write a 1 to the
5:43
bits ranging from bit 1 to bit 4. our
5:47
final data for this register is zero
5:50
zero zero one one which is 3 hex
5:53
this register is also protected using
5:56
CCP so we will again use the protected
6:00
let's write the value 3 hex to the main
6:04
the next register is the main plot lock
6:07
and it is used to lock the clock
6:09
configuration we just made
6:11
this prevents the clock registers from
6:14
unintentional modification by the
6:17
we don't need it for today so let's skip
6:20
it next is the clock status register
6:24
this register is used to check the state
6:27
of the different oscillators
6:29
since we are using the internal
6:31
oscillator it can be used to check if
6:33
the oscillator is stable or not
6:36
the bit 4 of this register is set to 1
6:38
if the oscillator is stable in our code
6:41
we will wait for the bit 4 of the status
6:43
register to set to 1. this will make
6:47
sure that the internal oscillator is
6:49
stable and we can proceed further the
6:52
next register controls if the oscillator
6:54
will run during the standby
6:56
let's leave it to the default settings
7:00
next is the calibration for the
7:03
I am leaving it to default settings as
7:06
then we have the setup for the other
7:08
oscillators which we don't need for now
7:12
so this is it for the clock setup
7:14
we have set the internal oscillator of
7:17
20 megahertz with a prescaler of 4 so
7:19
our final clock to the CPU is 5
7:23
let's define it in the file
7:26
also include the delay library for the
7:28
delay functions to work
7:30
now we will see the port registers which
7:33
control the i o pin configuration
7:36
the general purpose pins can be
7:38
configured with internal pull-up and
7:40
they can also be inverted
7:42
the interrupt can be triggered with
7:44
different configurations
7:47
it can sense both rising and falling
7:49
edges of the input signal which will be
7:51
useful for our input signal measurement
7:55
let's see the registers available for
7:58
the port configuration
8:00
we have the direction register which can
8:02
be used to set the direction of the pin
8:06
writing a zero to the respective pin
8:09
sets it as input while writing 1 to the
8:11
pin sets it as the output pin other than
8:14
the direction register we also have some
8:17
other registers to control the direction
8:20
the first is the direction set register
8:23
this register can also be used to set
8:26
the pin as output by writing a 1 to the
8:30
note that we can only set the pin as
8:33
output using this register
8:35
writing a zero to a pin has no effect on
8:39
this register can be used for the read
8:43
for example if I want to set the PC3 as
8:47
output and I don't want other pins of
8:49
this port to get affected
8:51
I have two options to do so
8:54
I can read the direction register modify
8:57
the PC3 value to 1 and then write the
9:00
data back to the register
9:02
or by simply using the direction set
9:04
register where I can simply write a 1 to
9:09
it will perform the read modify right by
9:12
itself and I don't need to worry about
9:15
we also have the direction clear
9:18
it is the same as the direction set but
9:21
instead of setting the pin as output it
9:25
writing a 1 to the particular pin sets
9:27
the pin as input and writing a zero does
9:31
the LED on the board is connected to the
9:34
pin pc0 so we need to set it as output
9:38
now we can do that using two different
9:42
either use the direction register to
9:44
first read the data then modify the pc0
9:47
to 1 and then write the data back to the
9:50
register or use the direction set
9:53
register to Simply write a 1 to the PC
9:56
0. both of these statements do the same
10:01
let me comment out one of them now in a
10:04
while loop we will blink the LED
10:07
we also have the direction toggle
10:09
register which toggles the direction of
10:11
the pin from input to output and vice
10:15
the output register is used to set the
10:17
pin state to high and low
10:20
writing a 1 to a particular pin sets it
10:22
as high and writing a zero sets it as
10:26
just like the direction register here
10:28
also we have the outset register out
10:31
clear register and the out toggle
10:34
they are again used in the similar
10:36
manner as the direction registers
10:39
we will see the output using all
10:43
let's start with the simple output
10:47
here I am setting the PIN pc0 to High by
10:51
writing a 1 to the zeroth position
10:54
let's give a delay of one second
10:56
and reset the PIN pc0 to Low by writing
11:00
a 0 to the zeroth position
11:03
again give the delay of 1 second
11:06
the above code will blink the LED every
11:10
now let's try the same using the outset
11:12
and out clear registers
11:15
to set the pin PC 0 we will write a 1 to
11:18
the zeroth position in the outset
11:19
register and to reset the pin we will
11:22
write a 1 to the zeroth position in the
11:26
or we can also use the out toggle
11:28
register to toggle the PIN pc0
11:32
let's comment out these parts and we
11:34
will test using the normal output
11:40
we need to call the clock init function
11:45
all right let's build the code now
11:48
the delay inclusion has some problems
11:52
actually it should be util not AVR
11:56
or write the code builds fine now
12:00
the code size is 190 bytes
12:04
we can directly Flash the code to the
12:06
board or start the debugging session
12:09
let's directly flash it for now
12:12
the code is flashed and you can see the
12:15
LED is blinking every one second
12:20
here in the tools you can see the board
12:22
is connected via the updi
12:26
this is the default setup for the
12:28
explained mini and we don't need to do
12:30
anything by ourselves
12:32
Let's test the second set of statements
12:42
the code size has been reduced to 184
12:46
bytes with these statements let's flash
12:50
the LED is blinking every one second so
12:54
this way of setting and resetting pins
12:57
now let's see the toggle function
13:00
the code size is now reduced to 160
13:09
the LED is blinking fine so the toggle
13:14
we can use either of these methods to
13:16
set and reset the PIN
13:18
now let's quickly see the debugger
13:24
this button will launch the debugging
13:30
all right here we are in the debugger we
13:34
have access to different memory
13:35
locations like eprom SRAM fuses Etc
13:42
we also have the watch Windows to watch
13:46
we will see their functions in the
13:50
today we will just use the breakpoints
13:54
right click on the statement where you
13:56
want to put the breakpoint go to break
13:58
point and insert the breakpoint
14:01
now we will run the debugger
14:03
it hit the first break point we set
14:06
the LED is off because this statement
14:09
turns it on and it hasn't been executed
14:12
resume the debugger and it hits another
14:16
the LED is on now and this statement
14:19
once executed will turn it off
14:25
both the breakpoints are in the while
14:27
loop so we will keep hitting them
14:40
we have other functions like step over
14:44
Etc but I will show their usage in some
14:47
other tutorial where they will be needed
14:50
we can stop the debugger using this stop
14:53
button and the board will run its
14:56
operation in a normal way
14:58
this is it for the video
15:00
I hope you understood some Basics about
15:02
setting the clock and blinking the LED
15:06
in the next tutorial we will see how to
15:09
configure the pin as input and how to
15:12
we will do this in a blocking way and
15:15
also using the interrupt
15:17
the link to download the code is in the
15:22
leave comments in case of any doubt keep
15:25
watching and have a nice day ahead