Simvik

Driver: a debounced button on PC13

Configure an input with its pull-up, then report each press once, ignoring contact bounce. A medium embedded C problem (GPIO), run and graded in the browser.

medium · GPIO · driver · registers · gpio · debounce · pull-up

A push button connects PC13 to ground when pressed. Nothing else is on the pin, so released it floats unless you turn on the chip's pull-up. The port is simulated like an STM32F4's, and the tests play the button.

  • button_init() makes PC13 an input with its internal pull-up (PUPDR value 01), changing no other pin's bits. Pressed then reads 0, released reads 1.
  • button_update() is called once a millisecond. It returns 1 exactly once per press: on the call where PC13 has read low for DEBOUNCE_SAMPLES calls in a row. After a press, the button must read high for DEBOUNCE_SAMPLES calls in a row before another press can count. Otherwise it returns 0.

Contacts bounce: for a few milliseconds after the button moves, the pin flips between levels.

Starter code

void button_init(void) {
}

int button_update(void) {
  return 0;
}

Example tests

One clean press

button_init();
change_t script[] = { { 10, 1 }, { 40, 0 } };
run(script, 2, 60);

Expected output:

press at 14 ms
1 press

Learn it first

  • GPIO · Pin modes, push-pull versus open-drain, pulls, edge interrupts and debouncing.

More GPIO problems