stm32单片机点灯代码

今天水一个博客:stm32单片机点灯代码

很简单吧,线上代码,一会再讲

#include "stm32f4xx.h"

int main()
{
    // 使能GPIOC外设
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);

    // 配置PC13口位GPIO输出口
    GPIO_InitTypeDef GPIO_InitStructure;
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
    GPIO_Init(GPIOC, &GPIO_InitStructure);

    while(1)
    {
        // 点亮LED
        GPIO_SetBits(GPIOC, GPIO_Pin_13);
      
        // 等待一段时间
        for(int i=0; i<1000000; i++);
      
        // 关闭LED
        GPIO_ResetBits(GPIOC, GPIO_Pin_13);
      
        // 等待一段时间
        for(int i=0; i<1000000; i++);
    }
}

注意啦,因为GPIOC口连接了开发板上的一个LED灯泡,所以这个程序可以通过点亮或关闭该LED来测试系统是否正常工作,程序通过使用GPIO_SetBits()和GPIO_ResetBits()函数来控制GPIO口的高电平或低电平状态。最后的for循环,是让实现程序等待时间的效果,但实际上可以使用更加高效的系统定时器来实现更加灵活的精确等待。

猜你喜欢

转载自blog.csdn.net/qiuweichen1215/article/details/129771124