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

Struggling with basic GPIO read on NRF52

Hello, I am trying to develop on the NRF52. I have the development board and have been spending time with the SDK and documentation. I understand how the GPIO should work, but I can't seem to get it to do a basic read.

I have a five position switch attached to pins 8, 9, and 10. Trying to detect when the pins are high.

I'm using the LED's on the Dev board as debugging tools.


//5 Position Switch Pins
#define SWITCH_0 8
#define SWITCH_1 9
#define SWITCH_2 10

//5 position switch value
uint32_t fiveposition = 0;

static void gpio_init(void)
{
		//initialize the 5 Position Switch GPIO
		nrf_gpio_cfg_sense_input(SWITCH_0, NRF_GPIO_PIN_PULLDOWN, NRF_GPIO_PIN_SENSE_HIGH);
		nrf_gpio_cfg_sense_input(SWITCH_1, NRF_GPIO_PIN_PULLDOWN, NRF_GPIO_PIN_SENSE_HIGH);
		nrf_gpio_cfg_sense_input(SWITCH_2, NRF_GPIO_PIN_PULLDOWN, NRF_GPIO_PIN_SENSE_HIGH);

		//turn on GPIO Nested Vector Inturrupt
		//NRF_GPIOTE->INTENSET = GPIOTE_INTENSET_PORT_Msk;
    //NVIC_EnableIRQ(GPIOTE_IRQn);
	
		//Setup LED's for debugging 5 position switch
		nrf_gpio_cfg_output(BSP_LED_0);
		nrf_gpio_cfg_output(BSP_LED_1);
		nrf_gpio_cfg_output(BSP_LED_2);
		nrf_gpio_cfg_output(BSP_LED_3);
		nrf_gpio_pin_clear(BSP_LED_0);
		nrf_gpio_pin_clear(BSP_LED_1);
		nrf_gpio_pin_clear(BSP_LED_2);
		nrf_gpio_pin_clear(BSP_LED_3);

}

int main(void)
{
		gpio_init();
	
    while (true)
    {
				uint32_t switch0 = 0;
				uint32_t switch1 = 0;
				uint32_t switch2 = 0;
				switch0 = nrf_gpio_pin_read(SWITCH_0);
				switch1 = nrf_gpio_pin_read(SWITCH_1);
				switch2 = nrf_gpio_pin_read(SWITCH_2);
			
				if (switch0 == 1) {
					nrf_gpio_pin_set(BSP_LED_0);
				}
				else {
					nrf_gpio_pin_clear(BSP_LED_0);
				}
				
				if (switch1 == 1) {
					nrf_gpio_pin_set(BSP_LED_1);
				}
				else {
					nrf_gpio_pin_clear(BSP_LED_1);
				}
				
				if (switch2 == 1) {
					nrf_gpio_pin_set(BSP_LED_2);
				}
				else {
					nrf_gpio_pin_clear(BSP_LED_2);
				}			
    }
		
}
Parents
  • I assume the development board you have is the nRF52 DK or nrf52 Preview DK? If so there are a few issues with your code:

    • The switch numbers should be 13, 14 and 15. (You could use the BSP header file as you do with LED's, and use BUTTON_1 etc.)
    • You should enable the internal pullup resistors, not the pulldown's.

    For a real application you would probably be better off using events/interrupts via the GPIOTE driver instead of reading the input pins in your main loop. You could also use PPI which would allow you to control the LED's using buttons without any involvement from the CPU (except the initial configuration).

    (I converted this from a answer to a comment)

Reply
  • I assume the development board you have is the nRF52 DK or nrf52 Preview DK? If so there are a few issues with your code:

    • The switch numbers should be 13, 14 and 15. (You could use the BSP header file as you do with LED's, and use BUTTON_1 etc.)
    • You should enable the internal pullup resistors, not the pulldown's.

    For a real application you would probably be better off using events/interrupts via the GPIOTE driver instead of reading the input pins in your main loop. You could also use PPI which would allow you to control the LED's using buttons without any involvement from the CPU (except the initial configuration).

    (I converted this from a answer to a comment)

Children
No Data
Related