Home | Projects | Notes > MCU Peripheral Drivers > GPIO Application 3: Toggling External LED with External Button (gpio_03_ext_led_toggle_with_ext_button.c
)
gpio_03_ext_led_toggle_with_ext_button.c
)
Connect an external button to the GPIO pin PB12 of the board and an external LED to the GPIO pin PA08, write a program to toggle the external LED whenever the external button is pressed.
Circuit design
When the button is pressed, the pin will be pulled to GND. If the button is released, it will be pulled to HIGH. (Without the external pull-up resistor (22Kohm), the pin state when the button is not pressed will be floating!)
Avoid the floating state by giving a proper state for the pin, either HIGH or LOW when the button is not pressed. In this case, LOW is when the button is pressed. So, make the pin HIGH when the button is not pressed through a pull-up resistor.
Another way is to enable the internal pull-up resistor (which is 40kohm) of the PB12 pin instead of using the external pull-up resistor.
Not all GPIO pins are available for custom use. Make sure to check the documentation before selecting the GPIO pins to use for your project.
gpio_03_ext_led_toggle_with_ext_button.c
Path: Project/Src/
xxxxxxxxxx
611/*******************************************************************************
2 * File : gpio_03_ext_led_toggle_with_ext_button.c
3 * Brief : Program to toggle the external LED whenever the external LED is pressed
4 * Author : Kyungjae Lee
5 * Date : May 24, 2023
6 ******************************************************************************/
7
8
9
10
11
12
13
14/**
15 * delay()
16 * Brief : Spinlock delays the program execution
17 * Param : None
18 * Retval : None
19 * Note : N/A
20 */
21void delay(void)
22{
23 /* Appoximately ~200ms delay when the system clock freq is 16 MHz */
24 for (uint32_t i = 0; i < 500000 / 2; i++);
25} /* End of delay */
26
27
28int main(int argc, char *argv[])
29{
30 GPIO_Handle_TypeDef GPIOLed, GPIOBtn;
31
32 /* GPIOLed configuration */
33 GPIOLed.pGPIOx = GPIOA;
34 GPIOLed.GPIO_PinConfig.GPIO_PinNumber = GPIO_PIN_8;
35 GPIOLed.GPIO_PinConfig.GPIO_PinMode = GPIO_PIN_MODE_OUT;
36 GPIOLed.GPIO_PinConfig.GPIO_PinSpeed = GPIO_PIN_OUT_SPEED_HIGH;
37 GPIOLed.GPIO_PinConfig.GPIO_PinOutType = GPIO_PIN_OUT_TYPE_PP;
38 GPIOLed.GPIO_PinConfig.GPIO_PinPuPdControl = GPIO_PIN_NO_PUPD;
39 GPIO_Init(&GPIOLed);
40
41 /* GPIOBtn configuration */
42 GPIOBtn.pGPIOx = GPIOB;
43 GPIOBtn.GPIO_PinConfig.GPIO_PinNumber = GPIO_PIN_12;
44 GPIOBtn.GPIO_PinConfig.GPIO_PinMode = GPIO_PIN_MODE_IN;
45 GPIOBtn.GPIO_PinConfig.GPIO_PinSpeed = GPIO_PIN_OUT_SPEED_HIGH; /* Doesn't matter */
46 //GPIOBtn.GPIO_PinConfig.GPIO_PinOutType = GPIO_PIN_OUT_TYPE_PP; /* N/A */
47 GPIOBtn.GPIO_PinConfig.GPIO_PinPuPdControl = GPIO_PIN_PU;
48 /* External pull-down resistor is already present (see the schematic) */
49 GPIO_Init(&GPIOBtn);
50
51 while (1)
52 {
53 if (GPIO_ReadFromInputPin(GPIOBtn.pGPIOx, GPIOBtn.GPIO_PinConfig.GPIO_PinNumber) == BTN_PRESSED)
54 {
55 delay(); /* Introduce debouncing time */
56 GPIO_ToggleOutputPin(GPIOLed.pGPIOx, GPIOLed.GPIO_PinConfig.GPIO_PinNumber);
57 }
58 }
59
60 return 0;
61} /* End of main */