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 (PUPDRvalue01), 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 forDEBOUNCE_SAMPLEScalls in a row. After a press, the button must read high forDEBOUNCE_SAMPLEScalls 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 pressLearn it first
- GPIO · Pin modes, push-pull versus open-drain, pulls, edge interrupts and debouncing.
More GPIO problems
- Update several pins at once · Write new levels to the pins selected by a mask and leave every other pin alone.
- Toggle an LED · Flip one pin with a single XOR, whatever its current level.
- Read an active-low button · A button wired to ground reads 0 when pressed. Report it as pressed.
- Configure pin modes · Set the direction and pull-up bits for one pin as input, input with pull-up or output.
- Driver: drive an LED through GPIO registers · Make a pin an output without disturbing its neighbours, then set, clear and toggle it atomically.
- Fix: the debugger cannot connect any more · Configuring one pin overwrites the mode of every other pin of the port, the debug pins included.
- Smart doorbell: one ring per press · Debounce a bouncing button sampled every millisecond, so one press rings once.