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.cPath: Project/Src/
xxxxxxxxxx441/*******************************************************************************2 * File : gpio_01_led_toggle_pushpull.c3 * Brief : Program to toggle the on-board LED4 (Push-pull config for output pin)5 * Author : Kyungjae Lee6 * Date : May 23, 20237 ******************************************************************************/8
910
11/**12 * delay()13 * Brief : Spinlock delays the program execution14 * Param : None15 * Retval : None16 * Note : N/A17 */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.cPath: Project/Src/
xxxxxxxxxx531/*******************************************************************************2 * File : gpio_01_led_toggle_opendrain.c3 * Brief : Program to toggle the on-board LED4 * (Open-drain config for output pin)5 * Author : Kyungjae Lee6 * Date : May 23, 20237 ******************************************************************************/8
910
11/**12 * delay()13 * Brief : Spinlock delays the program execution14 * Param : None15 * Retval : None16 * Note : N/A17 */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 of37 * using the internal pull-up resistor, add an external 470 ohm resistor with a38 * 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 for41 * 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 */