Home | Projects | Notes > MCU Peripheral Drivers > GPIO Application 4: Toggling LED with External Button Interrupt (gpio_04_led_toggle_with_ext_button_interrupt.c)
gpio_04_led_toggle_with_ext_button_interrupt.c)
Connect an external button to PD6 pin and toggle the LED whenever the interrupt is triggered by the button press.
Interrupt should be triggered during the falling edge of the button press.
gpio_04_led_toggle_with_ext_button_interrupt.cPath: Project/Src/
xxxxxxxxxx801/*******************************************************************************2 * File     : gpio_04_led_toggle_with_ext_button_interrupt.c3 * Brief    : Program to toggle LED whenever an interrupt is triggered by the4 *            the external button press5 * Author   : Kyungjae Lee6 * Date     : May 24, 20237 ******************************************************************************/8
9
12
16/**17 * delay()18 * Brief    : Spinlock delays the program execution19 * Param    : None20 * Retval   : None21 * Note     : N/A22 */23void delay(void)24{25    /* Appoximately ~200ms delay when the system clock freq is 16 MHz */26    for (uint32_t i = 0; i < 500000 / 2; i++);27} /* End of delay */28
29
30int main(int argc, char *argv[])31{32    GPIO_Handle_TypeDef GPIOLed, GPIOBtn;33
34    /* Zero-out all the fields in the structures (Very important! GPIOLed and GPIOBtn35     * are local variables whose members may be filled with garbage values before36     * initialization. These garbage values may set (corrupt) the bit fields that37     * you did not touch assuming that they will be 0 by default. Do NOT make this38     * mistake!39     */40    memset(&GPIOLed, 0, sizeof(GPIOLed));41    memset(&GPIOBtn, 0, sizeof(GPIOBtn));42
43    /* GPIOLed configuration */44    GPIOLed.pGPIOx = GPIOD;45    GPIOLed.GPIO_PinConfig.GPIO_PinNumber = GPIO_PIN_12;46    GPIOLed.GPIO_PinConfig.GPIO_PinMode = GPIO_PIN_MODE_OUT;47    GPIOLed.GPIO_PinConfig.GPIO_PinSpeed = GPIO_PIN_OUT_SPEED_HIGH;48    GPIOLed.GPIO_PinConfig.GPIO_PinOutType = GPIO_PIN_OUT_TYPE_PP;49    GPIOLed.GPIO_PinConfig.GPIO_PinPuPdControl = GPIO_PIN_NO_PUPD;50    GPIO_Init(&GPIOLed);51
52    /* GPIOBtn configuration */53    GPIOBtn.pGPIOx = GPIOD;54    GPIOBtn.GPIO_PinConfig.GPIO_PinNumber = GPIO_PIN_6;55    GPIOBtn.GPIO_PinConfig.GPIO_PinMode = GPIO_PIN_MODE_IT_FT;  /* Interrupt falling-edge */56    GPIOBtn.GPIO_PinConfig.GPIO_PinSpeed = GPIO_PIN_OUT_SPEED_HIGH; /* Doesn't matter */57    //GPIOBtn.GPIO_PinConfig.GPIO_PinOutType = GPIO_PIN_OUT_TYPE_PP;    /* N/A */58    GPIOBtn.GPIO_PinConfig.GPIO_PinPuPdControl = GPIO_PIN_PU;59    GPIO_Init(&GPIOBtn);60
61    /* IRQ configurations */62    GPIO_IRQPriorityConfig(IRQ_NO_EXTI9_5, NVIC_IRQ_PRI15); /* Optional in this case */63    GPIO_IRQInterruptConfig(IRQ_NO_EXTI9_5, ENABLE);    /* EXTI 9 to 5 */64
65    while (1);66} /* End of main */67
68/**69 * EXTI9_5_IRQHandler()70 * Brief    : Handles interrupt requests coming through EXTI lines 5-971 * Param    : None72 * Retval   : None73 * Note     : N/A74 */75void EXTI9_5_IRQHandler(void)76{77    delay(); /* Debounding time */78    GPIO_IRQHandling(GPIO_PIN_6);   /* Clear the pending event from EXTI line */79    GPIO_ToggleOutputPin(GPIOD, GPIO_PIN_12);80} /* End of EXTI9_5_IRQHandler */