---
title: Midterm exam
heading: "Midterm exam: the board, inputs, timing, interrupts and analog"
eyebrow: C12 · Midterm exam
type: problems
---

::: hook
Twelve problems from the first half of the course: the board and its datasheet, blinking in MicroPython and C, bits and volatile, memory budgets, inputs and pull-ups, debouncing, debugging, timers, interrupts, input capture, the ADC and its input, fixed point, filtering, PWM and driving loads, sound and the DAC. The board runs at 10 MHz, one instruction per cycle; MicroPython hardware calls take about 40 µs. Give yourself two hours, work without the lessons open, and write out your reasoning before checking each answer. "Another set" deals a different exam.
:::

::: pool
pick: 12

::: check
kind: parts
given: Vf = 2 | 2.9; R = 150 | 220 | 470

## An LED with forward voltage {Vf} V is driven from a 3.3 V pin through {R} Ω.

::: part
answer: (3.3 - Vf)/R*1000
unit: mA
tolerance: 1%

### What current does it draw?

::: misses
- [any] $(3.3 - V_f)/R$.
:::
:::

::: part
answer: 0.4*(3.3 - Vf)/R*1000
unit: mA
tolerance: 1%

### What's its average current with 40% PWM?

::: misses
- [any] Duty times the full current.
:::
:::
:::

::: check
kind: parts
given: base = 0x40003000 | 0x40007000; off = 4 | 8; n = 5 | 12

## A register sits at offset {off} from {base:hex8}, and a program needs a mask for pin P{n}.

::: part
answer: base + off
base: 16

### What's the register's address, in hex?

::: misses
- [any] Base plus offset.
:::
:::

::: part
answer: 2^n
base: 16

### What's the mask, in hex?

::: misses
- [any] $2^n$.
:::
:::
:::

::: check
kind: parts
given: s = 50 | 200 | 1000; n = 4 | 25

## A MicroPython loop makes {n} hardware calls and sleeps {s} ms each pass.

::: part
answer: s + n*0.04
unit: ms
tolerance: 0.1%

### How long is a pass?

::: misses
- [any] The sleep plus 40 µs per call.
:::
:::

::: part
answer: 1000*n*0.04
unit: ms
tolerance: 0.5%

### How far behind an exact grid is it after 1000 passes?

::: misses
- [any] 1000 times the extra per pass.
:::
:::
:::

::: check
kind: parts
given: x = 0x9C | 0xE7 | 0x5F; n = 1 | 4 | 6

## A `uint8_t` register holds {x:hex2}.

::: part
answer: floor(x/2^n) - 2*floor(x/2^(n + 1))
tolerance: 0

### What's bit {n}?

::: misses
- [any] Shift right {n}, keep bit 0.
:::
:::

::: part
answer: x + 2^n - 2*2^n*(floor(x/2^n) - 2*floor(x/2^(n + 1)))
base: 16

### What does it hold after `x ^= 1 << {n}`, in hex?

::: misses
- [any] Flip only bit {n}.
:::
:::
:::

::: check
kind: parts
given: R = 10 | 47; C = 30 | 80

## A pull-up of {R} kΩ charges {C} pF of pin capacitance.

::: part
answer: 1.2*R*C
unit: ns
tolerance: 2%

### How long until the pin reads high after release? (kΩ × pF = ns)

::: misses
- [any] $1.2RC$.
:::
:::

::: part
answer: 3.3/R*1000
unit: µA
tolerance: 1%

### What current flows while the button is held?

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

::: check
kind: parts
given: p = 4 | 7; k = 3 | 5; Ts = 2 | 4; N = 5

## A button bouncing {k} times each way is pressed {p} times; a debouncer samples every {Ts} ms and needs {N} equal samples.

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

### How many falling edges would a naive counter see?

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

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

### How long after settling does the debouncer accept a level?

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

::: check
kind: parts
given: P = 9 | 49 | 999; N = 199 | 4999

## A timer has PRESCALE = {P} and TOP = {N}.

::: 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: (P + 1)/1e7*1e6
unit: µs
tolerance: 0.5%

### What's the length of one count?

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

::: check
kind: parts
given: c = 150 | 600; f = 4 | 30; k = 2 | 16

## An ISR of {c} cycles, saving and restoring {k} registers inside that time, 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: 100*2*k/c
unit: %
tolerance: 0.5%

### What share of the ISR's own time is saving and restoring?

::: misses
- [any] $2k$ of its {c} cycles.
:::
:::
:::

::: check
kind: parts
given: base = 0x1000 | 0x1200; s = 2 | 4; n = 6 | 20

## A loop reads an array of {n} {s}-byte elements at {base:hex4}, but its test is `i <= {n}`.

::: part
answer: n + 1
tolerance: 0

### How many elements does it read?

::: misses
- [any] 0 through {n} inclusive.
:::
:::

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

### What address does the extra read come from, in hex?

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

::: check
kind: parts
given: V = 0.9 | 1.4 | 2.3

## A thermistor divider (10 kΩ, B = 3950, under a 10 kΩ resistor) gives {V} V.

::: part
answer: 10*(3.3 - V)/V
unit: kΩ
tolerance: 0.5%

### What's the thermistor's resistance?

::: misses
- [any] $R_f(3.3 - V)/V$.
:::
:::

::: part
answer: 1/(1/298.15 + ln((3.3 - V)/V)/3950) - 273.15
unit: °C
tolerance: 0.5%

### What temperature is that?

::: misses
- [any] The Beta equation.
:::
:::
:::

::: check
kind: parts
given: c = 829 | 2111 | 3333

## The ADC reads {c}.

::: part
answer: floor(c*3300/4095)
unit: mV
tolerance: 0

### What does `c * 3300 / 4095` give, in integers?

::: misses
- [any] Integer division truncates.
:::
:::

::: part
answer: floor((c*3300 + 2047)/4095)
unit: mV
tolerance: 0

### And the rounded version, `(c * 3300 + 2047) / 4095`?

::: misses
- [any] Add half the divisor first.
:::
:::
:::

::: check
kind: parts
given: f = 2000 | 25000; D = 15 | 70

## PWM must run at {f} Hz and {D}% duty.

::: part
answer: 1e7/f
tolerance: 0

### What's PERIOD?

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

::: part
answer: 1e7/f*D/100
tolerance: 0

### And DUTY?

::: misses
- [any] PERIOD × duty.
:::
:::
:::

::: check
kind: parts
given: n = 48 | 60 | 84

## A buzzer must play MIDI note {n}.

::: part
answer: 440*2^((n - 69)/12)
unit: Hz
tolerance: 0.1%

### What frequency?

::: misses
- [any] $440 \times 2^{(n-69)/12}$.
:::
:::

::: part
answer: 1200*log(1e7/round(1e7/(440*2^((n - 69)/12)))/(440*2^((n - 69)/12)))/log(2)
unit: cents
tolerance: 0.5%

### With PERIOD rounded to a whole number, how many cents off is it?

::: misses
- [any] $1200\log_2(f_\text{actual}/f)$.
:::
:::
:::

::: check
kind: parts
given: N = 32 | 128; fs = 10000 | 32000; V = 0.7 | 2.2

## A DAC plays a {N}-sample table at {fs} samples per second; separately, it's asked for {V} V.

::: part
answer: fs/N
unit: Hz
tolerance: 0.5%

### What frequency does the table play?

::: misses
- [any] $f_s/N$.
:::
:::

::: part
answer: round(V/3.3*4095)
tolerance: 0

### What code gives {V} V?

::: misses
- [any] $V/3.3 \times 4095$.
:::
:::
:::

::: check
kind: parts
given: a = 12 | 99; m = 1 | 4

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

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

### What is `count` afterwards?

::: misses
- [any] Main's stale copy plus 1.
:::
:::

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

### What should it be?

::: misses
- [any] Every increment counted.
:::
:::
:::

::: check
kind: parts
given: L = 50 | 200; R = 50 | 200; I = 0.5 | 2

## A relay coil of {L} mH and {R} Ω and a motor drawing {I} A are both switched from GPIO through low-side MOSFETs with 80 mΩ on-resistance.

::: part
answer: L/R
unit: ms
tolerance: 0.5%

### With a flyback diode, what is the coil current's decay time constant at turn-off?

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

::: part
answer: I^2*0.08
unit: W
tolerance: 0.5%

### What does the motor's MOSFET dissipate when on?

::: misses
- [any] $I^2R_{DS(\text{on})}$.
:::
:::
:::
:::
