Simvik

Toggle an LED

Flip one pin with a single XOR, whatever its current level. An easy embedded C problem (GPIO), run and graded in the browser.

easy · GPIO · gpio · output · xor

Write gpio_toggle(port, pin): flip the level of one pin and leave the others alone, without reading the pin first to decide.

Calling it twice must restore the original value.

Starter code

void gpio_toggle(volatile uint8_t *port, uint8_t pin) {
  *port |= (uint8_t)(1u << pin);
}

Example tests

Off to on

uint8_t port = 0x00;
gpio_toggle(&port, 2);
show(port);

Expected output:

0x04

On to off

uint8_t port = 0x04;
gpio_toggle(&port, 2);
show(port);

Expected output:

0x00

Learn it first

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

More GPIO problems