TactivergeLite

Lite / Building Machines That Respond / Week 19

Counting in nanoseconds

A 16 MHz chip ticks sixteen million times a second. Is that fast enough to catch a button, run a motor or record music?

About 30 min · 3 goals · Checked by automated tests · Full interactive version

Why should I care?

Engineers ask 'is the chip fast enough?' before they choose one. The answer is simple arithmetic with the clock rate, and getting it right early stops a project discovering, weeks later, that its loop is too slow to catch a fast sensor.

Predict

You double a microcontroller's clock from 16 MHz to 32 MHz. What happens to the length of one clock tick?

  1. It doubles
  2. It halves
  3. It stays the same
Guess first, then reveal

Period = 1 ÷ frequency, so twice the frequency means half the period: 62.5 ns becomes 31.25 ns. Twice as many ticks fit into each second, so roughly twice as many instructions run.

Discover

Choose the 1,000-times-a-second motor deadline. Find the smallest clock that lets a 2,000-instruction loop fit. Then switch to the audio deadline and see what it would take.

What the interactive shows (text version)

The budget calculator takes a clock frequency, cycles per instruction and loop size. It shows the clock period (1 ÷ frequency), instructions per millisecond, the time for one loop pass (instructions × cycles ÷ frequency) and compares it with a chosen deadline, saying whether it fits and what fraction of the deadline it uses.

You can also open the interactive on the full site when you have a faster connection.

Watch (optional)

Watch for: Watch for the clock and how each instruction takes a number of clock cycles to complete.

Follows the fetch, decode and execute cycle so you can see how a CPU runs a program one instruction at a time.

“The Central Processing Unit (CPU): Crash Course Computer Science #7” by CrashCourse (opens YouTube; streaming video uses a lot of data)

The video is optional. Everything you need is in this lesson's text.

Understand

Frequency and period are flip sides

The clock frequency is how many ticks happen each second, in hertz (Hz). Mega means million, so 16 MHz is 16,000,000 ticks a second. The period is how long one tick lasts: 1 ÷ 16,000,000 s = 62.5 nanoseconds. Light travels only about 19 metres in that time.

Instructions cost cycles

Simple instructions on many small chips take one clock cycle; others (dividing, reading slow memory) take several. Engineers use an average cycles per instruction to estimate. A 16 MHz chip at 1 cycle per instruction runs about 16 million instructions a second, which is 16,000 every millisecond.

Budgets and deadlines

Machines have deadlines: check the button every 10 ms, update the motor every 1 ms, take an audio sample every 22.7 µs. Divide the time per loop pass by the deadline to see how much of the budget you use. Leave a margin: real code has surprises, and a loop at 95% of its budget will overrun one day.

Explain it another way

Intuitive

A clock is a drummer. A faster drummer gives more beats per second, and each beat is shorter. Each instruction needs one or a few beats, so more beats means more instructions.

Technical

N = f·t/CPI. Memory wait states, interrupts and peripheral access add cycles, so measured execution time is often longer than the estimate; real-time engineers measure the worst case with a spare pin and an oscilloscope, or a timer.

Mathematical

T = 1/f. With f in MHz, T in µs is 1/f and in ns is 1000/f. Loop time τ = n·CPI/f; deadline met when τ ≤ D, i.e. f ≥ n·CPI/D.

Applied

A drone's flight controller updates its motors hundreds of times a second. Its designers check that the whole sense–compute–act loop fits inside that period with time to spare, or the drone wobbles.

Derivation: From MHz to an instruction budget
  1. Frequency f counts ticks per second; 1 MHz = 1,000,000 ticks per second.

  2. One tick lasts the period T = 1 ÷ f. At 16 MHz: T = 1 ÷ 16,000,000 s = 0.0000000625 s = 62.5 ns (1 ns = 0.000000001 s).

  3. If each instruction needs c clock cycles on average, the chip runs f ÷ c instructions per second.

  4. In a time window t, it runs N = f × t ÷ c instructions. At 16 MHz, c = 1, t = 1 ms: N = 16,000,000 × 0.001 ÷ 1 = 16,000.

  5. A loop of n instructions takes n × c ÷ f seconds per pass. It meets a deadline D only if n × c ÷ f ≤ D, and wise engineers keep it well under.

Worked example 1

A 48 MHz chip averages 2 cycles per instruction. Can a 30,000-instruction loop run 1,000 times a second?

Instructions per second = 48,000,000 ÷ 2 = 24,000,000. One pass = 30,000 ÷ 24,000,000 = 0.00125 s = 1.25 ms. The deadline is 1 ms, so no: it overruns by 25%. Halve the work, or use a faster chip.

Worked example 2

How long is one period of a 240 MHz clock?

T = 1000 ÷ 240 ns ≈ 4.17 ns.

Common mix-ups

Practice

Practice, not a test. Answers are checked on our server; a wrong answer gets a hint and another try. Nothing is saved.

ns

instructions

loops per second

MHz

Practice 5. Your loop uses 97% of its 1 ms deadline in testing. What should you conclude?

Apply it

A greenhouse controller must read four sensors, do 2,000 instructions of maths and update a display, all ten times a second. Estimate the instructions per pass (assume 500 per sensor read and 3,000 for the display), choose a clock from 1, 16 or 48 MHz at 2 cycles per instruction, and show your margin.

Reflect

Where in a machine you know would a slow loop actually cause a problem someone could notice?

Write your answer on paper or in your own notes. Lite never stores what you write.

← A whole computer on one chipSet up once, loop forever →