0:09
hello and welcome to controllers Tech
0:12
this is the second video in the AVR
0:14
series using the explained mini
0:16
evaluation board and in today's video we
0:19
will see how to use the input button we
0:23
will interface the button using the
0:24
input pin which we will read in a while
0:27
loop and also using the interrupt
0:30
the explained mini evaluation board
0:33
already has a user button soldered onto
0:35
the board itself so we don't need to
0:39
button here you can see how the button
0:42
is connected to the board the pin PC 5
0:45
is connected to the VCC so this pin is
0:50
default when the button is pressed the
0:53
pin will be pulled low to the
0:55
ground keep this in mind as we will use
0:58
this information to trigger the click in
1:01
code let's create a new project in the
1:05
Studio select xc8 application project
1:09
give some name to the project and click
1:13
okay I have the at tiny 817
1:17
microcontroller all right the project
1:20
generated we will use the clock setup
1:25
tutorial here is the main file from the
1:28
previous video Let's copy the clock
1:30
initialization function from here and we
1:33
will paste it in our main file in the
1:37
main function initialize the
1:41
clock now we will configure the pin as
1:43
input so let's open the data sheet of
1:48
817 go to the port register
1:53
description here first the direction
1:55
register will be used to set the pin pc5
1:58
as the input pin so we need to write a
2:04
position also the LED is connected to
2:07
pin pc0 which needs to be set as output
2:10
so write a one to the zeroth position
2:13
our final data for the direction
2:17
hex we have already covered the output
2:20
registers in the previous
2:22
tutorial the state of the input pin can
2:28
register the corresponding bit of this
2:31
register is set to one if the pin is
2:33
high and it resets to zero is the PIN is
2:37
low first we will read the pin state in
2:40
the while loop and later we will use
2:42
interrupt to do the same so in the while
2:46
loop we will read the pin pc5 by
2:49
monitoring the fifth bit of the input
2:51
register if the button is pressed the
2:54
pin will be pulled low to the ground and
2:56
the fifth bit of this register will
3:01
when that happens we will turn the LED
3:03
on by setting the pin
3:05
pc0 now we will wait for the button to
3:08
release and the input pin to go high
3:12
this is to make sure that we don't get
3:13
The Accidental trigger which is very
3:16
common when we press the button
3:18
basically the LED will remain on as long
3:23
pressed after the button is released the
3:25
control will come out of the if Loop and
3:28
here we will turn the LED on off let's
3:31
build the code we don't have any errors
3:39
board you can see the LED is turning on
3:43
when the button is being
3:44
pressed also the LED is on as long as
3:50
pressed let's debug the project to
3:53
understand how the execution
3:55
works we will add the break point inside
4:01
debugger when the button is pressed we
4:04
hit the break point the LED is still off
4:08
as this statement hasn't been executed
4:10
yet let's step over this
4:14
function the LED is on now the button
4:18
has been released so this function will
4:20
not wait for it to happen and once this
4:23
statement is executed the LED turns off
4:28
now you can see the control is not going
4:30
inside the if Loop let's resume the
4:33
debugger again and press the button we
4:37
have hit the break point again but this
4:39
time I am keeping the button
4:41
pressed as long as the button is pressed
4:44
the pin state will be low so the control
4:47
cannot come out of this while loop when
4:50
the button is released the control comes
4:52
out and turns the LED off so I hope you
4:56
understood how to read the button state
5:00
this code is fine as long as there are a
5:02
smaller number of things inside the
5:04
while loop but when there are a lot of
5:06
statements along with the delays this
5:09
idea is not going to be
5:11
effective say for example there is a
5:13
delay of 1 second being executed and if
5:16
the button is pressed and released in
5:18
that duration it will go unnoticed by
5:22
statement The Click is only recognized
5:24
if the button is pressed at the same
5:26
time when the if statement is being
5:30
to avoid such scenario we can use the
5:33
external interrupt to identify the
5:35
trigger let's comment out the while loop
5:41
first among the port registers we have
5:45
Flags the corresponding bit of this
5:48
register is set when the external
5:50
interrupt is triggered the bit can be
5:53
cleared by writing a one to the bit
5:56
since we are going to use the pc5 as the
5:58
external interrupt pin the bit five of
6:01
this register will be set next we have
6:06
register the input sense bits of this
6:09
register can be used to enable the
6:12
interrupt by default the bits are set to
6:15
zero which disables the interrupt but
6:19
enabled we can enable the interrupt for
6:22
Rising Edge falling Edge or for both the
6:26
edges our button is connected to the VCC
6:29
so when the button is pressed it is
6:33
ground this is the falling edge of the
6:36
signal when the button is released it is
6:39
pulled back to the VCC which is the
6:43
signal I am going to enable the
6:45
interrupt for the falling Edge so that
6:47
the interrupt will be triggered as soon
6:49
as the button is pressed to enable the
6:52
interrupt for the falling Edge we need
6:54
to write three hex to the input sense
6:57
bits the other bits of this register can
7:00
be used to enable or disable the pull-up
7:02
resistance and to invert the input
7:05
output level let's leave them to default
7:08
States for now so we need to write three
7:11
hex to the zeroth position of this
7:14
register after enabling the interrupt
7:17
for the pin we need to enable the global
7:20
interrupt include the interrupt header
7:24
also now once the button is pressed the
7:27
interrupt will trigger we need to write
7:30
a separate function to handle this
7:32
interrupt the parameter of the ISR
7:35
function is the interrupt Vector that
7:37
needs to be handled we can find the
7:40
interrupt vectors in the data sheet of
7:43
device here is a list of different
7:46
interrupt vectors available for the at
7:49
817 we have the vector for Port C and
7:52
its name is Port C Port let's set it as
7:56
the parameter of the ISR function
7:59
now we will write the function to handle
8:03
interrupt the port C interrupt is common
8:05
for all the pin of this port so we need
8:08
to First identify which pin triggered
8:11
interrupt we can do this by reading the
8:15
register so let's Define a variable in
8:19
flags and store the value of the
8:23
variable here we are expecting the
8:25
interrupt from the pin five of this port
8:27
so we will check if the fifth bit of
8:29
this register is high or not if it is
8:32
setor one this means the interrupt is
8:37
pc5 so toggle the LED first and then
8:41
clear the flag by writing a one to the
8:43
bit five if any other pin is also set as
8:47
the external interrupt you can write
8:49
another if condition for that pin in the
8:51
similar way that is it let's build and
9:02
you can see how the LED state is
9:05
Switched every time the button is
9:09
pressed let's debug this code to
9:12
understand how this actually
9:14
works I am setting a breakpoint here
9:22
function let's press the button now we
9:25
have hit the break point you can hover
9:28
on the very variable to see its current
9:31
value or just add it in the watch
9:34
below here the value of the in Flags is
9:37
20 hex which means that only the bit
9:40
five is set we can also see the decimal
9:44
equivalent of this value since the bit
9:47
five is set our if condition will return
9:50
true and if we step over this statement
9:54
inside the LED is still off and stepping
9:57
over this statement will turn turn it on
10:00
now the next statement will clear the
10:03
flag you can see that after resuming the
10:06
debugger the control doesn't enter the
10:09
ISR function until the button is pressed
10:11
again this time the LED is turned on so
10:15
the toggle statement will turn it off
10:18
the in flag is a local variable so
10:21
whenever the control goes out of the ISR
10:23
function this variable will be optimized
10:26
out so far we set the interrupt for the
10:29
falling Edge so it got triggered as soon
10:33
pressed similarly if we set it for the
10:36
rising Edge the interrupt will trigger
10:39
released and if we set for both the
10:42
edges the interrupt will trigger twice
10:44
when the button is pressed and also when
10:48
released let's do this and we will set
10:51
the input sense to both edges by writing
10:55
position let's build and Flash this to
11:08
you can see the LED remains on for as
11:10
long as the button is
11:12
pressed actually the first interrupt
11:14
turns on this LED and the second
11:17
interrupt turns it off so it remains on
11:20
for as long as the second interrupt is
11:22
not triggered basically as long as the
11:26
released this is it for the video
11:29
I hope you understood how to handle the
11:31
input button and the external
11:34
interrupt the link to download the code
11:37
is in the description of the
11:39
video leave comments in case of any
11:42
doubt keep watching and have a nice day