以下是使用 STM32F103C8T6 全部引脚的 LED 跑马灯程序示例:

#include "stm32f10x.h"

int main(void)
{
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE | RCC_APB2Periph_GPIOF | RCC_APB2Periph_GPIOG, ENABLE);

  GPIO_InitTypeDef GPIO_InitStructure;

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
  GPIO_Init(GPIOB, &GPIO_InitStructure);
  GPIO_Init(GPIOC, &GPIO_InitStructure);
  GPIO_Init(GPIOD, &GPIO_InitStructure);
  GPIO_Init(GPIOE, &GPIO_InitStructure);
  GPIO_Init(GPIOF, &GPIO_InitStructure);
  GPIO_Init(GPIOG, &GPIO_InitStructure);

  while (1)
  {
    GPIO_SetBits(GPIOA, GPIO_Pin_All);
    GPIO_SetBits(GPIOB, GPIO_Pin_All);
    GPIO_SetBits(GPIOC, GPIO_Pin_All);
    GPIO_SetBits(GPIOD, GPIO_Pin_All);
    GPIO_SetBits(GPIOE, GPIO_Pin_All);
    GPIO_SetBits(GPIOF, GPIO_Pin_All);
    GPIO_SetBits(GPIOG, GPIO_Pin_All);

    for (int i = 0; i < 1000000; i++);

    GPIO_ResetBits(GPIOA, GPIO_Pin_All);
    GPIO_ResetBits(GPIOB, GPIO_Pin_All);
    GPIO_ResetBits(GPIOC, GPIO_Pin_All);
    GPIO_ResetBits(GPIOD, GPIO_Pin_All);
    GPIO_ResetBits(GPIOE, GPIO_Pin_All);
    GPIO_ResetBits(GPIOF, GPIO_Pin_All);
    GPIO_ResetBits(GPIOG, GPIO_Pin_All);

    for (int i = 0; i < 1000000; i++);
  }
}

该程序使用了 STM32F103C8T6 芯片的全部引脚来控制 LED 灯,实现了一个跑马灯效果。程序中使用了 RCC_APB2PeriphClockCmd 函数来使能各个 GPIO 的时钟,然后使用 GPIO_InitTypeDef 结构体来初始化 GPIO 的模式、速度和输出状态。在主循环中,先将所有的 LED 都设置为高电平,然后延时一段时间,再将所有 LED 都设置为低电平,再延时一段时间,以此循环。这样就能让 LED 灯在全部引脚上跑马灯效果。

需要注意的是,由于程序使用了全部引脚,因此需要将板子上其他的功能引脚(例如 SWD 调试接口)全部拔掉,否则可能会影响程序的正常运行。

STM32F103C8T6 全部引脚 LED 跑马灯程序示例

原文地址: https://www.cveoy.top/t/topic/oSWz 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录