#58 Blinking An Led Using The Atmega328 Assembly Language
#58 Blinking An Led Using The Atmega328 Assembly Language
0:00
hello I welcome
0:02
in this video I'm going to show you how
0:05
to Blink an LED using the edmega 328
0:09
Assembly Language
0:12
introduction
0:15
now this is the
0:18
main program on the main code that I'll
0:21
be using to demonstrate uh how to Blink
0:25
an LED so this
0:28
part of the code that is in the rate red
0:31
dotted box is the main program
0:35
and um
0:37
and here at the bottom here
0:41
this is the what we call the delay
0:45
subroutine now the delay subroutine is
0:48
necessary in this program because we
0:52
need to slow down the microcontroller so
0:55
that we can see
0:57
uh the blinking
0:59
effect
1:02
so I've extracted the the main program
1:05
out and uh
1:08
there's one instruction
1:10
that I would like to highlight because a
1:13
lot of the uh well a number of the
1:15
instructions here uh you might be
1:18
familiar with it because I've used them
1:21
in my previous videos now this one
1:24
instruction that I'm not used
1:27
before that is the CBI instruction which
1:32
is highlighted here now I'll come back
1:34
to this in a few moments
1:37
now
1:39
this is the cold subroutine and here is
1:43
another call to the subroutine I'll get
1:45
back to the uh delays of routine details
1:49
uh later on in the video
1:52
so let's take a look a closer look at
1:56
the CBI instruction so CBI stands for
2:00
Clear bit
2:02
in an IO register so the format is here
2:08
so a CBI again let me just uh reiterate
2:12
that this CBI can be uppercase lowercase
2:16
uh it's not case sensitive so CBI a
2:21
comma B now a is a register from uh
2:26
ranges from
2:28
0 to 31 so this could be register 0
2:32
register one Resistance 2 and so on so
2:36
it's between 0 to 31. now the B is the
2:43
bit in that particular register so the
2:47
bit could have a value of 0 through 2 7
2:51
2 it could be bit 0 bit 1 bit 2 and so
2:54
on and the maximum is bit 7 which is the
2:58
eighth bit
3:01
so here uh I've highlighted the CBI
3:05
instruction again so in this case if you
3:07
notice the i o register is Port B and
3:12
the bit that we are interested is in
3:15
point B is Bit Zero
3:20
so uh with this instruction CBI Port B
3:24
comma zero that would clear bit 0 of
3:29
Port B that means uh Port B bit 0 will
3:33
be set to a logic zero okay so next I
3:39
would like to take a look at uh into the
3:42
details of the delay subroutine now
3:45
before I get into the details of the uh
3:48
delay subroutine let's take a brief look
3:51
at what is a subroutine
3:55
now a subroutine is a sequence or a
3:58
group of instructions that is written
4:01
just once but executed
4:04
uh when it is needed by the main program
4:09
let's see how this works out with this
4:13
diagram here so here I have
4:17
representing a main program with some
4:20
instructions and there is a call to a
4:24
delay subroutine
4:26
all right this call is a 8 Mega three to
4:31
eight instruction so here it will call a
4:35
delay
4:38
so that when this instruction is invoked
4:42
or called
4:43
it would then leave this main program
4:46
and execute a subroutine
4:50
and um there will there may be several
4:54
instructions in the subroutine depending
4:56
how the sub how long the subroutine is
4:59
uh once the subroutine has been executed
5:03
or completed the last instruction of a
5:07
subroutine is an ret that is the return
5:12
to the main program or the subroutine
5:15
will hand the control Bank
5:18
to the main program basically that's how
5:23
a subroutine
5:25
works out in a program
5:31
all right so I've extracted again here
5:35
the main portion of the main program of
5:38
our example and I just like the
5:41
Highlight the code to the delays of
5:46
routine which is this one the
5:48
instruction here and the other
5:51
call is here
5:55
so
5:56
um let's take a look at the core uh
6:00
instruction basically this is a
6:04
edmega328 uh assembly instruction you
6:08
will find similar instructions in many
6:12
of the other types of uh
6:15
microprocessor or microcontroller it is
6:18
quite a common instruction but for the
6:21
add Mega three to eight this is a long
6:24
call to the subroutine what this means
6:26
is when this is executed k
6:30
actually represent the range of memory
6:34
addresses that it can reach out to so
6:37
for the 8 Mega 3 to 8 the call
6:41
can call a subroutine within a address
6:45
zero all the way up to 64 K of addresses
6:51
that's that's uh how far
6:54
how far it can reach for a subroutine
6:59
but notice in our code or program
7:03
they call uses a label rather than an
7:09
address now in this case delay the label
7:14
delay points to the start of the
7:17
subroutine and this is uh quite common
7:22
okay now let's take a look at the delays
7:25
of routine that is being used in a video
7:29
now this is the delay subroutine that is
7:33
that I'm going to use and I'm going to
7:35
get into the details of this subroutine
7:38
now
7:40
now before I actually get into the
7:42
details of the subroutine let me just
7:44
take a look at something called a single
7:47
Loop
7:49
now the single Loop
7:51
um the flowchart for the single Loop is
7:53
shown here and here is the code
7:57
of using the edmega328 assembly code to
8:02
implement this Loop basically what this
8:05
single Loop would do is to set up a
8:08
counter
8:09
now
8:10
[Music]
8:10
um
8:11
the counter will be given as some
8:13
initial value
8:15
as is shown here ldi at r21 so r21 would
8:21
be the counter this can be any other
8:23
register it doesn't have to be r21 and
8:26
so the initial value is 255. so we go
8:29
back to this flowchart here so we
8:32
decrement the counter by one I mean
8:35
subtract it by one and then ask the
8:38
question is the contents of the counter
8:41
zero if it's not zero go back and
8:45
subtract or decrement the counter by one
8:48
again now this will go on for a little
8:52
while until the contents of r21 is equal
8:57
to zero when that happens the loop will
9:01
end and it comes out of the loop and
9:04
basically this is how it's been
9:06
implemented so you decrement r21
9:10
and subtract one from it check it is the
9:14
count is r21 equal to zero if it's not
9:18
go back and do it again so this brne
9:21
Loop one will go back here so this will
9:25
go on for a while until R 21
9:29
which is the counter uh is the contents
9:33
of r21 is 21 is zero it will then exit
9:36
this Loop and ends the loop
9:40
now there's two uh
9:43
instructions here that I want to take a
9:45
closer look uh that is the deck and the
9:49
prne instruction so first off let's look
9:52
at the brna brna stands for Branch if
9:55
not equal
9:57
so brne this is the format and K
10:00
although K in our program is a label so
10:05
K is actually the offset that you can
10:09
have the offset of -64 or all the way to
10:13
plus 63. basically these are the number
10:16
of locations
10:18
there's no such thing as the minus
10:20
location but what it means is a backward
10:24
uh Branch or backward backward jump this
10:29
is a forward jump so this is the number
10:31
location it can jump to
10:35
so
10:36
um
10:37
now to simplify this thing I've used
10:41
label instead of using these values here
10:44
all right as as you have seen it in our
10:47
example
10:50
the next one as explained earlier is the
10:53
deck basically this subtract one from
10:57
the register in this case the register
10:59
that we have used in the example is uh
11:02
21 so D is the number of the register so
11:08
this could be from Z register zero all
11:12
the way up to register 31. another
11:15
example as I'm repeating here is the we
11:19
have used r 21.
11:22
okay so
11:24
um we are in the at mail Studio 7 and
11:28
I've
11:29
created a new project uh to demonstrate
11:33
the single Loop
11:35
um
11:36
function
11:37
so here I have the code
11:40
I have slightly modified the uh the code
11:43
in that uh the setup during the setup
11:47
counter are only giving a three to the
11:52
counter which is our register r21
11:56
and then that's the only modification
11:59
that I made
12:00
so let's come build the solution
12:07
right so it's the it's successful
12:10
there's no errors now let's go to debug
12:13
and start the debugging session
12:17
now for this uh let me just close this
12:20
thing for the for this we need to go to
12:23
debug
12:25
yeah
12:27
and to Windows and they look for
12:30
processor status click on that and we
12:33
have the processes status we scroll down
12:36
until we see the Regis status register
12:41
all right there are several bits or
12:43
several Flags I'm only interested in the
12:46
in this flag here the zero the Z flag
12:51
right so we've got everything set up we
12:54
got the registers uh I mean right now
12:57
register uh 21 is zero zero hexa
13:01
so let's
13:03
go through the program so the first
13:06
thing and I'm going to sing a step is to
13:08
execute this instruction here now we
13:10
should watch r21 which is this one here
13:14
all right
13:15
so watch r21
13:18
we have zero three so because that's
13:21
what we have instructed the program to
13:25
do copy this value into r21 so we have
13:28
in r2103 and now the second instruction
13:32
will
13:34
subtract 1 from r21 so we should expect
13:38
to see a 0 2 here let's see if we are
13:41
right
13:42
yes it's a 0 2 in r21
13:47
then this BR B R and E instruction would
13:51
check is the contents of
13:53
r210a all right if it's not zero we need
13:57
to go back to this point here uh
14:01
Loop so let's see if it does that
14:06
it does so now
14:09
um r21 as you notice is 0 2 so we
14:13
subtract 1 from mid again
14:16
so it's 0 1 ask the question is it zero
14:19
yet it's not go back
14:23
all right we go back
14:26
yeah and we go back and this time
14:29
uh we're going to subtract another one
14:32
but this time watched R 21
14:35
we should see a zero
14:38
yes is zero zero ask the question uh is
14:42
it it's r210
14:45
yes it should come out of this from this
14:48
Loop if you come up from this Loop and
14:50
go down here let's see if it does that
14:54
and watch out
14:57
for the status register now notice the
15:00
status register now is highlighted in
15:02
red the Z flag indicating that the
15:06
result is zero so now
15:09
uh
15:11
it should come out of this Loop and come
15:13
down to this part here all right let's
15:16
see if it does that
15:19
yep and it does
15:21
now quite often the single Loop may not
15:25
be sufficient and this is because
15:28
microcontroller or microprocessor they
15:31
operate in the megahertz range
15:34
so this is way too fast uh for the uh
15:39
human eye to to see so we need to turn
15:43
attention for a uh to nested loops
15:49
now nested Loops are also known as a
15:53
loop within a loop so here is example
15:57
I have two counties uh Outer Loop and
16:01
the inner loop let's take a bit of
16:04
closer look at the so-called inner loop
16:08
if you notice the inner loop is the
16:10
single Loop that we have just uh looked
16:14
at earlier so in the Inner Loop counter
16:17
which is r21
16:19
we say give it a value
16:22
decrement that counted by one if it's
16:25
not zero go back and do it
16:27
so it will do it 255 times decimal
16:31
once this is done it comes out
16:34
and it
16:36
it would then decrement this second uh
16:39
of sorry this outer loop counter which
16:42
has been set up here with the initial
16:45
value of 255
16:47
so it would take
16:49
subtract one from the R22
16:53
and then ask the question is it zero yet
16:56
if it's not zero if R22 is not zero go
17:00
back to Loop two now look at Loop two
17:03
Loop 2 will then reset R 21 which is the
17:07
out in the loop counter with a
17:10
value of 255 and then this repeats again
17:15
255 times decimal
17:18
and then it comes when this is done it
17:20
comes out and then take uh uh sorry to
17:24
subtract one from R 22 ask the question
17:27
is it zero yet it's R22 0 yet if not go
17:31
back repeat this again
17:34
all right so let me highlight this outer
17:37
loop so now we have a loop that is
17:40
within another loop
17:43
all right so this inner loop here uh
17:47
will be repeated as many times uh
17:50
determined by
17:52
the value of the Outer Loop
17:56
right we are in the atmel studio 7 and
18:00
this time what I want what I want to do
18:02
is to demonstrate the double Loop or the
18:05
nested loop the loop within the loop
18:07
program so I've uploaded the program
18:11
here so let's go to build
18:15
and built the solution and there are no
18:18
errors it's successful so the next thing
18:21
we need to do is go to debug
18:25
all right so now it's in debug mode now
18:28
there's two panel set that we need the
18:31
register panel and also the status of
18:35
sorry the processor status and in a
18:37
processed processor status we look for
18:41
the status register all right which is
18:45
this role here
18:47
switch back to the register panel first
18:50
so
18:52
um quick look so we got two counties
18:55
here R22 is for the outer loop r21 is 4D
19:00
inner loop which is this one here
19:03
all right so let's
19:05
start off with the uh
19:08
first counter for the outer loop so
19:12
let's see load decimal two to R20
19:16
register R22
19:19
yes it so R22 is here and it's now got
19:24
zero two
19:25
next one is the load 2 into r21 which is
19:29
this one here
19:31
all right now right now it's zero zero
19:34
yes it's got zero two which is good
19:38
now the next instruction will decrement
19:40
or minus one for r21 so watch r21
19:46
it's now zero one that's correct and
19:50
then it comes to this check point here
19:53
which asks is the contents of r21 equals
19:57
to zero no it's not all right so it will
20:00
go back to Loop one which is this one
20:02
here all right so yes and it will
20:08
decrement r21 r21 at the moment is uh
20:11
zero one
20:14
all right so
20:16
now it's zero zero
20:18
and if you go to the processor status
20:20
look at the status register the Z flag
20:24
is it's red that means it's set to one
20:26
indicating the result is zero so a one Z
20:31
equals to one means there is
20:34
the result is zero right so now it will
20:38
come out this Loop drop down to the next
20:40
instruction which is this one here
20:43
all right so let's do that right now R22
20:48
at this moment it has
20:52
a value of 0 2 so we decrement that
20:57
here it's going to a 1 it's R 22 0 yet
21:02
no so it will go back
21:06
to Loop 2 reload r21 with 2 because
21:10
right now R20
21:13
r21
21:15
is 0 0.
21:18
all right so watch r21
21:21
yes it's reload with two so next
21:25
instruction decremented by one
21:28
it's r21 0 yet no not yet go back and
21:32
subtract one again or decrement one from
21:35
r21 and watch out for r21 here
21:41
all right before we do that let's look
21:44
at the status register right uh the
21:47
status register that flag is now uh
21:52
a zero it's clear so let's see what
21:55
happens now we take one away from and
21:58
now it's zero zero r31 is zero zero
22:02
let's go to this process and he sees the
22:04
exact flag is set to one telling us that
22:07
the result is a zero so now it drops
22:10
down to the next instruction
22:14
all right now R22
22:17
it's in a moment uh zero one
22:21
and the status register is still not
22:24
clear yet but watch out when I execute
22:28
this one
22:31
R22 is zero
22:36
and the status register is still set to
22:39
a one so it says it is 22 equals to zero
22:45
yeah it says yes
22:46
so it comes down and the whole thing
22:49
ends all right so this is how the nested
22:54
Loop works
22:58
now
23:01
sometimes even with the
23:04
um
23:05
a double loop as we've seen before a
23:08
loop within the loop that may not be
23:10
still not enough because for example the
23:14
edmega328 a microcontroller operates in
23:18
this at 16 megahertz
23:22
so
23:23
the uh double Loop that we had seen
23:26
earlier may not be sufficient so we need
23:30
to turn to
23:31
this method or this technique where we
23:36
have this uh inner loop as seen earlier
23:40
which is implemented here
23:43
all right in the atmega328 code
23:47
and then we have the outer loop which is
23:50
in the blue box
23:54
this is the repeat of the double loop as
23:57
we've seen earlier and then on top of
23:59
that we add in another loop
24:02
all right so this is this Loop here will
24:06
be controlled by this Loop here and this
24:09
two Loops here is controlled by this uh
24:12
uh what we call what I call the
24:14
outermost outermost Loop
24:18
so uh this is a loop within the loop
24:22
within a loop
24:24
now and this is the
24:27
subroutine the list of routine that we
24:30
have used that we are using in our
24:33
example here
24:37
so I'm repeating the the code here uh to
24:42
complete
24:44
uh the explanation so we have
24:47
the main program here
24:50
all right the main program here
24:53
and this first two lines basically Set
24:57
uh
24:58
Port B to output mode
25:01
and then this will set a port B bit 0 to
25:05
1 call the delay
25:09
when the delay is complete it will clear
25:12
uh bit 0 or Part B the mid-sided uh to
25:17
off State call the delay again
25:21
and then
25:23
when that is done it comes back here
25:25
when it says our domain goes back switch
25:29
the LED on call the delay clear
25:34
the Bit Zero call the delay and then
25:38
jump back set the uh the uh bit 0 to 1
25:43
again and it just keep repeating and we
25:47
will get a
25:48
blinking LED
25:51
on a port B Bit Zero
25:56
right
25:58
um to test the the program or the code
26:00
we require some Hardware
26:03
and here's the Circ circuit for the uh
26:08
interface to the Arduino Uno basically
26:12
it consists of a 220 Ohm resistor
26:17
and one end of the 220 Ohm resistor is
26:21
connected to pin 8 of the Arduino Uno
26:24
which is this one here
26:28
and
26:30
pin 8 is basically Port B and bit 0 of
26:36
the atmega328
26:38
microcontroller the other side of the
26:41
resistor which is this part here
26:45
this part here that's connected to the
26:48
anode of the LED
26:51
and the cathode of the LED which is this
26:54
part here is connected to ground
26:58
of the Arduino Uno
27:01
and basically here is the wiring diagram
27:04
for this schematic here
27:07
here is a picture of the actual setup
27:12
as you can see there's the red LED it
27:15
doesn't have to be red it could be any
27:16
color of your choice that's the 220 Ohm
27:19
resistor and the and is connected to pin
27:23
8 of the Arduino Uno and this black wire
27:26
here connects to ground of the Arduino
27:29
Uno
27:30
okay so we are back into the ml Studio 7
27:33
and I've uploaded the uh complete code
27:38
that will link the LED so this is
27:42
basically a copy of the code that we
27:45
have been discussing in earlier so let
27:49
me just build the uh
27:51
solution
27:54
and it's successful there's no errors so
27:58
now what I'm going to do is we're going
28:00
to upload it to the hardware and see if
28:05
the things if the thing works now before
28:07
we do that let me just try and
28:12
set up the hardware and get the camera
28:15
to focus on it this gave me a minute
28:19
all right so here we have the code here
28:23
and the hardware setup so let's go to
28:27
tools
28:29
and um let's upload the program to the
28:33
Arduino Uno
28:38
yeah and we as you can see now the LED
28:43
is blinking
28:45
let me just see if we can focus it
28:49
better on this thing
28:53
um sorry uh
28:55
the video cam
28:57
for the video cam quality but as you can
29:00
see the LED is now blinking
29:04
so the thing is working
29:07
hey so we have come to an end
29:11
of this to this video
29:13
I hope you have found this video useful
29:16
and like to thank you for watching and
29:20
I'll see you in the next video bye