以下是使用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/hp6N 著作权归作者所有。请勿转载和采集!

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