STM32 IO操作(点亮LED)

语言:
C
28 浏览
0 收藏
6小时前

代码实现

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
    }
}

以下是一个简单的STM32程序示例,使用HAL库点亮一个LED

#STM32#LED

片段说明

  • 在实际开发中,请根据您的具体STM32型号调整引脚和时钟配置。
  • 将程序编译后,通过ST-Link或J-Link等调试工具烧录到STM32芯片中。如果一切正常,您将看到LED以500ms的间隔闪烁。

评论

加载中...