---
title: Problem set 2
heading: "Problem set 2: inputs, debouncing and debugging"
eyebrow: C12 · Problem set 2
type: problems
---

::: hook
These problems use lessons 7 to 9: digital inputs and pull-ups, debouncing, and debugging. The board runs from 3.3 V; a pin reads 1 above 0.7 × 3.3 = 2.31 V and 0 below 0.3 × 3.3 = 0.99 V. It runs at 10 MHz, one instruction per cycle, and its memory is little-endian.
:::

::: pool
pick: 12

::: check
kind: parts
given: n = 4 | 8 | 16; R = 4.7 | 10 | 47; h = 2 | 24

## A keypad has {n} buttons, each pulling its own {R} kΩ pull-up to ground when pressed. A user holds one down for {h} hours a day.

::: part
answer: 3.3/R
unit: mA
tolerance: 0.5%

### What current does a pressed button draw?

::: misses
- [any] 3.3 V over the pull-up.
:::
:::

::: part
answer: 3.3/R*h
unit: mAh
tolerance: 0.5%

### How much charge does that take from the battery each day?

::: misses
- [any] Current × hours.
:::
:::

::: part
answer: 3.3/R*n
unit: mA
tolerance: 0.5%

### What if all {n} were held at once?

::: misses
- [any] {n} times one button's current.
:::
:::
:::

::: check
kind: parts
given: V = 5 | 12 | 24; R1 = 10 | 47 | 100

## A {V} V logic signal must drive a 3.3 V pin through a divider: {R1} kΩ in series, then a resistor to ground. Its high level should land at 3.0 V.

::: part
answer: R1*3.0/(V - 3.0)
unit: kΩ
tolerance: 0.5%

### What resistor to ground?

::: misses
- [R1*3.0/V] The pin's voltage is $V R_2/(R_1 + R_2)$; solve for $R_2$.
- [any] $R_2 = R_1 \cdot 3.0/(V - 3.0)$.
:::
:::

::: part
answer: R1*(R1*3.0/(V - 3.0))/(R1 + R1*3.0/(V - 3.0))
unit: kΩ
tolerance: 0.5%

### What source resistance does the pin then see?

::: misses
- [any] $R_1 \parallel R_2$.
:::
:::
:::

::: check
kind: parts
given: R = 2.2 | 4.7 | 10; C = 50 | 200 | 400

## An open-drain line has a {R} kΩ pull-up to 3.3 V and {C} pF of capacitance. It's released from 0 V.

::: part
answer: R*1000*C*1e-12*ln(3.3/(3.3 - 2.31))*1e9
unit: ns
tolerance: 0.5%

### How long until it reads 1?

::: misses
- [R*1000*C*1e-12*1e9] One time constant isn't enough: the pin must reach 2.31 V.
- [any] $RC\ln(3.3/(3.3 - 2.31))$.
:::
:::

::: part
answer: 1/(2*R*1000*C*1e-12*ln(3.3/(3.3 - 2.31)))/1000
unit: kHz
tolerance: 0.5%

### If the line must be high at least half of each period and the rise takes up all of that half, what is the fastest square wave it can carry?

::: misses
- [any] Period = 2 × the rise time.
:::
:::
:::

::: check
kind: parts
given: R = 47 | 100 | 470; L = 1 | 5 | 10

## A pin leaks up to {L} µA to ground, and its pull-up is {R} kΩ.

::: part
answer: 3.3 - L*R/1000
unit: V
tolerance: 0.5%

### What voltage does the released pin sit at?

::: misses
- [any] 3.3 V less the leakage's drop across the pull-up.
:::
:::

::: part
answer: step(3.3 - L*R/1000 - 2.31)
tolerance: 0

### Does it still read 1 (1 for yes)?

::: misses
- [any] Compare with 2.31 V.
:::
:::

::: part
answer: (3.3 - 2.31)/L*1000
unit: kΩ
tolerance: 0.5%

### What is the largest pull-up that keeps it reading 1?

::: misses
- [any] $(3.3 - 2.31)/I_\text{leak}$.
:::
:::
:::

::: check
kind: parts
given: R = 10 | 47 | 100; C = 0.1 | 1

## A button discharges a capacitor of {C} µF through {R} kΩ as a hardware debouncer; the pin starts at 3.3 V.

::: part
answer: R*1000*C*1e-6*ln(3.3/0.99)*1000
unit: ms
tolerance: 0.5%

### How long after the press does the pin fall below 0.99 V?

::: misses
- [any] $RC\ln(3.3/0.99)$.
:::
:::

::: part
answer: R*1000*C*1e-6*ln((3.3 - 0.1)/(3.3 - 0.99))*1000
unit: ms
tolerance: 0.5%

### If a bounce opens the contact once the pin has reached 0.1 V, how long must it stay open to recharge the pin past 0.99 V? (The pin then charges back toward 3.3 V through the same resistance.)

::: misses
- [any] $RC\ln((3.3 - 0.1)/(3.3 - 0.99))$.
:::
:::
:::

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

## A counter debouncer samples every {T} ms and accepts a new level after {N} equal samples. A switch bounces for up to {b} ms.

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

### What is its worst-case delay after the bouncing ends?

::: misses
- [any] {N} samples × {T} ms.
:::
:::

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

### And after the first contact?

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

::: part
answer: 30*1000/T/1e7*100
unit: %
tolerance: 0.5%

### If each sample is a 30-cycle timer interrupt, what share of the 10 MHz processor does it take?

::: misses
- [any] Samples per second × 30 cycles ÷ $10^7$.
:::
:::
:::

::: check
kind: parts
given: k = 3 | 10 | 25; c = 40 | 120; p = 2 | 6

## A button bounces {k} times on each press and each release, so each press and release makes $1 + 2k$ falling edges. A falling-edge interrupt of {c} cycles fires on every falling edge; it's pressed {p} times a second.

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

### How many falling edges arrive per second?

::: misses
- [(k + 1)*p] Bounces on release make falling edges too.
- [any] $1 + 2k$ per press.
:::
:::

::: part
answer: (2*k + 1)*p*c/1e7*100
unit: %
tolerance: 0.5%

### What share of the 10 MHz processor do they use?

::: misses
- [any] Edges × cycles ÷ $10^7$.
:::
:::
:::

::: check
kind: parts
given: B = 8192 | 12288; s = 12 | 16 | 24; i = 3 | 7; o = 4 | 8

## An array of structs, each {s} bytes, starts at address {B}. A field sits at offset {o} in each struct.

::: part
answer: B + i*s + o
tolerance: 0

### What address holds the field of element {i}?

::: misses
- [B + i + o] Multiply the index by the struct's size.
- [any] base + i × size + offset.
:::
:::

::: part
answer: floor((B + i*s + o)/4)*4
tolerance: 0

### A watchpoint covers aligned 4-byte words. What address does it watch?

::: misses
- [any] Round down to a multiple of 4.
:::
:::
:::

::: check
kind: parts
given: a = 10 | 50; b = 20 | 100; k = 3 | 7

## A conditional breakpoint `if (j == {k})` sits in an inner loop `for j < {b}` inside an outer loop `for i < {a}`.

::: part
answer: a
tolerance: 0

### How many times does it stop?

::: misses
- [a*b] Only when j equals {k}, once per outer pass.
- [any] Once per pass of the outer loop.
:::
:::

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

### How many times would an unconditional one stop?

::: misses
- [any] Every inner pass.
:::
:::
:::

::: check
kind: parts
given: b0 = 52 | 120; b1 = 18 | 200; b2 = 0 | 7; b3 = 0 | 1

## A memory view shows the bytes {b0}, {b1}, {b2}, {b3} (in decimal, lowest address first) at the address of a `uint32_t`.

::: part
answer: b0 + 256*b1 + 65536*b2 + 16777216*b3
tolerance: 0

### What value does it hold?

::: misses
- [b3 + 256*b2 + 65536*b1 + 16777216*b0] Little-endian: the first byte is the least significant.
- [any] $b_0 + 256b_1 + 65536b_2 + 2^{24}b_3$.
:::
:::

::: part
answer: b0 + 256*b1
tolerance: 0

### What does a `uint16_t` at the same address hold?

::: misses
- [any] The first two bytes.
:::
:::
:::

::: check
kind: parts
given: n = 20 | 60; b = 9600 | 115200; D = 5 | 20

## A control loop has a {D} ms deadline. A debug print of {n} characters at {b} baud is added to it, waiting for each character.

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

### How long does the print take?

::: misses
- [n*8/b*1000] Ten bits per character on the wire.
- [any] $10n/\text{baud}$.
:::
:::

::: part
answer: step(D - n*10/b*1000)
tolerance: 0

### Does the print alone fit in the deadline (1 for yes)?

::: misses
- [any] Compare it with {D} ms.
:::
:::
:::

::: check
kind: parts
given: S = 512 | 1024 | 2048; u = 200 | 450; m = 25 | 50

## A stack of {S} bytes is painted at reset. After testing, {u} bytes at the bottom are untouched. The team wants a {m}% margin over the high-water mark.

::: part
answer: S - u
unit: bytes
tolerance: 0

### What is the high-water mark?

::: misses
- [any] Size less untouched.
:::
:::

::: part
answer: (S - u)*(1 + m/100)
unit: bytes
tolerance: 0.5%

### What stack size meets the margin?

::: misses
- [any] Mark × (1 + margin).
:::
:::

::: part
answer: S - (S - u)*(1 + m/100)
unit: bytes
tolerance: 0.5%

### How much RAM does resizing free (negative if more is needed)?

::: misses
- [any] Old size less new size.
:::
:::
:::

::: check
kind: parts
given: w = 0.5 | 2 | 10; n = 4 | 10; T = 0.1 | 1

## A logic analyzer must catch pulses {w} µs wide with at least {n} samples each, over a {T} s capture.

::: part
answer: n/w
unit: MHz
tolerance: 0.5%

### What sample rate does it need?

::: misses
- [any] {n} samples per pulse width.
:::
:::

::: part
answer: n/w*1e6*T
unit: samples
tolerance: 0.5%

### How many samples per channel does the capture hold?

::: misses
- [any] Rate × duration.
:::
:::
:::

::: check
kind: parts
given: R = 10 | 22 | 47; C = 10 | 47; f = 1 | 5

## A {R} kΩ pull-up and a {C} pF pin are toggled by an open-drain output as a {f} kHz square wave.

::: part
answer: R*1000*C*1e-12*ln(3.3/(3.3 - 2.31))*1e6
unit: µs
tolerance: 0.5%

### How long does each rise take to reach a logic 1?

::: misses
- [any] $RC\ln(3.3/0.99)$.
:::
:::

::: part
answer: R*1000*C*1e-12*ln(3.3/(3.3 - 2.31))*1e6/(500/f)*100
unit: %
tolerance: 0.5%

### What share of the high half-period does that delay take?

::: misses
- [any] Rise time ÷ half the period.
:::
:::
:::

::: check
kind: parts
given: hi = 1.8 | 2.0; lo = 1.2 | 1.4; t = 1 | 10

## A Schmitt-trigger input switches high above {hi} V rising and low below {lo} V falling. A slow sensor drives it with $3.3(1 - e^{-t/\tau})$, τ = {t} ms.

::: part
answer: t*ln(3.3/(3.3 - hi))
unit: ms
tolerance: 0.5%

### When does it switch high?

::: misses
- [any] Solve $3.3(1 - e^{-t/\tau}) = V_{T+}$.
:::
:::

::: part
answer: hi - lo
unit: V
tolerance: 0.5%

### What is the largest noise, peak to peak, that can't make it switch back once it's high and settled near its switching point?

::: misses
- [any] The hysteresis: the gap between the thresholds.
:::
:::
:::
:::
