Let's Play!

If you've read and done what the last couple of posts said, you should be up and running with everything working, software-wise.

You've got a project called MM1. It compiles but it doesn't do anything.

Let's make it do something. Stop if you don't understand, and read it again. It isn't difficult, and it's going to tell you not just how to take something out of the box and plug it in, but how it works.

Once you understand how it works, you'll be able to make it do anything it's capable of doing.

The datasheet for this little 20-legged thing is here. If you can read and understand all of that, you don't need to read any more of this. Let me know if you want a job.

However, if you don't entirely understand, let's start at the beginning, hands-on, and see if you can make yourself a micromeister!

C experts: at this point, don't flame me with "that isn't how you do it!" please. I'm doing it for a reason, I know it's horrible. It's to demonstrate a thing.

Now, you have a source code file which has in it:

void main()
{


}

Don't you? If not, go back and look at the last couple of posts. Then you will.

You do? Good. now add these lines in between the curly braces, they're the squiggles that look like { and }

    int i=0;
    while (1)
        i = i + 1;


or copy and paste those.

Now it should look like this:

void main()
{
    int i=0;
    while (1)
        i = i + 1;
}

OK. Let's explain what that does (C folk will know, so skip this bit).

void means nothing. Literally nothing. main is a C thing. It's telling the compiler where to start, that's all. All programs (normally) have this first line. The () after void main is telling the compiler that this is a function. Just don't worry about it. EVERY program you'll be doing on this blog will have

void main() 

in it, and it will have that pair of curly braces (squiggles) after it.

In between those curly braces is the actual code.

So let's explain the code:

int means integer. A whole number, like 1, or 2. Not decimal, like 1.5 or 3.14159 or ½. Not a letter, like X. A whole number.

int i means let's have an integer called i. Doesn't have to be called i, but it has to be called something. You could have int tarquinfintimlinbinftangftangolebiscuitbarrel but who wants to type all that? Not me. So I'm sticking with i.

So what would we think int i=0 means then? Well, it means "let's have an integer (whole number), call it i, and make it 0 to start with.

The ; at the end means it's the end of an instruction. Like a full stop at the end of a sentence. Done that. Next.

Long-winded? Not really. Next time you see

int i=0;

you'll be all "oh look, an integer, called i, which is 0 to start with. OK then."

Clear? If not, read it again.

This simply tells the compiler that sometime, soon, you might want to something with this integer, or number, which you will call i. You might not ever do that but then why would you bother telling it you would?

Right then. Next bit:

while (1)

The C language has a while statement, like English does.

while the sun shines, make hay.

Compilers are a bit more fussy than this, so you have to follow their rules. To get a compiler to make hay while the sun shines, you'd write:

while (the_sun_shines)
    make_hay;

In this case "the sun shines" is something which can be true (the sun is shining) or false (the sun is not shining). The compiler is not much of a linguist, so it has false as 0 (zero) and true as not false (e.g.1). Traditionally we use 1 to mean true.

So what we ask the microprocessor to do when we say:

    while (1)
        i = i + 1;


is to add 1 to our very own integer called i, while ... while what? While 1. True. Forever. So this will (pointlessly) add 1 to i, until the cows come home. Or until someone unplugs it.

"Why would you want to do that?" I hear you ask.

Well, I'll tell you why. Oi! C Programmers! Come back, we're going to learn some microprocessor stuff here!

Now then. Put the cursor (click the mouse) on int i = 0; and set a "BREAKPOINT" on it by pressing Ctrl and F8. If you've done this, the line will be highlighted (highlit?) with a reddish blob, like this:



Yes? OK, we're ready to run the program. Once you connect up your hardware you will be running this actually inside the microprocessor. For now, it will run in the simulator.

Select Debug from the main menu, then Debug Main Project. It will compile again, then run. It will stop where your breakpoint is, and be flagged up in green.



Yes? Please, yes?

OK, this is where the C programmers will throw a wobbly. This isn't a Windows program, or a Linux program. This is microprocessor program.

Now, the F8 key will step an instruction. Press it and see where the green arrow points.



Press it a few more times. Same place, right? This is because it will do this forever. Right?

The F5 key tells it to run and run and run. Press it. What happens?

What? Come on then. What happened there?

I'll write while you think. If you know, leave a comment.

Here is a short video with that last bit, if you like videos.

Let me know if you prefer videos. I dont.

Comments

Popular posts from this blog

Get the software

Your first test

Lights!