#59 Blinking LED - Alternative Delay Subroutine
#59 Blinking LED - Alternative Delay Subroutine
0:00
hello and welcome in this video I'm
0:04
going to show you how to write an
0:07
alternative time deleted routine to
0:10
Blink an LED using the edmega 328
0:14
Assembly Language
0:16
introduction
0:19
now in my previous video I've used this
0:22
program or code to bling an LED and this
0:27
is the time delay routine that I use
0:30
now in this video I'm going to rewrite
0:33
this time delay subroutine but before we
0:37
do that let's take a brief look again at
0:40
this time delay routine
0:45
so here is the flowchart for the time
0:50
delay subroutine and here's the code
0:53
that implements the implements the uh
0:56
time delay subroutine so this is the uh
1:02
in a loop counter
1:05
and this is the code that implements the
1:08
inner loop counter
1:11
and
1:13
this is the outer loop
1:16
counter
1:18
and this is the code that implements the
1:21
outer loop counter and finally we have
1:24
the outermost Loop counter
1:29
and this part here that's that is
1:32
highlighted in green that is the code to
1:35
implement the outer loop outermost Loop
1:38
counter
1:41
okay
1:42
so let's take a look at the revised
1:47
blinking LED program
1:51
so this is the re revised program that
1:56
I'll be using for this video it is
1:58
pretty much
2:00
the same as the as the program that I've
2:04
used in my previous video except for a
2:07
few minor changes which I'll point up to
2:09
you in a few moments and the new
2:13
uh subroutine delay subroutine
2:18
I have removed the comments from the
2:22
code so that it's easy to read the code
2:25
and so that I can fit them into one
2:27
slide
2:30
so
2:32
um this are the directives which are
2:35
highlighted here so dot include Dot
2:40
csag and Dot o r g I've covered this
2:44
directives in my previous videos so I'll
2:48
focus on the DOT def directive which are
2:53
this three code here
2:56
now I have extracted the DF directives
3:01
here for discussion purposes
3:05
now the dot d f is an assembler
3:10
directive
3:12
DF stands for defined
3:16
now it can be used
3:20
to give registers
3:23
meaningful names
3:26
so in our examples are in our case
3:30
are register r18 for example is given a
3:34
name outer loop counter and register r24
3:39
is given the name
3:41
inter in sorry inner counter low and
3:46
register r25 is given in a counter high
3:51
now these names are entirely at the
3:55
discretion it's up to you what names you
3:59
want to give to these registers
4:03
um but my advice is give them meaningful
4:07
names
4:11
now I've highlighted here the main
4:13
program uh the first two lines in the
4:17
main program is ldi
4:20
R17 comma 0 xff and then out ddrb comma
4:27
R17 basically these two lines uh they
4:31
will set uh Port B to output mode I've
4:35
covered this in my previous video now
4:38
the SBI Port B comma 0 then we'll set uh
4:43
Port B bit 0 to a high and then
4:48
CBI Port B comma 0 that would set a Part
4:53
B bit 0 to a logic zero and then we have
4:57
these two call delays here that will
5:01
invoke or call the subroutine delay
5:05
and then our jump here basically that
5:08
would Branch back to this point here
5:11
so this is the main program
5:15
and here finally we've come to the new
5:19
time
5:21
delays of routine that I mentioned
5:23
earlier
5:25
right let's now take a closer look at
5:28
the new delay subroutine
5:32
and I have extracted the uh time delay
5:36
subroutine here uh for discussion
5:40
purposes I have also highlighted the
5:43
bits that are new and they are the low
5:48
and high here and the s b i w
5:53
instruction
5:57
so let's take a look at these two lines
6:00
of code
6:02
which uh which I highlighted here
6:05
and I'm repeating I repeated those two
6:08
lines of code here highlighting the new
6:11
bits
6:12
now
6:13
the purpose of a low and high
6:18
uh functions they
6:20
are there to allow us to work with
6:25
numbers
6:26
greater than
6:28
255 decimal and that would be
6:32
FF in hexa so if you want to work
6:36
uh with numbers greater than this
6:39
then what we need to do is to use two
6:43
register two registers together a pair
6:46
of registers so that it will give us a
6:49
range from uh values or numbers uh
6:54
ranging from zero decimal and hexar that
6:58
would be zero zero zero zero because
7:00
because these are 16 bits number all the
7:03
way up to 65
7:06
535 decimal in hexa that would be f f f
7:12
f
7:15
now
7:17
the inner counter low and the inner
7:21
counter High has been def was defined an
7:25
earlier part of the program so the inner
7:29
counter low is being assigned with the
7:32
registered r24 and the inner counter
7:37
High has been assigned with register r25
7:43
by using this information I can rewrite
7:46
these two lines of code replacing the
7:49
inner counter low
7:51
an inner counter high with these two
7:54
registers like so
7:57
all right so uh
8:00
so what happens here is that
8:02
the this 16-bit number here which is
8:07
written in hexa
8:09
the low byte which is highlighted in
8:11
green
8:13
will be assigned to our register r24 and
8:17
the high byte which is highlighted in
8:19
yellow that would be assigned to a
8:22
register r25 now this assignment of
8:28
um
8:30
high low and high bikes is being done
8:35
by the assembler not the microcontroller
8:39
now let's take a look at the s b IW
8:44
instruction now sbiw stands for subtract
8:49
immediate from word and the format
8:53
is shown here so it's sbiw Rd colon Rd
9:00
plus 1 comma k
9:04
now let's look take a look at this uh D
9:08
which is highlighted in red now D is an
9:11
element
9:12
of from this set of numbers
9:17
all right it has to be this set of
9:19
numbers that is uh 24 26 28 and 30.
9:26
nothing else different
9:27
only these numbers so D is an element
9:30
from this from this set of numbers
9:33
so for example if you choose to use r24
9:39
so the first one will be r24 then the
9:43
next one after the colon would be uh R
9:46
24 plus 1 that would be r25 so
9:51
r24 colon r25 goes together that would
9:55
be the register pair these are the two
9:58
registers that you'll be using
10:00
then if you choose to use 26
10:04
r26 for example then the next one after
10:08
the colon will be r26 plus 1 that would
10:11
be r 27 so r26 colon r27 that would be
10:17
another pair and if you choose 28 for
10:22
this r28 and then after the colon are 28
10:26
plus 1 that will be r29 so r28 colon r29
10:32
goes together that would be another pair
10:34
and if you choose 30 that would be R30
10:40
colon R30 plus 1 there will be R 31 so
10:44
that would be another pair
10:47
so coming back to our example
10:51
sbiw inner counter low comma one now in
10:55
the counter low was assigned R
11:00
24. now this was done in the early part
11:04
of the program
11:06
uh so what I've done here I've extracted
11:09
that part of the program to remind
11:11
ourselves
11:12
if you remember we have a DOT def inner
11:17
counter low and give giving it at the
11:22
register r24 so if we go back to this
11:26
instruction here what this instruction
11:29
means is that uh it will subtract one
11:34
from
11:36
inner counter low which is
11:39
our register r24
11:46
okay now that we have uh looked at the
11:50
details of the delay subroutine let's
11:53
take a look at the delay subroutine
11:55
subroutine together with the flowchart
11:58
so we have the code on this side and we
12:01
have the flowchart on this side now
12:03
these two lines of code
12:05
is represented by these two blocks here
12:10
all right and then we have this block
12:14
here highlighted in blue and the code
12:17
for these two
12:18
blocks here it's been implemented here
12:22
which is basically the sbiw in account
12:26
uh low comma one and then BR and e in a
12:31
loop
12:32
which is what this point here
12:34
so it checks for zero if it's not zero
12:38
we keep going back to spiw keep taking
12:42
one from the inner counter low
12:47
and then finally this would be the outer
12:52
loop counter and this is the check of
12:55
the outer loop counter which is
12:58
implemented in the uh at Omega three to
13:01
eight code which is highlighted here
13:08
oh okay so
13:10
let's take a look again at the entire
13:13
program so I repeated the entire program
13:15
here so this part here basically that is
13:20
the directive
13:23
our directives and then this part here
13:27
is the main program and this first two
13:30
lines basically will set up the uh
13:35
Port B for output mode and then SBI uh
13:40
will set point B bit 0 to 1 called the
13:44
delay
13:45
and then CBI will clear Port B bit 0
13:49
call the delay and then do an odd jump
13:52
back to the main so it will keep doing
13:55
this and then finally we have the delay
13:59
subroutine which we have been through uh
14:03
in details earlier so let's take a look
14:06
at the code demonstration now
14:11
okay so before we look at the code
14:14
demonstration let's take a quick look at
14:16
the hardware that is used uh in this
14:19
video
14:21
now the hardware
14:22
is the same as the one used in my
14:26
previous videos
14:29
so basically
14:32
so here is the circuit diagram or
14:34
schematic so uh
14:37
basically it's a 220 Ohm resistor in
14:41
series with an LED and one the one end
14:44
of the resistor is connected to pin 8 of
14:48
the Arduino Uno uh that is basically
14:51
Port B bit 0 on the atmega three to
14:56
eight microcontroller and the other end
14:59
of the
15:01
LED
15:02
will be taken to ground
15:06
and here on this side would be the
15:09
wiring diagram
15:12
of this schematic
15:15
so you have a 220 Ohm resistor in series
15:19
with a LED
15:21
cathode of the LED is connected to
15:23
ground on the Arduino and the resistor
15:26
is connected to pin 8 of the Arduino Uno
15:32
now so here's the a picture of the
15:35
actual setup
15:37
so that's basically the hardware
15:41
the demonstration
15:44
okay so now we are in the atmel studio 7
15:50
and I've uploaded the uh the documents
15:53
the code here
15:56
uh so you can see this is the same code
15:59
that we have been uh discussing earlier
16:03
so I don't think that's uh it doesn't
16:07
need to go through the code
16:09
um it's just that I'd like to point out
16:11
so there are I've had in that put in the
16:14
comments here in the demonstration code
16:17
so because this is my usual practice I
16:21
highly recommend you do that uh in all
16:24
your code
16:25
so uh let's so let's now proceed to uh
16:31
let's see go to build
16:34
and build solution
16:37
and we have no errors so that is
16:41
successful
16:43
let's go down to uh wait a minute let me
16:46
just adjust this so that I can bring on
16:49
the uh my video camp
16:52
just give me a minute
16:54
yes right so so here is my Arduino Uno
17:00
with the LED connected to Port B Bit
17:03
Zero the bigger pattern so go to tools
17:09
and program the Arduino and you see the
17:14
red LED now is flashing uh
17:18
as a proof as evidence that the
17:22
code is working
17:24
all right so uh so much for the
17:27
demonstration
17:29
hey so that's it for this video I hope
17:32
you find the video useful and thank you
17:35
for watching and I'll see you in the
17:37
next video bye