Using external switch as interrupt

496 views Asked by At

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?

1

There are 1 answers

3
Realtime Rik On BEST ANSWER

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).