So in the last tutorial, we learned how to make this LED blink on and off.
And hopefully you did your homework and you made it, so you could make them alternatingly
blink.
If you did not, please stop this video, go back, practice that and learn it, because
we're going to be learning even more commands.
And it's going to get even more complicated here.
And so if you don't have a good grasp on what we did in the last tutorial, you're probably
going to get lost in this one.
So if you haven't, go back and do that, if you have, awesome, let's move on.
So this time, we are going to be creating basically a software PWM, pulse width modulation.
And what we're going to be doing is we're going to be taking the brightness of this
LED, and slowly ramping it up until it's fully bright, resetting it starting again, bright
and restarting over and over again.
So we're going to be using a lot of the same ideas and principles that we used from the
last tutorial in terms of that delay and the nested loops and all that sort of stuff.
But it's going to be a little bit more complicated.
Actually, I better say first, if you don't know what a PWM is, then please go check out
our tutorial on PWM's, it's going to make a lot more sense.
And also, we have done a separate tutorial on PWMs with LEDs a`nd so just go check those
things out if you're not completely clear on PWMs, duty cycles and all of those sorts
of things.
And I'm going to struggle because...
PWM's... and that's going to be a fun one.
So I'm going to say that wrong at least a couple of times, couple of times during this
tutorial, let's get into it.
We start at the very beginning and we configure our program like we do before, but there's
something unique and new on lines four through six on this and that is "i EQU 10", "limit
EQU 11", and all those sorts of things.
Now this is kind of like the equivalent of a defined function.
It's making it so it's easier for us as the readers and the writers to just say, 'i' instead
of register 10, or 'limit' instead of register 11.
After watching the previous tutorial, you probably noticed that I was getting the 10
and 11 register all mixed up and confused.
So this is one way of making it so you don't do that.
And that is all.
It does not matter at all to the microcontroller, it is purely to make it more human readable.
So as we continue on, you'll see that we initialize things, we set up GP1, one as an output, and
we get all that and then line 11, we get a new command, and that is CLRF.
And so that is basically "clear the value in register F".
That's a pretty straightforward one.
So in this case, we are taking the limit register and making sure that it is zeroed out.
And then we jump straight into a loop.
Now there's these nested loops here.
So you have the first loop that also includes what we've called the internal loop.
And so within that first loop, we set our 'i' as FF and then we turn on our GP1, so
we start it on and then on the internal loop, what we're basically doing is we're going
to be dividing a delay so the way a PWM works is you have it on for a short period of time
and off for a short period of time and you vary that duty cycle.
So sometimes it's on longer or it's off longer, but the overall length of that on/off period
is the same, you're just changing the ratio of on/off within that.
And so we're doing that same thing here on the internal loop portion to change that ratio.
So in line 17, we have a new command it is MOVF.
Now MOVF is interesting, because basically what it's doing is it's taking the register
that you give it as the first operand and then either putting it into the working register,
or back into the same register.
Now in this case, in the second operand, we've put "W" so we're basically taking what is
in the register limit and putting it into "W".
Now you may say, Well, what would be the point of taking that and putting it in itself?
And that is purely because as you do that, it changes the status bit so if you want to
check to see if there's a value of zero in the limit, you'll move "f" into itself, and
then check your status bit and if the zero bit is set, then Okay, then that is a zero
or it's not a zero.
So that's really the only point for that.
So line 17 we are putting the register limit into the working register and then we are
subtracting w so in this case, because we can't do that direct subtraction, so we are
taking the W and subtracting it from 'i', which is one of our counters.
Once you do that, again, you're affecting the status register and you're going to either
have it be zero or not zero.
So if limit and I are the same, then after line 17 and 18, they will subtract and they
will be zero.
And then in 19, where you do the BTFSS 'bit test F ss' - another exciting one - Bit Test
in File register Skip if Set - you're checking to see if when w or limit was subtracted from
I that the status but the zero bit was actually zero.
And if so, you skip.
If not, perform the next operation.
So I always get those confused because sometimes like wait, skip if set skip if zero, which
one is it?
Make sure you get that straight.
I have to do it every single time I have to double check to make sure I am thinking through
it properly, there's no shame in it.
Once we get better, we'll probably not have to worry about it.
But for right now is we're still learning this, take your time, make sure it's clear
in your head.
So once we make sure that we understand that that bit test in file, register, skip, if
set and we're understanding how that works, we will check on this one.
So the case here is we're checking to see if the zero bit and the status register is
set, in essence, whether or not I equals limit, and if it is, we skip line 20 and go to line
21.
So it is not then we go to 20.
And then on line 20, we have that GOTO $+2.
Now the dollar sign is the program counter.
And basically, if you're at 20, it's saying if you said GOTO $ and not the +2, it would
just repeat and go back to itself all the time.
But it's saying instead of executing this line, add two lines.
So instead of executing this line, you're going to jump two lines.
So it's basically another skip command.
So if we hit 20 what we're doing is we're skipping over that clearing of the GP1 bit
and GPIO.
So basically, if the limit does not equal I, then we will go to line 20, which will
then have us skip the bit clear F. So in that case, we do not turn off the LED at that time,
but we continue on we hit a delay, we decrement I, and then depending on whether or not that's
a zero, we either execute the internal loop again, or if that does equal zero, then we
move on, we decrement the limit and then go back and start the entire loop over again.
So this is complicated, and it's very strange, it's difficult in the assembly does not have
a quick "if this equals this, then do that" in C or some other higher level language that's
very, very simple.
Assembly, it requires a little bit more... strangeness to do it.
I still struggle trying to figure out exactly how this works and make sure that I'm not
saying it backwards, as I'm saying to you, so please, please, in this particular case,
I highly, highly recommend going to CircuitBread.com checking out this written tutorial.
And making sure you fully understand these few lines, basically lines 17 to 26.
Everything else is pretty simple in here.
But make sure that you know how those lines all go together to create this functionality.
Once you do that, you should be golden because that's really it.
Once we're there, the rest of this is done.
We've already done DELAY loops in the previous tutorial.
We know how that works.
It's not a big deal.
So with that admonition, let's program this and I'll show you what it's supposed to look
like when you're done.
And then we'll talk about the homework assignment that Sergey has left for all of us.
Okay, it's downloading, blinked a little bit.
So now you can see the LED is gradually getting brighter and brighter and brighter turns off,
brighter and brighter and brighter, turns off and that is what it should look like.
So if you do this correctly, that is exactly how your circuit should be acting like now.
The homework is, and that Sergey has given to all of us, is to make it so that not only
does it get bright, and then just get dark, it gets bright and then it gets dim.
And so not necessarily a heartbeat type of getting brighter and dimmer, but just a bright,
dim, bright, dim.
And frankly, if you can do that, you will have a really good intuitive understanding
of how all of this works.
And it will do a huge amount of good for you in terms of actually understanding this and
being a better programmer.
Hopefully this is helpful.
Again, I highly, highly recommend you go and you either watch this again, or you go to
the link in the description below and go to the written tutorial for this and see all
of this in greater detail and then you'll be ready to rock on.
In the next one we are actually going to be creating some music and that's going to be
very exciting.
But for now, I really hope you enjoyed this video.
I hope you're enjoying the series.
I really enjoy the series and I've really enjoyed working with Sergey and asking questions
and making sure that things are clear.
So please, please come in jump on and ask any questions on YouTube, on CircuitBread.com.
We'd love to help.
We'd love to make any clarifications if something is not clear for you right now, as it is like
this video, subscribe to our channel.
We'll see you in the next one.