---
title: Problem set 12
heading: "Problem set 12: embedded systems across the course"
eyebrow: C12 · Problem set 12
type: problems
---

::: hook
Multi-step problems drawing on the whole course, with lessons 49 to 53 in mind: comparing MicroPython and C, testing, the data logger and the thermostat. The board runs at 10 MHz, one instruction per cycle; MicroPython hardware calls take about 40 µs; the CPU draws 2 mA running, 0.5 mA asleep, 0.002 mA in deep sleep.
:::

::: pool
pick: 12

::: check
kind: parts
given: n = 200 | 3000; k = 6 | 8

## A job makes {n} hardware accesses.

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

### How long in MicroPython (40 µs each)?

::: misses
- [any] n × 40 µs.
:::
:::

::: part
answer: n*k*0.1/1000
unit: ms
tolerance: 0.5%

### And in C at {k} cycles each?

::: misses
- [any] n × k × 100 ns.
:::
:::
:::

::: check
kind: parts
given: F = 256 | 512 | 2048; R = 32 | 128

## A chip has {F} KB of flash and {R} KB of RAM; MicroPython needs about 256 KB of flash and 16 KB of RAM.

::: part
answer: F - 256
unit: KB
tolerance: 0

### How much flash is left for the application?

::: misses
- [any] Flash minus 256.
:::
:::

::: part
answer: R - 16
unit: KB
tolerance: 0

### And how much RAM, beyond the minimum?

::: misses
- [any] RAM minus 16.
:::
:::
:::

::: check
kind: parts
given: k = 3 | 6 | 10; s = 4 | 7; e = 3 | 5

## A function has {k} independent decisions; a state machine has {s} states and {e} events.

::: part
answer: 2^k
tolerance: 0

### How many paths through the function?

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

::: part
answer: s*e
tolerance: 0

### How many transition tests for the machine?

::: misses
- [any] s × e.
:::
:::
:::

::: check
kind: parts
given: f = 10 | 50; c = 400 | 1200; s = 60; b = 2

## A logger samples at {f} Hz with a {c}-cycle ISR and keeps {s} s of {b}-byte readings.

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

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

::: misses
- [any] f × c / 10⁷.
:::
:::

::: part
answer: f*s*b
unit: bytes
tolerance: 0

### How much RAM does its buffer need?

::: misses
- [any] f × s × b.
:::
:::
:::

::: check
kind: parts
given: s = 2337 | 4101; N = 10 | 20

## {N} readings in tenths of a degree sum to {s}.

::: part
answer: floor((s + floor(N/2))/N)
tolerance: 0

### What's the rounded average, in tenths?

::: misses
- [any] (sum + N/2) / N.
:::
:::

::: part
answer: floor((s + floor(N/2))/N) - 10*floor(floor((s + floor(N/2))/N)/10)
tolerance: 0

### What digit is printed after the decimal point?

::: misses
- [any] The tenths value modulo 10.
:::
:::
:::

::: check
kind: parts
given: P = 1500 | 2500; To = 0 | 5; R = 0.02; C = 2000 | 4000; T = 18 | 21

## A room ({C} J/°C, R = {R} °C/W, outside {To} °C) is at {T} °C with a {P} W heater.

::: part
answer: (P - (T - To)/R)/C
unit: °C/s
tolerance: 0.5%

### How fast does it warm with the heater on?

::: misses
- [any] $(P - (T - T_\text{out})/R)/C$.
:::
:::

::: part
answer: (T - To)/R/C
unit: °C/s
tolerance: 0.5%

### How fast does it cool with the heater off?

::: misses
- [any] Only the loss term.
:::
:::
:::

::: check
kind: parts
given: b = 0.5 | 1; rh = 0.2 | 0.5; rc = 0.3; T = 60 | 600

## A thermostat's band is ±{b} °C; the room warms at {rh} and cools at {rc} °C/s.

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

### How long is a cycle?

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

::: part
answer: floor(T/(2*b/rh + 2*b/rc))
tolerance: 0

### About how many times does the heater switch on in {T} s?

::: misses
- [any] Whole cycles in {T} s.
:::
:::
:::

::: check
kind: parts
given: Ia = 2; d = 0.1 | 1; Is = 0.05 | 0.5; Q = 220 | 2000

## A device is awake {d}% of the time at {Ia} mA and asleep at {Is} mA, from {Q} mAh.

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

### What's its average current?

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

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

### How many days does the battery last?

::: misses
- [any] Capacity over current, then days.
:::
:::
:::

::: check
kind: parts
given: n = 25 | 40; b = 9600 | 115200; T = 1 | 5

## A report of {n} characters goes out at {b} baud every {T} s.

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

### How long does each report take?

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

::: part
answer: 100*n*10/b/T
unit: %
tolerance: 0.5%

### What fraction of the time is the UART sending?

::: misses
- [any] Report time over {T} s.
:::
:::
:::

::: check
kind: parts
given: P = 9999 | 999; N = 99 | 4999; Q = 2 | 4

## A sampling timer has PRESCALE = {P} and TOP = {N}; each sample's I²C read is {Q} bytes at 100 kHz plus two clocks.

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

### How often does it sample?

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

::: part
answer: (9*Q + 2)*10
unit: µs
tolerance: 1%

### How long is each read on the bus?

::: misses
- [any] 9 clocks per byte at 10 µs each, plus two.
:::
:::
:::

::: check
kind: parts
given: g = 10 | 25; m = 3 | 4; h = 300 | 800

## A main loop's longest pass is {g} ms; its watchdog gets a {m}× margin. A bug hangs it {h} ms after each start.

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

### What WDT_LOAD?

::: misses
- [any] g × m ms in cycles.
:::
:::

::: part
answer: floor(10000/(h + g*m))
tolerance: 0

### How many resets in 10 s?

::: misses
- [any] Whole cycles of hang time plus timeout.
:::
:::
:::

::: check
kind: parts
given: t1 = 0.5 | 4; t2 = 0.05 | 0.3; f = 1 | 20

## A task runs {f} times a second: {t1} ms in MicroPython or {t2} ms in C, at 2 mA awake.

::: part
answer: 2*t1*f/1000
unit: mA
tolerance: 0.5%

### What does the MicroPython version add to the average current?

::: misses
- [any] 2 mA × awake fraction.
:::
:::

::: part
answer: 2*(t1 - t2)*f/1000
unit: mA
tolerance: 0.5%

### How much does the C version save?

::: misses
- [any] The difference in awake time.
:::
:::
:::

::: check
kind: parts
given: t = 20 | 60; T = 1 | 5; K = 2 | 8

## A heater-controlled plant (gain 1, τ = {t} s) is sampled every {T} s under proportional gain {K}.

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

### Where is the closed-loop pole?

::: misses
- [any] $a - bK$.
:::
:::

::: part
answer: 100/(1 + K)
unit: %
tolerance: 0.5%

### What steady-state error does it leave?

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

::: check
kind: parts
given: Is = 0.003 | 0.02; Ia = 2 | 4; t = 10 | 100; T = 5 | 60; Q = 220 | 2400

## A logger sleeps at {Is} mA and wakes every {T} s for {t} ms at {Ia} mA, from {Q} mAh.

::: part
answer: Is + (Ia - Is)*t/1000/T
unit: mA
tolerance: 0.5%

### What is the average current?

::: misses
- [any] Sleep plus the awake extra times the duty.
:::
:::

::: part
answer: Q/(Is + (Ia - Is)*t/1000/T)/8766
unit: years
tolerance: 0.5%

### How many years does it last?

::: misses
- [any] Capacity ÷ average ÷ 8766 h.
:::
:::
:::

::: check
kind: parts
given: k = 0.6 | 1.7; q = 8 | 12; e = 50 | 333

## A PI controller's proportional gain {k} is stored in Q{q}; the error is {e} codes.

::: part
answer: floor(round(k*2^q)*e/2^q)
tolerance: 0

### What does the proportional term compute?

::: misses
- [any] (integer gain × error) shifted right by {q}.
:::
:::

::: part
answer: k*e - floor(round(k*2^q)*e/2^q)
tolerance: 2%

### How far is that from the exact $K_pe$?

::: misses
- [any] Exact less computed.
:::
:::
:::
:::
