---
title: Final exam
heading: "Final exam: embedded systems in MicroPython and C"
eyebrow: C12 · Final exam
type: problems
---

::: hook
Twelve problems from the whole course: the board and its registers, C for small machines, memory, inputs, debouncing, timers, interrupts and capture, analog in and out, fixed point and filtering, driving loads, serial buses and protocols, state machines and preemptive scheduling, real time, watchdogs, power, flash storage, control loops, and the two languages. The board runs at 10 MHz, one instruction per cycle. Give yourself three 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 = 1.9 | 2.6; R = 100 | 330; D = 25 | 60

## An LED with forward voltage {Vf} V is driven from 3.3 V through {R} Ω, with PWM at {D}% duty.

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

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

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

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

### What's its average current?

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

::: check
kind: parts
given: x = 0xB4 | 0x6E | 0xD9; n = 2 | 5

## A register byte holds {x:hex2}.

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

### What does it hold after setting bit {n}, in hex?

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

::: part
answer: floor(x/16)
base: 16

### What's its upper 4-bit field, in hex?

::: misses
- [any] Shift right 4.
:::
:::
:::

::: check
kind: parts
given: R = 22 | 47; C = 40 | 100; p = 3 | 6; k = 2 | 4

## A {R} kΩ pull-up charges {C} pF; separately, a button bouncing {k} times each way is pressed {p} times.

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

### How long until a released pin reads high? (kΩ × pF = ns)

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

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

### How many falling edges does an undebounced counter see?

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

::: check
kind: parts
given: P = 99 | 9; N = 999 | 2499; c = 300 | 900; f = 10 | 50

## A timer has PRESCALE = {P}, TOP = {N}; separately, an ISR of {c} cycles fires {f} thousand times a second.

::: 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: 100*c*f*1000/1e7
unit: %
tolerance: 0.5%

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

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

::: check
kind: parts
given: V = 0.7 | 1.8 | 2.9

## A thermistor divider (10 kΩ at 25 °C, B = 3950, over 10 kΩ) gives {V} V.

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

### What code does the 12-bit ADC report?

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

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

### What temperature is it?

::: misses
- [any] Thermistor resistance from the divider, then the Beta equation.
:::
:::
:::

::: check
kind: parts
given: x = 2.75 | -0.5; n = 10 | 16; c = 1499 | 3707

## A value {x} is stored with {n} fraction bits; separately, the ADC reads {c}.

::: part
answer: round(x*2^n)
tolerance: 0

### What integer stores {x}?

::: misses
- [any] $\text{round}(x \cdot 2^n)$.
:::
:::

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

### What does `(c * 3300 + 2047) / 4095` give?

::: misses
- [any] Rounded integer millivolts.
:::
:::
:::

::: check
kind: parts
given: n = 57 | 66 | 76; N = 32 | 64; fs = 8000 | 32000

## A buzzer plays MIDI note {n}; separately, a DAC plays a {N}-sample table at {fs} Hz.

::: part
answer: round(1e7/(440*2^((n - 69)/12)))
tolerance: 0

### What PWM PERIOD plays the note?

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

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

### What frequency does the table play?

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

::: check
kind: parts
given: b = 57600 | 115200 | 230400; n = 30 | 120

## A UART runs at {b} baud.

::: part
answer: round(1e7/b)
tolerance: 0

### What BAUDDIV?

::: misses
- [any] $f_\text{clk}/\text{baud}$, rounded.
:::
:::

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

### How long do {n} bytes take?

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

::: check
kind: parts
given: b = 115200 | 460800; T = 4 | 20; h = 900 | 140; t = 870 | 100

## Bytes arrive at {b} baud while the main loop may be away for {T} ms; its ring buffer has head {h} and tail {t}.

::: part
answer: b/10*T/1000
tolerance: 1%

### How many bytes can arrive meanwhile?

::: misses
- [any] (b/10) × T.
:::
:::

::: part
answer: h - t
tolerance: 0

### How many bytes are waiting now?

::: misses
- [any] head − tail.
:::
:::
:::

::: check
kind: parts
given: a = 0x29 | 0x50 | 0x6B; f = 100 | 400; n = 3 | 6

## An I²C device answers at {a:hex2}; a transaction sends {n} bytes at {f} kHz, plus about two clocks.

::: part
answer: 2*a + 1
base: 16

### What's the first byte of a read, in hex?

::: misses
- [any] (address ≪ 1) | 1.
:::
:::

::: part
answer: (9*n + 2)*1000/f
unit: µs
tolerance: 1%

### How long does the transaction take?

::: misses
- [any] 9 clocks per byte, plus two.
:::
:::
:::

::: check
kind: parts
given: CLKDIV = 2 | 10; k = 2 | 4; a = 220 | 180; b = 100 | 150; c = 70 | 200

## An SPI bus (clock $f_\text{clk}/(2\cdot\text{CLKDIV})$) updates {k} chained shift registers; separately, a frame's payload is {a}, {b}, {c}.

::: part
answer: 8*k*2*CLKDIV/10
unit: µs
tolerance: 0.5%

### How long does one update take on the wire?

::: misses
- [any] 8 bits per register, each 2 × CLKDIV cycles.
:::
:::

::: part
answer: (a + b + c) - 256*floor((a + b + c)/256)
tolerance: 0

### What's the payload's 8-bit sum checksum?

::: misses
- [any] Modulo 256.
:::
:::
:::

::: check
kind: parts
given: c1 = 0.4 | 1; t1 = 5 | 10; c2 = 12 | 40; t2 = 250 | 1000

## A superloop runs task 1 ({c1} ms every {t1} ms) and task 2 ({c2} ms every {t2} ms).

::: part
answer: 100*(c1/t1 + c2/t2)
unit: %
tolerance: 0.5%

### What's the utilization?

::: misses
- [any] $\sum C_i/T_i$.
:::
:::

::: part
answer: c2
unit: ms
tolerance: 0.5%

### How late can task 1 start, at worst, because of task 2?

::: misses
- [any] Task 2's whole turn.
:::
:::
:::

::: check
kind: parts
given: f = 100 | 2000; A = 1 | 3; dt = 4 | 40; L = 30 | 90; C = 100; D = 200

## A {f} Hz, {A} V signal is sampled with {dt} µs of jitter; separately, a job has latency {L} µs, execution {C} µs and deadline {D} µs.

::: part
answer: 2*pi*f*A*dt*1e-6*1000
unit: mV
tolerance: 0.5%

### How large can a sample's error be?

::: misses
- [any] $2\pi f A\,\Delta t$.
:::
:::

::: part
answer: D - L - C
unit: µs
tolerance: 0

### What's the job's margin?

::: misses
- [any] D − L − C.
:::
:::
:::

::: check
kind: parts
given: g = 8 | 30; m = 3 | 5; Ia = 2; d = 0.5 | 5; Is = 0.05 | 0.5

## A watchdog gets a {m}× margin over a {g} ms longest pass; separately, a device is awake {d}% of the time at {Ia} mA and asleep at {Is} mA.

::: part
answer: g*m*10000
tolerance: 0

### What WDT_LOAD?

::: misses
- [any] Timeout in cycles.
:::
:::

::: part
answer: d/100*Ia + (1 - d/100)*Is
unit: mA
tolerance: 0.5%

### What's the average current?

::: misses
- [any] Weight by time.
:::
:::
:::

::: check
kind: parts
given: P = 1000 | 2000; To = 0 | 5; R = 0.02; C = 2000; b = 0.5 | 1; rh = 0.3; rc = 0.3

## A room ({C} J/°C, R = {R} °C/W, outside {To} °C) has a {P} W heater; its thermostat's band is ±{b} °C, warming and cooling at {rh} and {rc} °C/s.

::: part
answer: To + P*R
unit: °C
tolerance: 0.5%

### Where would it settle with the heater always on?

::: misses
- [any] $T_\text{out} + PR$.
:::
:::

::: part
answer: 2*b/rh + 2*b/rc
unit: s
tolerance: 0.5%

### How long is one on-off cycle?

::: misses
- [any] $2b/r_h + 2b/r_c$.
:::
:::
:::

::: check
kind: parts
given: E = 10000 | 100000; w = 24 | 1440; r = 32 | 64; t = 1 | 5; T = 20 | 50

## A device logs {r}-byte records {w} times a day in a 4 KB flash sector rated for {E} erases. Its control loop samples every {t} ms against a plant with a {T} ms time constant.

::: part
answer: E*floor(4096/r)/w/365.25
unit: years
tolerance: 0.5%

### How long does the log's sector last?

::: misses
- [any] Endurance × records per erase ÷ records per year.
:::
:::

::: part
answer: (1 + exp(-t/T))/(1 - exp(-t/T))
tolerance: 0.5%

### What is the loop's largest stable proportional gain (plant gain 1)?

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