STM32 IO Operation (LED Lighting)

Language:
C
28 views
0 favorites
6 hours ago

Code Implementation

C
#include "stm32f1xx_hal.h"

int main(void) {
    HAL_Init(); // Initialize the HAL library
    SystemClock_Config(); // Configure the system clock

    __HAL_RCC_GPIOC_CLK_ENABLE(); // Enable the GPIOC clock

    GPIO_InitTypeDef GPIO_InitStruct = {0};
    GPIO_InitStruct.Pin = GPIO_PIN_13; // Select the PC13 pin
    GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; // Configure as push-pull output
    GPIO_InitStruct.Pull = GPIO_NOPULL; // No pull-up/down resistor
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; // Low-speed mode
    HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); // Initialize the GPIO

    while (1) {
        HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_13); // Toggle the state of PC13 pin
        HAL_Delay(500); // Delay for 500ms
    }
}

The following is a simple STM32 program example that uses the HAL library to light up an LED.

#STM32#LED

Snippet Description

  • In actual development, adjust the pin and clock configurations according to your specific STM32 model.
  • After compiling the program, burn it into the STM32 chip using a debugging tool such as ST-Link or J-Link. If everything works properly, you will see the LED blink at 500ms intervals.

Comments

Loading...