Simple Questions about clock speed

tahmod:
Hello,
I have three question on clock speed
1.-Why the clock rate is needed in microcontroller?
2.-My one is 16MHz arduino uno, what is the different if it is lower or high?
3.-How long it take to run once the void loop? And how long it take to run one instruction?

Imagine you are a microcontroller. I will write a program for you to execute:

grab bucket
while_bricks_remain {
goto pile
place one brick into bucket
return from pile
drop brick on ground
}

The point of a CLOCK in a microcontroller is that it tells you what to do next.

For example, start the program. You grab the bucket. Now what? The clock tells you "do the next thing you see".

You see "while bricks remain" and you see that some do remain.
The next clock sends you to the brick pile.
The next clock tells you to place one brick into the bucket.
The next clock tells you to return from the pile.
The next clock tells you to dump the brick on the ground.
The next clock makes you check if any more bricks remain.
If they do, the next clock sends you to the brick pile.
...etc...

You see? The processor needs to be told to do each step. Without a clock, it will just sit at the first instruction, forever waiting for something to do.

With the clock, the processor will execute, one at a time, every task it's given by the code you wrote.

Now for your other questions: Clock speed is how fast the processor executes each command. Of course, we want that clock as fast as possible... thousands of gigahertz would be nice! But the physical silicon chip cannot run that fast. The AVR chips used in the Arduino can run at 16 MHz or even 20, but that's about it. Of course, the chip can run SLOWER than 16... you could run it at 10Hz clock rate if you wanted to... but programs would take forever to run.

A good analogy would be if I required you to move one brick per minute. You could do it in a lot less time, and the excess time you would spend relaxing. You would be nice and cool. Now if I up that to one brick every 10 seconds, you get a lot less rest time. At a certain point, you have zero rest time. You are beginning to warm up.

Now imagine I want 1 brick every second moved. You can do it. but you have to run as fast as you can and you start to really sweat.

Now imagine I want 5 bricks per second moved. You physically, simply cannot go that fast, so you end up tripping, falling and knocking over the pile of bricks from exhaustion. You (the computer) crashed from too fast a clock speed.

As far as how long instructions take, that depends on the instruction and the type of processor. Some simple instructions need only 1 clock cycle to finish, others need 2 or more to a lot more.

To know how many clock cycles a particular piece of code uses, you need to look at the individual opcodes used, see how many times each one is used (especially if you're timing a loop), then add up all those cycles to get the answer.

In most cases, you don't care how many cycles are used. The only time you would care would be if you were writing a precision delay loop and if, say, you specified "delay 1000 nanoseconds" the delay would use EXACTLY the proper number of cycles for a 1000 nanosecond delay.

Hope this helps.....

Make sense?