Home | Projects | Notes > MCU Peripheral Drivers > GPIO Application 1: Toggling On-board LED (gpio_01_led_toggle_pushpull.c
, gpio_01_led_toggle_opendrain.c
)
gpio_01_led_toggle_pushpull.c
, gpio_01_led_toggle_opendrain.c
)
Write a program to toggle the on-board LED with some delay.
Case 1: Use push pull configuration for the output pin
Case 2: Use open drain configuration of the output pin
gpio_01_led_toggle_pushpull.c
Path: Project/Src/
xxxxxxxxxx
441/*******************************************************************************
2 * File : gpio_01_led_toggle_pushpull.c
3 * Brief : Program to toggle the on-board LED
4 (Push-pull config for output pin)
5 * Author : Kyungjae Lee
6 * Date : May 23, 2023
7 ******************************************************************************/
8
9
10
11/**
12 * delay()
13 * Brief : Spinlock delays the program execution
14 * Param : None
15 * Retval : None
16 * Note : N/A
17 */
18void delay(void)
19{
20 /* Appoximately ~200ms delay when the system clock freq is 16 MHz */
21 for (uint32_t i = 0; i < 500000 / 2; i++);
22} /* End of delay */
23
24int main(int argc, char *argv[])
25{
26 GPIO_Handle_TypeDef GPIOLed;
27
28 GPIOLed.pGPIOx = GPIOD;
29 GPIOLed.GPIO_PinConfig.GPIO_PinNumber = GPIO_PIN_12;
30 GPIOLed.GPIO_PinConfig.GPIO_PinMode = GPIO_PIN_MODE_OUT;
31 GPIOLed.GPIO_PinConfig.GPIO_PinSpeed = GPIO_PIN_OUT_SPEED_HIGH;
32 GPIOLed.GPIO_PinConfig.GPIO_PinOutType = GPIO_PIN_OUT_TYPE_PP;
33 GPIOLed.GPIO_PinConfig.GPIO_PinPuPdControl = GPIO_PIN_NO_PUPD; /* Push-pull, no pupd necessary */
34
35 GPIO_Init(&GPIOLed);
36
37 while (1)
38 {
39 GPIO_ToggleOutputPin(GPIOLed.pGPIOx, GPIOLed.GPIO_PinConfig.GPIO_PinNumber);
40 delay();
41 }
42
43 return 0;
44} /* End of main */
gpio_01_led_toggle_opendrain.c
Path: Project/Src/
xxxxxxxxxx
531/*******************************************************************************
2 * File : gpio_01_led_toggle_opendrain.c
3 * Brief : Program to toggle the on-board LED
4 * (Open-drain config for output pin)
5 * Author : Kyungjae Lee
6 * Date : May 23, 2023
7 ******************************************************************************/
8
9
10
11/**
12 * delay()
13 * Brief : Spinlock delays the program execution
14 * Param : None
15 * Retval : None
16 * Note : N/A
17 */
18void delay(void)
19{
20 /* Appoximately ~200ms delay when the system clock freq is 16 MHz */
21 for (uint32_t i = 0; i < 500000 / 2; i++);
22} /* End of delay */
23
24
25int main(int argc, char *argv[])
26{
27 GPIO_Handle_TypeDef GPIOLed;
28
29 GPIOLed.pGPIOx = GPIOD;
30 GPIOLed.GPIO_PinConfig.GPIO_PinNumber = GPIO_PIN_12;
31 GPIOLed.GPIO_PinConfig.GPIO_PinMode = GPIO_PIN_MODE_OUT;
32 GPIOLed.GPIO_PinConfig.GPIO_PinSpeed = GPIO_PIN_OUT_SPEED_HIGH;
33 GPIOLed.GPIO_PinConfig.GPIO_PinOutType = GPIO_PIN_OUT_TYPE_OD; /* Open-drain config */
34 GPIOLed.GPIO_PinConfig.GPIO_PinPuPdControl = GPIO_PIN_NO_PUPD;
35 /* Even if you use internal pull-up resistor (GPIO_PIN_PU), the LED will toggle very dim.
36 * It is because of the high resistance of the internal pull-up resistor. Instead of
37 * using the internal pull-up resistor, add an external 470 ohm resistor with a
38 * jumper wire between PD12 and +5V. Then, the LED will toggle with normal brightness.
39 *
40 * For these reasons, it is not a good idea to use open-drain configuration for
41 * a GPIO output pin in general unless it is required by the requirements.
42 */
43
44 GPIO_Init(&GPIOLed);
45
46 while (1)
47 {
48 GPIO_ToggleOutputPin(GPIOLed.pGPIOx, GPIOLed.GPIO_PinConfig.GPIO_PinNumber);
49 delay();
50 }
51
52 return 0;
53} /* End of main */