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

Read GPIO Output State

Hi,

What is the best way to read the state of an output pin? I have an LED connected to a pin and want to know in a different part of the program whether the LED is currently on or off.

Thanks,

Parents
  • You can just check the same register that you wrote to. Are you using nrf_gpio_pin_write? If you dig into how it works, you can find the OUT, OUTSET, and OUTCLR registers. The OUTSET and OUTCLR registers simply modify the OUT register, which holds the current output state.

    In conclusion, NRF_GPIO->OUT is the register you're looking for, and you just need to mask it to get the correct bit. The masking can also be found in nrf_gpio.h, and will look something like this: ((NRF_GPIO->OUT >> pin_number) & 1UL).

Reply
  • You can just check the same register that you wrote to. Are you using nrf_gpio_pin_write? If you dig into how it works, you can find the OUT, OUTSET, and OUTCLR registers. The OUTSET and OUTCLR registers simply modify the OUT register, which holds the current output state.

    In conclusion, NRF_GPIO->OUT is the register you're looking for, and you just need to mask it to get the correct bit. The masking can also be found in nrf_gpio.h, and will look something like this: ((NRF_GPIO->OUT >> pin_number) & 1UL).

Children
Related