---
title: Problem set 3
heading: "Problem set 3: inputs, timers and interrupts"
eyebrow: C12 · Problem set 3
type: problems
---

::: hook
These problems use lessons 7 to 13: inputs and pull-ups, debouncing, debugging, timers, interrupts, and input capture and encoders. The clock is 10 MHz; the timer counts every PRESCALE + 1 cycles and wraps after TOP + 1 counts.
:::

::: pool
pick: 12

::: check
kind: parts
given: R = 10 | 40 | 100; C = 20 | 50

## A button to ground has a {R} kΩ pull-up; the pin and its wire have {C} pF.

::: part
answer: 3.3/(R*1e3)*1e6
unit: µA
tolerance: 1%

### What current flows while it's pressed?

::: misses
- [any] $3.3/R$.
:::
:::

::: part
answer: 1.2*R*1e3*C*1e-12*1e6
unit: µs
tolerance: 2%

### After release, how long until the pin crosses 70% of 3.3 V?

::: misses
- [any] $RC\ln(1/0.3)$.
:::
:::
:::

::: check
kind: parts
given: p = 2 | 5 | 8; k = 2 | 4

## A button's contacts bounce {k} times on every press and on every release; it's pressed {p} times.

::: part
answer: p*(1 + 2*k)
tolerance: 0

### How many falling edges does a fast program see?

::: misses
- [any] $1 + 2k$ per press.
:::
:::

::: part
answer: p
tolerance: 0

### How many does a correct debouncer count?

::: misses
- [any] One per real press.
:::
:::
:::

::: check
kind: parts
given: Ts = 1 | 2 | 5; N = 4 | 6; b = 3 | 8

## A debouncer samples every {Ts} ms and accepts a level after {N} equal samples; the bounce lasts {b} ms.

::: part
answer: N*Ts
unit: ms
tolerance: 0.5%

### How long does it take to accept a settled level?

::: misses
- [any] $N T_s$.
:::
:::

::: part
answer: b + N*Ts
unit: ms
tolerance: 0.5%

### About how long after the first contact does it report the press?

::: misses
- [any] The bounce, then the samples.
:::
:::
:::

::: check
kind: parts
given: base = 0x1000 | 0x1080; s = 2 | 4; n = 10 | 16

## An array of {n} elements of {s} bytes starts at {base:hex4}, and a loop runs from 0 to {n} inclusive.

::: part
answer: base + s*n
base: 16

### What address does its last pass read, in hex?

::: misses
- [any] base + size × {n}.
:::
:::

::: part
answer: s
tolerance: 0

### How many bytes past the end of the array does that read start?

::: misses
- [any] Element {n} starts right after the last real one.
:::
:::
:::

::: check
kind: parts
given: P = 9 | 99 | 999; N = 499 | 999 | 9999

## A timer has PRESCALE = {P} and TOP = {N}, and a pin toggles at each wrap.

::: part
answer: (P + 1)*(N + 1)/1e7*1000
unit: ms
tolerance: 0.05%

### How long between wraps?

::: misses
- [any] $(P + 1)(N + 1)/f$.
:::
:::

::: part
answer: 1e7/((P + 1)*(N + 1))/2
unit: Hz
tolerance: 0.05%

### What frequency is the pin's square wave?

::: misses
- [any] Two wraps per period.
:::
:::
:::

::: check
kind: parts
given: f = 100 | 1000 | 3000; P = 0 | 9

## A pin must square-wave at {f} Hz from a timer with PRESCALE = {P}.

::: part
answer: round(1e7/(P + 1)/(2*f)) - 1
tolerance: 0

### What TOP comes closest?

::: misses
- [any] Half a period in counts, rounded, minus 1.
:::
:::

::: part
answer: 1e7/(P + 1)/(2*(round(1e7/(P + 1)/(2*f))))
unit: Hz
tolerance: 0.01%

### What frequency does it actually make?

::: misses
- [any] Use the rounded TOP.
:::
:::
:::

::: check
kind: parts
given: c = 80 | 300 | 1200; f = 2 | 10 | 25

## An ISR takes {c} cycles and fires {f} thousand times a second.

::: part
answer: 100*c*f*1000/1e7
unit: %
tolerance: 0.5%

### What share of the processor does it use?

::: misses
- [any] $c f/f_\text{clk}$.
:::
:::

::: part
answer: 1e7/c/1000
unit: kHz
tolerance: 0.5%

### What rate would use all of it?

::: misses
- [any] $f_\text{clk}/c$.
:::
:::
:::

::: check
kind: parts
given: k = 2 | 6 | 16; n = 3 | 8

## An ISR saves and restores {k} registers and takes {n} cycles to reach its first useful instruction after that.

::: part
answer: k + n
tolerance: 0

### How many cycles from entering the ISR to its first useful instruction?

::: misses
- [any] The saves come first.
:::
:::

::: part
answer: (k + n)/10
unit: µs
tolerance: 0.5%

### And in microseconds?

::: misses
- [any] 100 ns per cycle.
:::
:::
:::

::: check
kind: parts
given: a = 7 | 19 | 100; m = 2 | 3

## `count` is {a}. While main does `count = count + 1` (load, add, store), {m} ISR increments land between its load and its store.

::: part
answer: a + 1
tolerance: 0

### What is `count` afterwards?

::: misses
- [any] Main stores its stale copy plus 1.
:::
:::

::: part
answer: m
tolerance: 0

### How many increments were lost?

::: misses
- [any] The ISR's increments were overwritten.
:::
:::
:::

::: check
kind: parts
given: t1 = 4294966000 | 4294900000; t2 = 300 | 5000

## A 32-bit cycle counter read {t1} at one event and {t2} at the next, wrapping in between.

::: part
answer: t2 + 4294967296 - t1
tolerance: 0

### How many cycles passed?

::: misses
- [any] Up to $2^{32}$, then on to {t2}.
:::
:::

::: part
answer: (t2 + 4294967296 - t1)/10
unit: µs
tolerance: 0.5%

### How long is that?

::: misses
- [any] 10 cycles per µs.
:::
:::
:::

::: check
kind: parts
given: n = 12 | 30 | 80; baud = 9600 | 115200

## A debug print sends {n} characters over the UART at {baud} baud, waiting for each.

::: part
answer: n*10/baud*1000
unit: ms
tolerance: 0.5%

### How long does it take?

::: misses
- [any] 10 bits per character.
:::
:::

::: part
answer: n*10/baud*1e7
tolerance: 0.5%

### How many clock cycles is that?

::: misses
- [any] Seconds times $10^7$.
:::
:::
:::

::: check
kind: parts
given: a = 3 | 6; b = 10 | 50; c = 2 | 4

## A breakpoint sits in the innermost of three nested loops of {a}, {b} and {c} passes.

::: part
answer: a*b*c
tolerance: 0

### How many times does it stop the program?

::: misses
- [any] The product of the passes.
:::
:::

::: part
answer: a*b
tolerance: 0

### And a breakpoint in the middle loop's body, outside the inner loop?

::: misses
- [any] Outer passes times middle passes.
:::
:::
:::

::: check
kind: parts
given: N = 5000 | 40000; p = 0 | 9

## A 16-bit capture timer runs at $10^7/(\text{PRESCALE} + 1)$ Hz with PRESCALE = {p} and records edges {N} counts apart.

::: part
answer: 1e7/(p + 1)/N
unit: Hz
tolerance: 0.5%

### What frequency is that?

::: misses
- [any] Timer rate ÷ counts.
:::
:::

::: part
answer: 1e7/(p + 1)/65536
unit: Hz
tolerance: 0.5%

### What is the lowest frequency it can measure without counting overflows?

::: misses
- [any] One full turn of the counter per period.
:::
:::
:::

::: check
kind: parts
given: P = 100 | 360 | 1000; r = 60 | 1200 | 6000

## A {P}-line quadrature encoder is decoded 4× and turns at {r} rpm.

::: part
answer: 4*P
unit: counts/rev
tolerance: 0

### How many counts per revolution?

::: misses
- [P] Both edges of both channels count.
- [any] $4 \times \text{PPR}$.
:::
:::

::: part
answer: 360/(4*P)
unit: °
tolerance: 0.5%

### What angle is one count?

::: misses
- [any] 360° ÷ counts per revolution.
:::
:::

::: part
answer: 4*P*r/60
unit: counts/s
tolerance: 0.5%

### How many counts per second?

::: misses
- [any] Counts per rev × rev/s.
:::
:::
:::

::: check
kind: parts
given: t = 0.3 | 2.9 | 11.7; T = 15 | 25

## An ultrasonic echo returns {t} ms after the ping at {T} °C (sound at $331.3 + 0.606T$ m/s), timed by a 1 MHz capture.

::: part
answer: (331.3 + 0.606*T)*t/1000/2*1000
unit: mm
tolerance: 0.5%

### How far is the target?

::: misses
- [any] Speed × time ÷ 2.
:::
:::

::: part
answer: (331.3 + 0.606*T)*t/1000/2*1000*(0.606*5/(331.3 + 0.606*T))
unit: mm
tolerance: 1%

### How much would the reading be off if the firmware assumed a temperature 5 °C too low?

::: misses
- [any] Distance × (5 × 0.606)/speed.
:::
:::
:::
:::
