This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Set and clear GPIO pins at the same time

Hey,

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

Parents
  • 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.

    -Torbjørn

  • Hi NewtoM

    I can't think of any concerns when accessing GPIO->OUT, other than making sure you don't change bits in the register that you are not supposed to change. There is no way to accept multiple answers unfortunately, but RK's answer already got more upvotes so I suggest you accept that ;)

Reply Children
No Data
Related