#61 Word
#61 Word
0:00
hello and welcome in this video I will explore the edmega
0:07
328 external interrupts introduction
0:14
now let's consider this setup here so here I have my Arduino Uno with a 8 Mega
0:22
3 to 8 microcontroller under normal circumstances stay at Mega
0:28
3 to 8 main program will keep this blue LED turn on that so that is the main
0:36
task on the main program he keeps this blue LED uh
0:42
turn on now when push button a is pressed
0:48
the red LED turns on for a second and when push button B is press the
0:55
green LED turns on for it second now there are two ways of
1:02
achieving this the first method is known as polling and the second method is
1:08
known as intro polling
1:14
with the polling method the microcontroller the atmega328 checks the
1:21
status of these two button button a and button B so referring to the flow chart
1:28
here so uh the first thing it does is to check
1:34
button a and is then you ask the question is button a on
1:40
if it's no it moves on to check button B and ask the question is button B turn on
1:48
or press rather and if not it will repeat checking button a again let's
1:54
assume that this time around that button is pressed or turned on it will then
2:01
turn on the red LED for a second and then it comes back to the main
2:06
program checks button B and then let's assume again it's
2:12
if a button B is turned on or pressed it will then turn on the
2:19
green LED and when you finish that task it goes about repeat checking button a
2:25
and so on now you may have noticed that
2:33
there may be a chance in fact there is a great chance that button a and button B may not be
pressed
2:41
for a long long time so this is a very inefficient way of
2:49
achieving the task now interrupts now in the in with the
2:56
intro method um it will look at in fact the
3:02
microcontroller would ignore the two push buttons now when
3:07
it when the push button needs attention and if the let's assume the push button
3:13
a is pressed then only the add omega-3 to it will look at push button a so
3:20
let's refer to this uh block type uh well so the block diagram here so here
3:26
we have a main program which as before its main task is to keep the blue LED
3:33
turned on now let's assume also that an intro occurs now interrupt can occur
3:40
anywhere at any time so let's assume that there occurs here at this point now
3:48
the what the main program would do is to complete the current instruction that is
3:53
executing it has to finish that then branches off to a interrupt service
4:01
routine in ISR now the ISR is uh similar
4:06
to a subroutine it's a small program that completes a certain task now when
4:14
this ISR is completed it returns back to the main program
4:20
now now with this method the microcontroller
4:25
do not need to keep checking the two push buttons it will only attend to them
4:33
when it is pressed
4:38
okay let's consider how does an at Mega 3 to
4:44
8 internal interrupt work right here is the setup now in this
4:53
particular setup I'm going to this Arrangement here assume that I'll be
5:00
using the internal resistance a built
5:05
into the atmega three to eight pins all right so that it um without it
5:12
without showing this then this does not make make much sense so I have this uh
5:19
push button uh switch uh connected to the pd2 that
5:25
is port D B2 which is the int 0 or the interrupt
5:32
zero and over here we have
5:39
The Flash program memory now
5:46
I'm going to this I have got I have display here the block diagram of the
5:52
AVR architecture now the reason for doing this is to remind us where things
5:58
are so I just want to highlight The Flash program memory which is this
6:07
one here all right this block diagram of the flash program memory refers to the
6:15
flash program memory in the AVR what they call CPU
6:22
all right I just want to uh everyone to understand where this comes from now the
6:30
flash program memory uh contains the
6:35
main program now when the interrupt occurs notice that
6:42
the uh my the push button now is red and it's connecting the uh this completing
6:48
the circuit that will send a signal to the atmega328
6:53
this signal is the interrupt signal the interrupt would then signal would
7:00
then interrupt the uh execution of the main program so what it does is now the
7:07
interrupt signal will cause the main program to complete the current
7:12
instruction that is executing and then saves the address of the next is next
7:19
instruction onto the stack it dense it then after that it jumps to
7:26
location 0 0 to H now this is the
7:31
address of the vector address that is attached to the int 0. now this information is
7:40
contained in something called the interrupt Vector table I'll come to that later on now at this
address there will
7:48
be an instruction that will instruct the CPU to execute the ISR store
7:56
in the location in the flash memory so
8:02
it will execute that ISR
8:08
and then when that ISR is completed
8:14
it then Returns the control back to the main program let's take a closer look at
8:21
the vector address as mentioned earlier the quick reminder
8:27
when an interrupt occurs on pd2 that is the int 0 it's send a
8:37
signal to the admaker 328 and that's a hardware signal would then interrupt the
8:43
main program and it sends the uh the control to an address
8:50
uh zero zero zero two h uh
8:56
so that's where you'll find an instruction that will direct
9:01
the uh see the microcontroller to the ISR now zero zero zero to H is known as
9:09
the vector address then this address is uh tied in with int 0. this has been
9:17
defined in something called the interrupt Vector table
9:22
no for every interrupt as we've seen earlier there is a fixed address
9:28
location that contains the address of the ISR as demonstrated just a few
9:35
moments ago for INT 0 they address an inter uh addressed location would be
9:43
zero zero zero to H now the memory these memory locations
9:49
are put aside the whole addresses of the ISR it's called the intra Vector table
9:58
all right so the locations for the site for this addresses that those mem
10:04
location is known as the interrupt Vector table
10:10
so when an interim occurs the this the microcontroller of the processor jumps
10:15
to an address location which contains the start address of the appropriate
10:21
interrupt service routine the eyes are we've seen that in the earlier earlier
10:26
examples now um here is the intra vectors of the
10:33
eight Mega three to eight now I've taken all these information from the data
10:39
sheet of the eight Mega three to eight and this is table 11-1 reset and
10:45
interrupt vectors in the atmega328p now
10:51
I will be focusing only on the external interrupts so reset will have then
10:58
interrupt Vector of zero zero zero zero
11:04
hexa now int 0 as we've seen earlier is State
11:09
Vector address would be zero zero zero two hey hexa now there is an extra there
11:15
is a zero missing in this I think it's a typo error then there is the int one
11:22
that would have an address of zero zero zero four hexa
11:29
let's take a closer look at these addresses again the reset as mentioned earlier the
11:37
director addresses zero zero zero zero hexa
11:43
int 0 that as mentioned earlier that will be
11:48
zero zero zero to hexa and thirdly is the int one that that
11:56
Vector address would be zero zero zero four hexa
12:02
now I like to draw your attention to something called the pin change interrupt
12:10
now this is highlighted here in this red dot the box now pin change interrupt is
12:16
another form of hardware intro now unlike the interrupts that we
12:23
just mentioned earlier these pin change interrupt they are not
12:29
restricted to specific pins for example int 0 is dedicated to uh PD to Port D B2
12:41
now on the Arduino we know that it's pin two now so int 0 it is tied into a very
12:49
specific pin now for the pin change interrupt
12:54
they all the pins can be used for pin change interrupt any pins on the pots
13:01
any pots can be used for the pin changing drum and they are actually the
13:06
pin change interrupts are grouped into pots and all the pins on the same part create
13:13
the same pin change interrupt now
13:20
um let me just move on to the next so this is what I meant so you can group
13:27
your PIN change and drop into Port B and the or you can group them into Port D or
13:33
you can group the pin change into Port C now this is just to make you aware of it uh
13:42
I made the future do a video on the pin change uh interrupts
13:49
okay so far we've covered what topics like what is an interrupt how the
13:56
atmega3 to it interrupt works and so on so it's time to take a look at a
14:03
practical example now for this first example I'm going to
14:10
use this setup here so I have an Arduino Uno uh with pin 11 connected to a blue LED
14:19
now the cathode of the blue LED is connected to a 220 Ohm resistor taking
14:26
down to ground then on pin 10 of the Arduino Uno
14:33
is connected to Green LED and the castle of the green LED is connected to a 220
14:38
Ohm resistor and the other end is connected to ground now before the
14:46
switch here one end of the switch is connect well
14:51
push button switch is connected to pin 2 and the other end is connected to the 1K
14:57
resistor in turn it's connected to ground now this Arrangement here I'm
15:03
making use of the internal pull up resistor of the edmega328
15:11
now here is the what they call the wiring diagram on the
15:17
breadboard uh so fish here we have the blue LED connect the castle is connected to 20
15:25
ohm the anode is connected to the pin 11 time don't forget the ground from the
15:31
Arduino Uno to the breadboard and then the green led the handle is connected to pin 10
15:37
and the cathode of the green LED is connected to 20 ohm taken down to the
15:43
other end is taking down to the ground and here is the push button switch
15:50
Arrangement one end is connected to pin two the other end of the switch or push
15:56
button switch is connected to ground via the 1K resistor
16:03
now before we get on to the code itself let's take a look at the steps that is
16:10
required to program the atmega 3284 and interrupt so the first thing we need to
16:16
do is to set up the stack then we need to set the int 0 bit
16:23
all right uh of the external interrupt control register a the eicra
16:31
the in the int 0 bit in this register has to be set to a one
16:37
then next off we need to enable the external interrupt mask or the eimsk now
16:43
please take note of this acronyms here because this will be appearing in the
16:51
code later on and then set the next thing we do is set the global
16:57
intro enable bit or the I bit in the status register
17:03
that would be the sreg register
17:10
and as and for the requirements for example one the main program uh is to
17:18
keep this blue LED on and when an interim occurs that would be
17:24
generated by the the push button switch here uh that will send an interrupt signal to
17:31
the Arduino node which will trigger an ISR or interrupt service routine that
17:37
will switch on the green LED on for our short while so that would be the requirements for
17:45
this example one okay so here is the uh the code that
17:52
I've been talking about so this
17:58
let's let's take a look at this part of the code so the first line here is the
18:03
include directive that will include this file Then followed by a DOT Seaside directive
18:12
and followed by the dot org directive we did it with an address of zero zero zero
18:18
zero hexa now this is the location for the reset and in this location we have a
18:25
jump to main instruction so that would be executed and it will come down to
18:32
this part which is the main portion of the of the code
18:37
then next comes a.org0002 hexa now this address is the
18:46
location for the vector location for external interrupt zero and in this
18:53
location we have the jump uh instruction that says jump to this
18:58
ISR so the the name of the ISR is external
19:04
isr0 now this can be any other name it's entirely up to you I've chosen to call
19:10
it external isr0
19:16
now this part here is to set up the stack remember one of the steps in
19:22
setting up the edmega3 to it for interrupt is to first of all set up today uh stack and here you see
this Ram
19:33
end now RAM and is defined in the include file now this is the last
19:41
address in the SRAM in the AVR CPU now here
19:48
uh this Ram N is a 16-bit address
19:54
so what it does is this is these two instruction here will split the the
20:00
upper eight bits the high byte with this function the high function and
20:07
then copy that into R20 and then from R20 copy to the stack pointer High byte
20:14
then here we copied the low bite of the dress to
20:20
R20 and then from R20 we copy into the stack pointer low byte so this four
20:29
instruction will set up the stack
20:34
so next comes the uh the setup for the external inter control
20:41
register a so we load 0 2 0 2
20:46
is the hex code to make the int 0
20:53
into a falling Edge trigger all right so you use this code this hex
21:01
code here copy that into our 20 from R20 we call we copy this to the e i CRA which is the
21:12
external interrupt control register a so we use the STS instruction to do that
21:18
that is SDS is the store direct to data space instruction now this instruction
21:25
has been explained in my previous video next we need to enable the int 0 so we
21:34
need this binary code zero zero zero zero zero zero zero one that is hexa
21:41
zero one copy that into RR into R20 from R20 we copied it out into the external
21:49
interrupt mask the e i m s k
21:55
and then here we enable the global interrupt I and the I bit so we use the
22:03
SEI instruction now more of the SEI instruction later
22:11
now come to this point here we have the SBI
22:17
Port D comma 2 that would activate the
22:23
bit 2 in Port d um remember I mentioned that I'm making use
22:30
of the internal cool lock resistor of the AVR pins now each of the AVR pin in
22:38
the AV each of the pin in the AVR CPU has an
22:44
internal pull up so act to activate it we need to set it to our logic one so
22:49
this is what I'm doing here so activate that then these two instructions here should
22:55
be familiar with those who have been following my video this is to set Part B Direction and
register to output mode so
23:04
we set Port B to output mode with this two instruction
23:10
now coming to these two instructions here we basically set Port B bit three that
23:17
is connected to the blue LED that would then uh switch on the blue LED and here
23:24
the jump instruction basically basically will repeat it so this two instruction will keep the blue
LED on
23:35
okay so let's take a look at the interrupt service routine the ISR now
23:42
the name of these eyes are as mentioned earlier are giving it an external
23:48
isr0 now as mentioned earlier you can name this any other name right for now
23:55
is external isr0 so the first line here basically copy
24:01
the contents the byte and point B to R16 and then when we come to this part these
24:08
two instruction basically uh we do an exclusive or
24:14
or X or here to toggle uh bit three
24:20
of Port B so that would be in binary zero zero zero zero zero one zero zero
24:27
translate that to the hex code is zero four hexa copy this into uh 17 and then
24:34
we carry it out at xor with the contents of R16 and R17 the result of the xor or
24:44
exclusive or would be stored in R16 now these two lines here basically will
24:49
toggle uh the LED which is the green LED no sorry
24:57
yeah it is the green LED uh on Port B bit three so if it's a zero it will it
25:06
will turn that into a one if it's a one it would turn it into a zero and then output that result to point
B
25:14
and then Define the instruction in the inter service routine in fact
25:19
the last instruction in any ISR routine is the reti that is returned from intro
25:26
this will then read this instruction will return the control back to the main
25:32
program okay so much for the code
25:38
um before I go any further there are a few things I would like to clarify before moving to the uh
the code demonstration
25:46
now I'm going to use uh information from the data sheet of
25:52
the atmega3 to 8p now this data sheet you can download it
25:59
from this link here I have actually provided a link in the description and I
26:07
highly recommend you download the data sheet is very useful
26:13
now I'm going to use this state information from the data sheet to
26:18
explain the external interrupt control register a or the e i c r a remember
26:26
that acronym we have covered that earlier and then I'll need to also to explain the external intro
mask or the e
26:36
i m s k and also the status register the
26:42
sreg okay first off let's take a look at the
26:48
eicra or the external interrupt control register a e i c r a
26:58
now they it this is the uh information I've taken out from the data
27:05
sheet from section 12.2.1 I've extracted that part of the data
27:12
sheet here so let's take a look at it so this is a the register is an 8-bit
27:19
register and um they all
27:24
what we are interested in it is four bits here zero one two and three now
27:30
zero bit zero one and bit bit zero and bit one okay it's known as the isc00 and bit one
27:40
is known as the isc01 now ISC stands for inter sense control and the description
27:48
of these bits are given here in table 12-2
27:56
now there are four ways of of
28:02
full control that we can have and I'm only interested
28:10
in this control here where we need to set isc00
28:16
to logic 0 and isc01 to logic one then this will give us a falling edge of the
28:24
int 0 to generate an interrupt request
28:29
because um the reason for doing that because in the first demonstration on the first
28:35
example I'll be using the int 0.
28:41
now in section 12.2.2 we have the e i m s k or the
28:50
external interrupt mask register again this is an 8-bit register
28:58
we are on there are only two bits that we should concern ourselves with that is a bit zero and bit
one if you look at
29:06
Bit Zero that is the int 0 and bit one is the int one now when I
29:15
and t0 is set to a one we set to a one then we will enable the
29:24
external uh pin uh for interrupt
29:30
all right so for this uh code that we're using I'll be using int 0 and this is
29:37
pin two on the Arduino Uno and this is the int1
29:45
I'll be using this one in the second example later on in the video
29:53
now the next one that I the next thing that I want to clarify is the remember
29:58
the there's an instruction in the code that it says se1 SEI that stands for set
30:06
enable in Trump now that comes from the status register or the sreg now the sreg
30:16
or the status register is an 8-bit register each bit has a particular
30:22
specific function I will not go into uh that here what I'm interested is this
30:30
most significant bit here this is the eye bit of the intro bin so
30:35
when we execute this instruction this is taken up from our code so SEI is a
30:42
standalone instruction with no operand when you execute this instruction you
30:48
will then set this bit to a one and that would enable the intro
30:53
it's only when you enable the interrupt that the uh
31:00
that the instruction following the SEI will be executed
31:06
uh I've taken this is not taken up from the data sheet this is taken from another uh
31:12
well another set of instruction that is the AVR instruction set manual here's
31:18
the link uh for that document again I've included this link in the description
31:27
demonstration one okay so we're now in the atmel studio 7
31:35
so I uploaded the code here uh let's just quickly go through this so we have
31:41
the include directive here the dot csac directive the dot
31:49
org0000 hexam uh this is the vector address for reset and then we have
31:57
in that location they jump to main instruction so it comes down here before
32:03
we do that then there's the dot org0002 hexa that's the vector address
32:09
for external interrupt zero and there we have an instruction that says jump to
32:15
external isr0 then we have the setup the stack here
32:21
and we have the uh enable the external interrupt control
32:29
register a making the int 0 to do a falling Edge
32:37
trigger then we have a enable interrupt int 0
32:44
here using the external interrupt mask
32:49
then we set the I bit to enable the global interrupt here this instruction
32:55
will activate the internal resistor for Port D bit 2
33:03
then this was set up called B data direction for output port
33:10
switch on the LED connected to bit three for Part B that's the blue LED so that
33:16
will set it on all the time then finally this is the internal isr0 eyes
33:25
are routine so this would toggle a port B bit 2 which is the green LED that you
33:33
will see later on so this part here will do the toggle and then here this
33:39
instruction will get it back to the main program here so
33:44
um let's now build the thing solution and we have no
33:50
errors successful compilation now let us
33:56
now try to look at the here's my setup so
34:03
I would zoom in here close up and go to tools
34:10
program it here it's all right so the blue LED is switched on
34:17
so if I were to interrupt the program by pressing this
34:24
push button degree there's a switch bounds problem here yep
34:32
that turns it on turns it off let's try again
34:39
on off
34:45
on off again switch bounce problem and it's
34:53
off again and this blue LED is on all the time because that's the function of
34:58
the main program all right so this is the uh end of this
35:06
demonstration okay let's take a look at another
35:11
example I'm going to call this example example two
35:16
and here is the setup for the for example two so I have an Arduino Uno
35:23
uh with the blue LED connected to pin 11.
35:29
a red LED connected to pin 6 and a green LED connected to pin seven now there are
35:37
two push button switch here the this one here is connected to pin
35:44
three and this other push button is connected to pin two
35:50
now um the arrangement here assume that I'm
35:57
using the internal pull-up resistance resistor in the atmega 3 to 8.
36:08
right here is the wiring diagram let me just briefly go over it so the blue LED
36:15
is connected to pin 11 then the red LED is connected to pin 6
36:21
the green LED is connected to pin seven push button a is going into pin two and
36:29
push button B is connected to pin three so this is the wiring diagram
36:36
now these are the requirements for example two so the main program will blink
36:44
the blue LED continued continuously then if push button a is activated or
36:52
pushed or pressed sorry sorry push button in here then the green LED will toggle meaning if it's
uh on
37:02
then it'll be off and then if you press it again if it's off it will turn on so
37:08
it toggles it similarly for push button B it toggles
37:14
the red LED so these are the requirement requirements for example two okay so
37:23
this is the program or the code for this particular demonstration and
37:29
this program is actually almost identical to the earlier program except for a few additions and
modifications so
37:38
this is let's take a look at this one here so as usual we will include this file
37:45
all right and then we have the dot C site directive then we have the dot o r
37:51
g which uh have an address of zero zero zero zero hexa remember this is the
37:57
vector address for the reset and this location will have a uh instruction here
38:03
that says jump to main so the bush is this part here Then followed by a DOT
38:13
org00020002 hexa is the vector address for the external interrupt zero and then
38:20
at that location we'll find a job instruction to a ISR called external
38:27
isr0 now this these two lines here is
38:32
virtually identical to the previous example then the addition here is the
38:39
dot org 0004 hexa this is the vector address for
38:44
x external interrupt one and here we have an instruction that says jump to
38:50
external isr1 so this is the addition of modification
38:57
to the to the previous code then we come to this part remember to program
39:04
the edmega3284 interrupt so the first thing we need to do is to set set up the
39:09
stack again this is identical to the previous video so next we need to uh make int 0 and int
39:21
one uh set them to a falling Edge so with that we need to make use of the
39:27
external interrupt control register a sorry a register a and then we need to
39:35
copy this value 0 a hexa translate that to Binary that would be zero zero zero
39:42
zero one zero one zero now this last four bits here will set the
39:49
interrupt uh what we call sense control the ISC the four Inc bits
39:58
uh related to int 0 and int1 you need to refer back to the data sheet or to the
40:07
uh previous code you see the details there
40:13
then next up we need to um
40:18
enable int 0 and int1 now you know that we need to make use of the external
40:25
interrupt mask So within there we need to put in this binary value zero zero zero zero zero
40:33
zero one one that translates that to hexa is is zero three
40:40
all right so this will then uh enable this to interrupts
40:48
next we have the s e i instruction basically this will enable the global
40:55
interrupt by setting the I bit this I bit can be found in the status register
41:01
as mentioned earlier in my earlier code that we did for example one
41:08
then the next two instruction which is this two
41:15
write this to SBI will set up this uh
41:20
uh Port D B2 and Port db3 activate the
41:25
pull-up internal resistor all right this will set the
41:31
internal resistor for these two bits to how uh to activate it sorry then
41:41
this portion here basically will blink
41:46
the uh LED connected to this port B bit three
41:52
that is the blue LED on the breadboard and in between there
41:58
is a call delay to slow down the blink blinking rate so
42:04
that we can see it and here is the delay subroutine that we
42:12
mentioned earlier in in a few moments ago now the details of this a delay
42:19
subroutine can be found in my previous video that is video 58 blinking an LED
42:26
using the edmega 328 Assembly Language now I've included this link in the
42:33
description below please check it out if you want to understand this Loop
42:42
then finally we come to this two ISR that we mentioned earlier so this is the
42:50
internal isr0 that's the name of this ISR and it's
42:56
identical to the previous code that we've seen except that this time I'm going to Blink
43:01
the sorry not blink to Target that led connected to Port D bit six this is the
43:08
red LED and this toggle it these are the these are the two instructions that will
43:13
do the toggling part then this part here is virtually
43:19
identical to this except we are using bit 7 for the green LED again this is the part
43:27
that toggles the the green LED all right so if you look at it carefully
43:34
it's identical except for the bits that are used
43:40
to bling oh sorry to toggle the LEDs
43:46
demonstration too okay so uh we're in the atmel studio
43:52
seven so I've uploaded the code here uh this let us quickly just go through
44:00
this so I have the dot C file here then the org for the location
44:08
for the reset and then the external interrupt zero
44:14
uh here and then the dot org for external intro one
44:21
uh then we have the setup the the stack here
44:26
enable external interrupt control register a
44:32
enable the sorry the external mask interrupt mask then finally the I bit
44:40
enabled I bit the global interrupt then we set up the
44:47
or activate rather they pull up resistors then this portion here is to
44:53
Blink the blue LED so we set it uh on
44:58
here delay then switch it off then delay and then here is the delays of routine
45:07
and then this is these are the two ISR so this one will toggle
45:14
this ISR here will sorry this eyes I will will toggle the great LED
45:20
and then this here will toggle the green LED so
45:26
um let me just upload it to the uh
45:32
what it call like let me just see
45:38
yeah let me just upload this all right let's just build it first
45:44
so we have no errors made successful then we go to this part here before we
45:51
do that let me just see if we can all right okay so
45:58
we have here the
46:04
you can see the board here so let me just upload the code
46:10
here it's uploading and you got the blue LED blinking
46:15
so this one here will shoot rather
46:20
turn the would toggle the red LED
46:26
right it's on now press it again it goes off so it toggles the red LED let's do
46:33
it again oh there's some bounce problem here
46:39
all right so it's on and it's off so that earlier uh on off
46:47
effect of this Ray LED is due to the bounce of this switch that is a different problem we are
46:53
probably make a video on how to debounce switches later on now this early this
46:59
switch here should toggle the green LED
47:04
again this bounce problem it's quite bad all right cool now it's on
47:12
it's off let's try it again
47:17
on off so that's not too bad so this is the
47:23
blue LED that's the main program uh flashing it now blinking it rather and
47:29
so that's it for this particular demonstration
47:36
okay that's it for this video I hope you have found this video useful I have also
47:44
included the code that I've used in this video uh in the link uh in the
47:50
description thank you for watching and I'll see you in the next video bye