Simvik

Update several pins at once

Write new levels to the pins selected by a mask and leave every other pin alone. An easy embedded C problem (GPIO), run and graded in the browser.

easy · GPIO · gpio · output · masks

A port's 8-bit output register drives eight pins, and a 4-LED bar uses pins 2 to 5. Write gpio_write_masked(port, mask, value):

  • every pin whose bit is 1 in mask takes the level of the same bit in value,
  • every pin whose bit is 0 in mask keeps its level.

port points at the output register, so it is volatile.

Starter code

void gpio_write_masked(volatile uint8_t *port, uint8_t mask, uint8_t value) {
  *port = value;
}

Example tests

Set a group of pins

uint8_t port = 0x00;
gpio_write_masked(&port, 0x3C, 0xFF);
show(port);

Expected output:

0x3C

Change only the selected pins

uint8_t port = 0xC3;
gpio_write_masked(&port, 0x3C, 0x28);
show(port);

Expected output:

0xEB

Learn it first

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

More GPIO problems