0

How do I use an external switch as an interrupt in Nucleo STM32L073RZ microcontroller?

This is my code:

#include "stm32l0xx.h"
#include "stm32l0xx_nucleo.h"
#include "stm32l0xx_hal.h"

static void GPIO_Init(void);

int main(void)
{
 HAL_Init();
 GPIO_Init();
 while(1)
{

 }
}    
static void GPIO_Init(void)
{

GPIO_InitTypeDef GPIO_InitStruct;

/*Configure GPIO pin : PC15 */
GPIO_InitStruct.Pin = GPIO_PIN_15;
GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;
GPIO_InitStruct.Pull = GPIO_PULLUP;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);

/* EXTI interrupt init*/
HAL_NVIC_SetPriority(EXTI2_3_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(EXTI2_3_IRQn);
}



void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin_15)
{
HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5);
}

Also how do I connect the external switch to my board?

Niteesh Shanbog
  • 167
  • 1
  • 1
  • 11

1 Answers1

2

Connect the switch between the GPIO and and ground (as you have set a pull-up).

Not sure your interrupt setup is correct. It looks like PC15 is set to EXTI_15 from the reference manual (I only had a quick glance so I could be wrong here).

Realtime Rik
  • 1,632
  • 10
  • 22
  • Changed it to HAL_NVIC_SetPriority(EXTI4_15_IRQn, 0, 0); HAL_NVIC_EnableIRQ(EXTI4_15_IRQn); Still doesnt work – Niteesh Shanbog Sep 18 '18 at 07:37
  • 1
    Have you turned the peripheral clock on for GPIO C ? Can't remember if the GPIO init does this and dont currently have the code installed to look at. – Realtime Rik Sep 18 '18 at 11:14
  • Yeah, I had done that. On a whim I changed the input to PA_8. And it worked. Don't know why though! Thanks anyway! – Niteesh Shanbog Sep 18 '18 at 11:18