C12 · Lesson 13 · Capturing time and counting motion
Input capture, frequency measurement and quadrature encoders
Plenty of sensors report their reading as time rather than voltage: an ultrasonic rangefinder's echo delay, a flow meter's pulse rate, a fan's tachometer, the A and B channels of a motor's encoder. Measuring them well is a matter of reading a timer at exactly the right moment. A hardware input capture copies the timer's count into a register on the edge itself; the same job done in an interrupt adds the interrupt's latency and jitter. This lesson works out what resolution each method gives, and how a quadrature encoder reports both position and direction with two square waves.
- A quadrature encoder makes two square waves, A and B, a quarter cycle apart. Which one leads tells the direction.
- Flip the direction: B now leads A. Each of the four edges per cycle moves the count by one, up or down.
- The readout is the edge rate a decoder must keep up with: four times the pulses per revolution times the revolutions per second.
Predict
A 500-line encoder on a motor at 3000 rpm is decoded in software, one 50-cycle interrupt per edge, on the 10 MHz board. How much of the processor does that take?
3000 rpm is 50 revolutions a second; four edges per line make 2000 counts a revolution, 100,000 edges a second. At 50 cycles each that's 5 million cycles a second, half the processor. That's why microcontrollers meant for motors have hardware quadrature decoders that count without interrupting at all.
Name it
Input capture
On a chosen edge, the timer's count is copied into a capture register. The time between two captures is their difference (modulo the counter's length, as in the timers lesson), so a period of N counts at gives
for ±1 count of uncertainty. A software capture (reading the timer in an edge interrupt) adds the interrupt's latency to each reading, and its variation adds jitter.
Counting or timing
Counting edges for a gate time resolves Hz; timing one period resolves . They're equal at ; above it, count; below it, time.
Quadrature encoders
Two channels a quarter cycle apart. Counting every edge of both (4× decoding) gives counts a revolution, where PPR is the pulses (lines) per revolution. The state sequence 00, 01, 11, 10 one way and its reverse the other way give direction; a jump of two states means an edge was missed.
Speed
Counting edges over a fixed window works at high speed; at low speed only a few counts arrive per window and the resolution is poor, so firmware switches to timing the interval between edges.
Worked example
A capture timer at 10 MHz records edges 25,000 counts apart. What is the frequency, and how finely is it resolved?
Show the solution
- Frequency.
- Resolution. One count in 25,000 is of the reading: so ±0.016 Hz.
- Compare with counting edges. Counting edges for 1 s would only resolve 1 Hz; at this frequency timing is about 60 times finer.
Answer 400 Hz, resolved to ±0.016 Hz.
Worked example
An ultrasonic rangefinder's echo returns 5.83 ms after the ping. How far is the target, and what distance does one 100 ns timer count represent?
Show the solution
- Distance. Sound travels out and back at 343 m/s, so halve the round trip:
- One count.
- Compare with the echo. That's far finer than the echo's own uncertainty, which is set by the sound's rise time and the air's temperature (0.18% per °C).
Answer the target is 1.000 m away, and one count is 17 µm.
Worked example
A 500-line encoder is counted in 4× mode over 10 ms windows. What speed gives 1000 counts per window, and what is the resolution at 1 rev/s?
Show the solution
- Counts per second. 1000 counts in 10 ms is 100,000 counts/s.
- Speed. Over 2000 counts a revolution: resolved to 0.1%.
- At 1 rev/s. Only 20 counts arrive per window, so a single count is 5% of the reading.
- A better method there. Timing the interval between edges does far better.
Answer 50 rev/s, 3000 rpm, gives 1000 counts per window, resolved to 0.1%; at 1 rev/s a single count is 5% of the reading.
Check
A capture timer at 1 MHz records edges 25000 counts apart.
(a)
What is the signal's frequency?
Write units the way you normally would: 1.5 Hz, 1500 mHz and 0.0015 kHz are the same answer.
(b)
What is the resolution for ±1 count?
Check
A 500-line encoder, decoded 4×, turns at 600 rpm; each edge costs an 80-cycle interrupt at 10 MHz.
(a)
How many edges arrive per second?
(b)
What share of the processor does decoding take?
Check
A 256-line encoder in 4× mode gives 150 counts in a 20 ms window.
(a)
What is the speed?
(b)
What fraction of the reading is one count?
Check
An ultrasonic echo returns 5.83 ms after the ping, in air at 0 °C, where sound travels at m/s.
(a)
How far away is the target?
Write units the way you normally would: 1.5 cm, 15 mm and 0.015 m are the same answer.
(b)
What distance does one 100 ns count represent?
Check
A frequency is measured either by counting edges for 0.1 s or by timing one period with a 10 MHz clock.
(a)
Above what frequency is counting better?
Write units the way you normally would: 1.5 Hz, 1500 mHz and 0.0015 kHz are the same answer.
(b)
What does counting resolve?
Check
A 16-bit capture timer runs at Hz with PRESCALE = 9.
(a)
What is the longest period it can time without counting overflows?
Write units the way you normally would: 1.5 ms, 1500 µs and 0.0015 s are the same answer.
(b)
What is its resolution?
Check
A quadrature encoder turns in the direction where A leads B. Starting from A = 0, B = 0, put the states (A, B) in the order they appear.
- (0, 0)
- (1, 0)
- (0, 1)
- (1, 1)
Check
Write decode(states): states is a list of (A, B) pairs sampled from an encoder. Return (count, errors), where the count goes up by one for each step in the order 00 → 10 → 11 → 01 → 00, down by one for each step the other way, doesn't change when the state repeats, and a jump of two states adds one to errors instead.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Explain why
Why does measuring a low frequency by timing one period give much better resolution than counting edges for a second?
Counting edges in a fixed gate can only be wrong by one edge, but at a low frequency one edge is a large fraction of the total: a 10 Hz signal counted for a second gives 10 ± 1, a 10% uncertainty. Timing a single period instead counts the fast reference clock between two edges of the signal, and at 10 Hz that's a million counts of a 10 MHz clock, so one count of error is a millionth. The slower the signal, the more clock counts fit in its period and the better timing does; counting edges only wins at frequencies above the square root of the clock rate over the gate time.
Practice
Keep practicing
9 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.