Simvik

Configure pin modes

Set the direction and pull-up bits for one pin as input, input with pull-up or output. A medium embedded C problem (GPIO), run and graded in the browser.

medium · GPIO · gpio · direction · pull-up · registers

A port has a direction register ddr (1 = output, 0 = input) and an output register port. On this part, writing 1 to the output bit of an input pin turns on its pull-up.

Write gpio_configure(ddr, port, pin, mode):

modeddr bitport bit
MODE_INPUT00 (no pull-up)
MODE_INPUT_PULLUP01
MODE_OUTPUT1unchanged

Only the bits of pin may change.

Starter code

void gpio_configure(volatile uint8_t *ddr, volatile uint8_t *port, uint8_t pin, pin_mode_t mode) {
  *ddr = 0;
  *port = 0;
}

Example tests

Output

uint8_t ddr = 0x00, port = 0x00;
gpio_configure(&ddr, &port, 5, MODE_OUTPUT);
show(ddr, port);

Expected output:

ddr=0x20 port=0x00

Input with pull-up

uint8_t ddr = 0xFF, port = 0x00;
gpio_configure(&ddr, &port, 0, MODE_INPUT_PULLUP);
show(ddr, port);

Expected output:

ddr=0xFE port=0x01

Learn it first

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

More GPIO problems