How to Blink an LED – Part 6 Microcontroller Basics (PIC10F200)

Okay, so last time, we finally got this microcontrollerprogrammed, 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 tobe a little bit more complicated than if we were doing this in Arduino or something likethat, 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 howdelay loops work and figure out that structure, so that we can, because we're going to usethat all over the place.

And that's really what we need to do is weneed to turn it on, delay, turn it off, delay, turn on, delay, turn off, delay.

So first, I want to go through and introducesome of the new assembly instructions that we're going to be hitting and so I'm goingto go through this line by line and then I'm going to go back and talk about the structureand and what is actually happening from a higher viewpoint.

So with that, let's get straight into thecode.

You're going to see that basically the firstnine lines of code we've already discussed, you already know how to configure it, youknow how to initialize it, and you can see from the move literal to working registerthat 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 10to 15.

So under 10, you can see where it says loop.

As much fun as it'd be to say, a loop, allI 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 cansee BSF, which is Bit Set F.

So it's saying basically in the GPIO register, bit GP2 – setthat 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 thatGP2 of the GPIO register and clear it or turn it off.

And then you're going to call that delay inline 14, before hitting 15 doing that GOTO loop and bam, you're up at the top and you'redoing that loop forever and ever.

That's pretty straightforward.

So it gets a little bit more complicated oncewe actually get into the delays.

So let's go down and we're going to look atsome 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 that162 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 intoa register F.

In this case, it's register 10.

And then we do the same thing where we copythat 162 in line 20, to register 11.

So we're getting everything set up.

And then in line 21, we're actually doingthe delay loop itself.

So this is a new command.

This is kind of a long, weird, random lookingone DECFSZ – decrement F, skip if zero.

Now that's actually pretty straightforwardif you take it apart completely.

So what you're doing is you're decrementingF, in this case, the operand is 10, register 10.

So you're decrementing whatever value is in10.

And then checking, is that actually a zero? Or does it still have a number?  So if we start at 162, and we're decrementingto 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 goingto 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, Igotta go back up to the top, goes back up to line 21, jumps down to 22.

takes that 161changes to 160 says, hey, it's still not zero jumps up to 21 does that over and over andover again.

Now, you'll notice in line 22, the secondoperand is F.

And the reason it's doing that is because it's taking that F register, it'sdecrementing it and then putting that value back into the F register.

That is something where you could take thevalue, 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, loadinga 161 and the working register, and it would have absolutely no point whatsoever.

So since we're doing that, and we're goingthrough 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 jumpto line 24, where it's the exact same command, but instead of reducing the value in register10, we're doing it in register 11, which is also 162 right here.

So, now this is where it gets a little bitmore complicated because it's going to go through and that 162 and it's going to goall the way down to zero.

And then it's going to jump down and it'sgoing to take that 162 in register 11 and decrement that to 161 then it's not goingto 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'sgoing to go check, decrement file register 10, which is zero, and then check to see ifit's zero again.

But the way computers work, since this isan eight bit microcontroller, it goes zero to 255.

So if you do zero minus one, it's not negativeone, it's 255.

So what's going to happen is, you're goingto have that zero, you're going to decrement it, and it's going to pop up to 255, it'sgoing to check and say 255 isn't zero.

So it's going to do that loop all the waydown 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 timeswith 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 allhappening very, very fast.

And this is all busy work.

This is something that is just telling themicrocontroller to do something to give it time to be able to say all right, That's theamount 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, andyou've been returned.

Now you're done, you go to line 15, you loopback 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 ofthis 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 register10 so many times before you decrement register 11 at all.

And it just seems weird at first, it seemslike a whole lot.

But again, it's happening so fast, that youhave to do it whole lot of times to make it slow enough that, as this is turning on andoff, we can actually see it.

Really quick, I do want to mention why wechose 162.

So Sergey figured this all out and when hesaid, I want this to oscillate a couple of times a second.

And this is not very precise, we don't needto have it exactly a certain speed, we just need it approximately an eighth of a secondon, 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 had162 times 255, times the three cycles that it takes to do the GOTO, because the GOTOinstruction takes two cycles, and the decrement takes one cycle.

And also take the fact that each cycle, youcan run a million cycles per second, all of those added together comes out to about 125milliseconds, approximately 125 milliseconds.

So that's why we chose that 162.

Now again, it's not going to be precise, becausethe 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'tneed that sort of precision right now.

In a couple of tutorials, we're going to bemaking music using a speaker.

And then you need to get that pitch exactlyright.

And that's when it's going to really matter.

But for right now, we're not worrying aboutit.

So I'm going to load this really quick andshow 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 turningon and off is waiting huge amount of time, according to the microcontroller, turningit 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 itso you have one of these blink on, and while the other ones off, this one turns off, thatone's on and basically have them oscillate.

Now, I highly recommend that you do this.

So as you're listening to this, hopefullyyou've been following along, you have all of these parts.

We tried as hard as we could to make thisas inexpensive and easy to follow as possible, because only by doing it, are you going tolearn 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 myselfand not just following along with these tutorials, that all of these commands really sunk in.

And I got a better intuitive understandingand 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 learnit.

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 tolearn.

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 thecomments on YouTube or on our website CircuitBread.

com and we'll try and get back to you as quicklyas possible and we'll put it in one of her FAQs, subscribe to our channel and we'll seeyou in the next one.

.

Leave a Reply