Okay, so last time, we finally got this microcontroller programmed, and we got the LED turned on,
and that was awesome.
Now, we're going to make it blink.
Since we're using assembly, it's going to be a little bit more complicated than if we
were doing this in Arduino or something like that, because you have to do everything very
manually.
But at the same time, it's not that hard.
So we're going to go through this.
And we're going to learn mostly about how delay loops work and figure out that structure,
so that we can, because we're going to use that all over the place.
And that's really what we need to do is we need to turn it on, delay, turn it off, delay,
turn on, delay, turn off, delay.
So first, I want to go through and introduce some of the new assembly instructions that
we're going to be hitting and so I'm going to go through this line by line and then I'm
going to go back and talk about the structure and and what is actually happening from a
higher viewpoint.
So with that, let's get straight into the code.
You're going to see that basically the first nine lines of code we've already discussed,
you already know how to configure it, you know how to initialize it, and you can see
from the move literal to working register that we're enabling the GPIO and also setting
it as an output.
And so all that sort of stuff should be straightforward.
Now, the actual program itself is from 10 to 15.
So under 10, you can see where it says loop.
As much fun as it'd be to say, a loop, all I have to do is put in the loop command, that's
actually not a command, that's just saying, "This is the loop section."
So from 10 to 15, that is a loop and you can see BSF, which is Bit Set F. So it's saying
basically in the GPIO register, bit GP2 - set that bit.
So, basically turn it into a one.
Then you're going to call that delay routine, then you're going to have Bit Clear F in line
13 BCF and then you're going to take that GP2 of the GPIO register and clear it or turn
it off.
And then you're going to call that delay in line 14, before hitting 15 doing that GOTO
loop and bam, you're up at the top and you're doing that loop forever and ever.
That's pretty straightforward.
So it gets a little bit more complicated once we actually get into the delays.
So let's go down and we're going to look at some of these commands.
And in line 18, we have Move Literal to W. And that's something we've already discussed,
where you take a literal, an actual number, and put it into the working register, the
W register.
And in this case, we put decimal 162.
We'll go over that afterwards.
First, I want to go through this, but that 162 isn't a completely random number.
And the next line is where we do MOVWF.
And that is where we copy the value of W into a register F. In this case, it's register
10.
And then we do the same thing where we copy that 162 in line 20, to register 11.
So we're getting everything set up.
And then in line 21, we're actually doing the delay loop itself.
So this is a new command.
This is kind of a long, weird, random looking one DECFSZ - decrement F, skip if zero.
Now that's actually pretty straightforward if you take it apart completely.
So what you're doing is you're decrementing F, in this case, the operand is 10, register
10.
So you're decrementing whatever value is in 10.
And then checking, is that actually a zero?
Or does it still have a number?
So if we start at 162, and we're decrementing to 161, it'll check to see, oh, it's been
decremented, it's 161.
It's not zero.
And then, since it's not zero, it is not going to skip the next line.
So since it's not going to skip the line, it's not going to skip line 23, since it's
not zero, it's going to go - 23 say, Oh, I gotta go back up to the top, goes back up
to line 21, jumps down to 22. takes that 161 changes to 160 says, hey, it's still not zero
jumps up to 21 does that over and over and over again.
Now, you'll notice in line 22, the second operand is F. And the reason it's doing that
is because it's taking that F register, it's decrementing it and then putting that value
back into the F register.
That is something where you could take the value, decrement it, and then load that new
value into the working register.
But we want to keep it in that F register.
So it keeps on decrementing.
Because if we loaded in the working register, it would just be going from 162, loading the
161 into the working register 162, loading a 161 and the working register, and it would
have absolutely no point whatsoever.
So since we're doing that, and we're going through that one, six to go back through through
through, finally, we're going to hit zero, and we're going to skip line 23.
When we skip line 23, we're going to jump to line 24, where it's the exact same command,
but instead of reducing the value in register 10, we're doing it in register 11, which is
also 162 right here.
So, now this is where it gets a little bit more complicated because it's going to go
through and that 162 and it's going to go all the way down to zero.
And then it's going to jump down and it's going to take that 162 in register 11 and
decrement that to 161 then it's not going to be zero, so it's going to go to the Delay
Loop bumping up to the very top.
Now once it goes up to the very top, it's going to go check, decrement file register
10, which is zero, and then check to see if it's zero again.
But the way computers work, since this is an eight bit microcontroller, it goes zero
to 255.
So if you do zero minus one, it's not negative one, it's 255.
So what's going to happen is, you're going to have that zero, you're going to decrement
it, and it's going to pop up to 255, it's going to check and say 255 isn't zero.
So it's going to do that loop all the way down from 255 to zero again, before going
down, checking register 11, saying, "Okay, now I'm at 161, drop it to 160," jump all
the way back up to the top loop 255 times with 10, go down to 11.
do that one more time.
10, 255 times down to 11.
And it's going to do that a lot.
And since this works so quickly, this is all happening very, very fast.
And this is all busy work.
This is something that is just telling the microcontroller to do something to give it
time to be able to say all right, That's the amount of time I want.
And once it goes through all of those loops, all those times, then it jumps back up to
line 14, where you've called the delay, and you've been returned.
Now you're done, you go to line 15, you loop back up to line 10, you do the Bit Set F,
call that delay, go through all that rigmarole, go down to Bit Clear F, call DELAY, do all
that rigmarole, go back up to the loop.
And that's what it's doing.
So that's basically the entire structure of this program.
And again, that delay, it can be kind of confusing, because it's all nested in there.
And because you have to count that register 10 so many times before you decrement register
11 at all.
And it just seems weird at first, it seems like a whole lot.
But again, it's happening so fast, that you have to do it whole lot of times to make it
slow enough that, as this is turning on and off, we can actually see it.
Really quick, I do want to mention why we chose 162.
So Sergey figured this all out and when he said, I want this to oscillate a couple of
times a second.
And this is not very precise, we don't need to have it exactly a certain speed, we just
need it approximately an eighth of a second on, eighth of a second off, so we can see
it.
And that's really it.
This is not exactly an eighth of a second.
But in general, we realized that if we had 162 times 255, times the three cycles that
it takes to do the GOTO, because the GOTO instruction takes two cycles, and the decrement
takes one cycle.
And also take the fact that each cycle, you can run a million cycles per second, all of
those added together comes out to about 125 milliseconds, approximately 125 milliseconds.
So that's why we chose that 162.
Now again, it's not going to be precise, because the first time we do that, register 10 decrement,
we're starting at 162 instead of 255.
So, this is a bit hand-wavy, but... we don't need that sort of precision right now.
In a couple of tutorials, we're going to be making music using a speaker.
And then you need to get that pitch exactly right.
And that's when it's going to really matter.
But for right now, we're not worrying about it.
So I'm going to load this really quick and show you what you should be seeing when you
get this.
Okay, so it's loading up right now programming.
And that's it.
So that's all you get is that blinking LED.
So all of this is every time it's turning on and off is waiting huge amount of time,
according to the microcontroller, turning it on waiting a huge amount of time, turning
it off, back and forth, back and forth.
And it's just an endless loop.
Sergey, when he did the written tutorial, he put a homework assignment in there.
And that homework assignment is to make it so you have one of these blink on, and while
the other ones off, this one turns off, that one's on and basically have them oscillate.
Now, I highly recommend that you do this.
So as you're listening to this, hopefully you've been following along, you have all
of these parts.
We tried as hard as we could to make this as inexpensive and easy to follow as possible,
because only by doing it, are you going to learn it, and intuitively understand it.
Because, honestly, Sergey put this together, I did it.
Okay, I kind of learned some things.
But it wasn't until I was doing it myself and not just following along with these tutorials,
that all of these commands really sunk in.
And I got a better intuitive understanding and everything started to click.
So, he likes to give these homework assignments, because he's a former professor, and he understands
the necessity of actually doing it, to learn it.
So before you move on to the next tutorial, once you get this working, and you get that
LED blinking, make it make your own modifications, so that you can have both of them blink, alternating.
You can go check, because he's got the solution, he's got A solution for doing that.
And you can see if yours is similar to his, it doesn't have to be, it can be better can
be worse, whatever.
As long as you're doing it, you're going to learn.
All right, I'll get off my soapbox now.
If you like this video, give it a like!
If you have any questions, leave it in the comments on YouTube or on our website CircuitBread.com
and we'll try and get back to you as quickly as possible and we'll put it in one of her
FAQs, subscribe to our channel and we'll see you in the next one.