Skip to content
Tactiverge

School/△ Builder/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

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

  • ◇ Convert a clock frequency in MHz into a clock period in nanoseconds.
  • ◇ Estimate how many instructions fit into a time window from the clock rate and cycles per instruction.
  • ◇ Decide whether a loop can meet a deadline, with units shown.

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

Loading the interactive…

Can't use the interactive? Read a description instead

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.

3.Checkpoint: guided practice

Check what you just discovered before going on. Hints appear after a wrong answer.

Practice 1 · a fresh version every time

A microcontroller's clock runs at 100 MHz. How long is one clock period, in nanoseconds?

ns

4.Predict

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

Guess first. Being wrong here is how the surprise works.

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

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.

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

  • A 100 MHz chip runs 100 million lines of my program every second.
    ✓ One line of Python or C becomes many instructions, and instructions can take several cycles each.
  • Higher frequency means a longer period.
    ✓ Period = 1 ÷ frequency: they move in opposite directions.

6.Watch

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

11 min 37 s · captions on YouTube · source

Nothing loads from YouTube until you press play.

The video is optional: everything you need is also in the lesson text and the interactive.

7.Practice on your own

Practice, not a test. Wrong answers get a hint and another try.

Practice 2 · a fresh version every time

A 16 MHz microcontroller needs about 4 clock cycles per instruction on average. Roughly how many instructions can it run in 1 ms?

instructions

Practice 3

A 16 MHz chip at 1 cycle per instruction runs a loop of 4,000 instructions. How many times can the loop run each second?

loops per second

Practice 4

A loop needs 5,000 instructions at 1 cycle each and must finish within 0.1 ms. What is the slowest clock that just meets the deadline, in MHz?

MHz

Practice 5

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

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

9.Reflect

Concepts: Inside a microcontroller · Real-time computing on robots