Set and clear GPIO pins at the same time


currently I use nrf_gpio_pins_set() and and nrf_gpio_pins_clear() from nrf_gpio.h to set and clear specific GPIO ports, respectively. If I want to change the current state of -say- 4 GPIO ports at once (some of them may be cleared, some of them may be set), how do I do that? It would be important that the change on all pins happens at the same time.

Any ideas? Thanks, NewtoM

  • Hi NewtoM

    If you want to do this you probably have to bypass the nrf_gpio functions, and write directly to the GPIO registers. Fortunately it isn't as scary as it sounds ;)

    To set/clear many pins at once you can write to the OUTSET or OUTCLR registers in the NRF_GPIO peripheral.

    If you want to set and clear many pins at once you have to use the OUT register.

    Say you want to set pins P0.08-P0.011 to 0xA you could do it like this:

    pin_mask = 0xF << 8;
    NRF_GPIO->OUT = (NRF_GPIO->OUT & ~pin_mask) | (0xA << 8);

    You can still use the nrf_gpio driver to configure the outputs, and set/clear individual pins.


Reply Children
No Data