Welcome back to the next PIC10F200 basic microcontroller tutorial, this is going to be the first time
we actually load a program on there and turn on an LED.
So I don't know if you know but in the embedded world if you come from the computer science
programming world, turning on an LED and making it blink is kind of the Hello World equivalent
of doing something in C or Java or whatever.
But since we're not going to make it blink, this time, we're just going to call it the
Hello program.
Let's get started.
If you did the last tutorial, you should be set up you should have your project and a
main.asm file ready to go and your MPLab IDE and your circuit should be all set up and
your PICKit or ICD or whatever you're using should all be connected and all we should
need to do now is loading the program and upload it with all of that if you want to
grab the code either from our website circuit bridge com Or we're going to flash this code
up on screen either way, I recommend that you actually type in this code line by line
because it will make it stick in your head a little bit better.
But I am going to go over it line by line so...do whatever feels natural for you.
So first, let's get into assembly.
Assembly is made up of basically four different parts.
So first you have labels, which is just something you put in there and it labels a section of
the code.
And it's partially for you to understand what's going on and for you to know what your particular
sections are doing.
It's also useful for the code itself, because you can reference it within the code and we'll
get to that not in this program, but in the next program.
The other thing is mnemonics, which are also known as operational code, op code or instructions.
Now, mnemonics is a way where you take a word and you shorten it up and use the first letters
to make it represents something and you'll see why that is because there are certain
codes here that, for example, decrement file register, skip if zero.
You don't want to type that out every single time.
So they came up with a shortened code, so that you can type that in, you know what it
means.
It actually has some sort of meaning to you, but it's not this great, long, crazy thing
that you have to type in.
Every single time I like to refer to them as instructions, because to me that is more
intuitively what they are their instructions that tell the microcontroller, what to do.
But you can hear them as opcode or mnemonics or whatever.
The third thing is operations, or basically the parameters that you give your opcode or
your instructions.
And so when you have an instruction, it's saying you're typically wanting to move something
or to calculate something, that thing that you're going to want to move or calculate
is going to be different every time.
And so with that different parameter operand, that's how you make that up code or instruction
do exactly what you want to do.
So that's the third thing you need to worry about.
The fourth thing is comments.
Comments are super easy, because that is purely something for you, or whoever is going to
be looking at your code to be better able to understand what your code is trying to
do.
I don't know about you, but if you've ever written code and think and this is perfectly
clear, and then for some reason you don't touch it again for six months to a year and
you come back and you look at it and you think what in the world was I doing?
Here comments and my experience help, don't make it completely clear, but at least help.
So, kind of, Okay, this is this is what we want to do.
So using an IDE is very beneficial.
In reality, you could do all of this coding in text editor or something, and create the
make file and do everything in a command line.
And it could be much more customizable, and it'll be exactly what you want.
But that is pretty challenging.
I've done that in the past, and I learned a ton.
But it is very nice to have an IDE that creates the infrastructure around it and simplifies
a lot of things.
Though, if you do it manually, it does help you understand when there are problems in
the IDE.
And I bring this up in particular, because if you look at the code, you can see that
different things are different colors.
You notice everything after a semi colon is a comment and it's actually in green.
And that helps give you more subtle clues.
But when you're looking at something, first of all, what it does, and second of all, if
it's not the right color, it gives you an intuitive.
Something's wrong here.
Oh, I put a colon instead of a semi colon, those sorts of things.
So let's look at this code.
It's only 11 lines, we should be able to go through it pretty quick.
And then we're going to load this up, and then we're going to be done.
And then next time, we're going to start making that LED blink.
But first, let's go through this line by line.
So the first thing we see on the first line is pound include in that in quotes p10F200.inc
So basically, what this is doing is this is telling the IDE and the compiler, that there
are certain things that have already been predefined, to make your life easier.
As you go through, you'll see references to GP1, and you'll see config and all these sort
of things where that would mean absolutely nothing if it weren't for the fact that somebody
already went in and said, if you're working with a PIC10F200.
Then you have four GPIO, three GPIO and one inputs.
So when somebody says GP1, that means they're referring to this particular pin, and that
makes it a lot easier for you to just throw in some things instead of having to go to
the data sheet and say okay, GP1 is this bit in this Register.
So that means I need to do this.
And it really simplifies things.
And also, it's one of those things where if you say, hey, I want to send something to
GP5, this knows a PIC10F200 doesn't have five GPIO.
So what are you talking about, and it'll throw up errors to tell you, hey, this isn't something
you can actually use.
So that's important.
If you don't have that, it's not going to work.
And the second line is just a comment that says, Okay, now we're going to do the configuration.
Third line, you have those double underscores config.
And what it's telling you here is you need to set up the microcontroller in some ways.
And none of this is absolutely necessary for our current setup.
But because we didn't want to have to worry about the watchdog timer or code protection
or anything like that.
We configured this so the watchdog timer is off.
Otherwise it will reset itself every once in a while, and we'll get into watchdog timers
later, but we also don't need the code protection because we're turning on an LED there's there's
nothing to protect here and then the MCLR off that just means that we can can use that
pin that has MCLR for a GPIO if we want and we won't we use that as a reset pin.
So online for is kind of interesting whenever I see ORG, I think organization something
like that, but that actually means origin.
So what we're saying is that we are now at the beginning of the program that is actually
going to be executed.
So here is where we start number five, this is where we see our first label.
So "init" or initialization, this is basically telling us as ourselves it's almost like a
comment in a way that this is where we're going to get the microcontroller set up, ready
to go number six.
This is the most complicated line in the entire program here the first time I saw something
setup like this, I just had no idea what was going on.
What's the tilde what's the one that I just didn't know.
So first, let's break this into two parts.
So the MOVLW is our first mnemonic or first opcode that we need to worry about and that
is move literal to W, W is working register as we've talked about in a previous tutorial.
And the strange thing about assembly is you can't just say I want to multiply this by
this or I want to do this, you have to take one number, stick it in the working register,
and then you can take that working register and multiply it or deal with it with other
registers.
For example, if you do want to multiply something, multiply this register by that register, but
you need to take one of those and put it into the working register first.
So what this MOVLW is is taking a literal or the number that you give it as the operand
and puts it into the working register.
Now, the operand in this case, the parameter is that tilde one and then the two "less than"
signs and then GP1.
Now as I mentioned, GP1 is given to us as a specific code is how the compiler knows
exactly where which bit to move.
So in this case, just happens to be that GP1 is in bit one, so when you go from zero to
seven, in the GPIO register, it is bit one, it's saying, let's put a one in this register.
Let's shift that register over the amount of spaces where you need to go to fill that
gap one bit.
And then let's take all of that and not it.
And that makes it so instead of being a bunch of zeros, one, zero, it's a bunch of ones
and 01.
Let's go over that really quick again.
So the tilde, if you are not familiar with bitwise, operations, takes all ones turns
into zeros and all zeros and turns into one and then that left shift with that double
less-than sign takes whatever you have in shifts it to the left, what we're doing here
is we're simply putting a one in at the very right hand side, shifting it to the left enough
so that it fills that gap, one slot and then inverting them all with this, you can look
at it and intuitively know oh, I want to put a zero at GP1 and leave everything else as
ones.
So even though this may be a lot more complicated originally.
Once you get used to it and it's more intuitive, it's going to be a lot easier to do.
So line seven you have TRIS GPIO now this is an instruction that it always takes GPIO
as an operand.
TRIS stands for TriState.
So that refers to the fact that it can be a high voltage, low voltage or it can be a
high impedance, those are the three states that a pin can take.
And so in this case, what it's doing is it's saying take the value that is in the W register
that we just put in there from the previous state and load that in here into the GPIO
register.
And so we set that and so that's how we can say of all these things, GP1 is going to be
an output because it is a zero and we're good to go.
Line eight we have BSF This is our next mnemonic that stands for Bit Set F so as we know a
set bit makes a pin high and a clear but makes a pin low.
And so as we do this, we take bits FNGPIO take that register and bit GP one and set
it so the two opposite ends RGPIO which again is the registered and GP one which is the
bit within that register and set that particular bit in that particular register and that is
the actual line the turns on our LED, the following three lines are really simple.
So loop and line nine, that just is another label and then number 10, it's GOTO loop.
So you will learn in any programming class that in C, c++, Java, Python, whatever GOTO
is terrible, you never want to use it, it's going to cause all sorts of problems.
In assembly, that's not the case you use GOTO quite a bit.
And in this case, GOTO loop just basically tells the program that once you hit that line
10 to go back to the label of loop, which is line nine, and it's going to do that it's
going to go Oh, go to line nine.
Oh, go to line nine.
And it's going to do that forever and ever and ever and ever.
Because that's all we want is we turn on the LED.
Now we're done.
Line 11 just says END.
And that's just a way for the program to know this is the end of the code, and it's done.
And if you don't have that, you're going to have some sort of error.
So that's it, that's probably going to be pretty fast and I'm hoping those explanations
will help you get a better intuitive feel highly recommend going back and reading the
actual tutorial on our website because then you're going to have a little bit more time
to read through it and let it absorb.
And don't worry if it takes two or three times through for all of these things to make sense.
But now that we have this setup, let's load this up.
So as you may have noticed, I'm actually on the MPLAB Xpress, I'm on the online IDE.
And this is because Mac OS decided that it wanted to update and I didn't think twice
about it.
And in so doing it broke MPLAB X IDE.
And so now, it is a real pain.
But I'm going to show you approximately how you're going to do it in MPLAB Xpress.
And hopefully they'll fix it in the real MPLAB IDE so that we don't have to go through this
every time.
But typically, you just come up and there's this button right here and it shows the box
and an arrow down to the microcontroller.
You hit that button and it not only builds it but then it automatically programs it and
if you've done everything successfully, then it just goes but because this is being a pain,
it just built it and saved it as a hex file and then I had to download MPLAB IPE. go in
here and now I need to connect...
That's going to take a minute.
See, all these things that I'm doing in the IPE, the integrated programming environment
are actually done inside of the IDE.
So ... um.. don't care about that.
See, now it's connected.
And it doesn't save your settings either.
What a pain.
Like we go, let's switch over to power really quick power target from PIC, go back to operate,
try and connect again.
So again, it's much simpler when everything's working through the IDE.
But if for some reason something doesn't work, and MPLAB decides it doesn't want to work,
you can switch it over to the online version, and then build your file there and upload
it here manually.
It's just a bigger pain.
So let's browse our download.
So for the middle, there we go.
And then we click program.
Again, all of these things are done automatically and the IDE if everything's done properly
and everything set up properly.
So that makes it super easy.
Now we're done.
So all of that work, all that explanation and those 11 lines of code getting through
building it, uploading it on the IPE.
And now we have this beautiful shiny green LED.
Now, this is a wonderful moment.
And this is just the first of many, many projects that you can do.
And this is our Hello program.
Hello, we are working, we're able to put the code in, build it, download it and have it
actually do what we wanted to do.
So do not do not think that this is not important because this is important.
And I hope that this is something that has been really helpful for you.
I hope this is getting you really excited for the future projects we're going to be
doing because we've got a lot of cool stuff coming down the pipeline.
If you did like this, give this video like go to our website to get more information
circuitbread.com and you can see all this code and a little bit more in how to do this.
Leave a comment if you have any questions, subscribe to our channel and we'll catch you
on the next tutorial.