单片机学习小组
直播中

云达贞

7年用户 868经验值
私信 关注

请教大神GPIO-LED点灯实现的步骤有哪些

LED库开发该怎样去实现呢?
请教大神GPIO-LED点灯实现的步骤有哪些?

回帖(1)

李铭鑫

2022-2-28 14:39:42
引言

长话短说今天开始STM32的学习,本阶段的知识是基于ST公司的标准库函的开发:Keil.STM32F4xx_DFP.1.0.8.pack。



  • 所需要的硬件设备:STM32F407ZET6开发板、 J-LINK下载器



    • 软件:keil v5,



安装软件和工程搭建就不多说了,工程模板标准库(Keil.STM32F4xx_DFP.1.0.8.pack)点击下载

下面就开始正文了,激动人心的点灯

LED库开发

LED库开发要添加库文件:stm32f4xx_gpio.c

理解LED灯原理图

LED0连接在PF9引脚

PF9输出VCC(1),灯灭

PF9输出GND(0),灯亮



点灯步骤

1、打开GPIOF组时钟,也叫做使能F组时钟(STM32当中外设的时钟默认不打开,降低功耗)

//使能GPIO F组时钟,
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOx, ENABLE);
2、设置GPIOF9 引脚为输出模式 输出推挽 上拉  速度,并初始化GPIO

GPIO_InitTypeDef  GPIO_InitStruct;

GPIO_InitStruct.GPIO_Pin       = GPIO_Pin_9;   //引脚9
GPIO_InitStruct.GPIO_Mode  = GPIO_Mode_OUT;    //输出模式
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;    //推挽
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; //快速
GPIO_InitStruct.GPIO_PuPd   = GPIO_PuPd_UP;    //上拉
GPIO_Init(GPIOF, &GPIO_InitStruct);
3、通过置位或者复位函数控制引脚电平

GPIO_SetBits();          //置位(1)
GPIO_ResetBits();        //复位(0)
GPIO_ToggleBits();       //电位翻转
GPIO_ReadInputDataBit(); //读取引脚输入电平
GPIO_ReadOutputDataBit();//读取引脚输出电平
GPIO-LED点灯实现例程

led.c

#include "led.h"

/*
引脚说明:
LED0 -- PF9
LED1 -- PF10
LED2 -- PE13
LED3 -- PE14
输出VCC(1),灯灭
输出GND(0),灯亮
*/

void Led_Init(void)
{

        GPIO_InitTypeDef  GPIO_InitStruct;
      
      
        RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE);//使能GPIO F组时钟
        RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);//使能GPIO E组时钟
      
      
        GPIO_InitStruct.GPIO_Pin        = GPIO_Pin_9;                //引脚9
        GPIO_InitStruct.GPIO_Mode        = GPIO_Mode_OUT;        //输出模式
        GPIO_InitStruct.GPIO_OType        = GPIO_OType_PP;        //推挽
        GPIO_InitStruct.GPIO_Speed        = GPIO_Speed_50MHz; //快速
        GPIO_InitStruct.GPIO_PuPd        = GPIO_PuPd_UP;     //上拉
        GPIO_Init(GPIOF, &GPIO_InitStruct);
      
        GPIO_InitStruct.GPIO_Pin        = GPIO_Pin_10;                //引脚10
        GPIO_InitStruct.GPIO_Mode        = GPIO_Mode_OUT;        //输出模式
        GPIO_InitStruct.GPIO_OType        = GPIO_OType_PP;        //推挽
        GPIO_InitStruct.GPIO_Speed        = GPIO_Speed_50MHz; //快速
        GPIO_InitStruct.GPIO_PuPd        = GPIO_PuPd_UP;     //上拉
        GPIO_Init(GPIOF, &GPIO_InitStruct);
      
        GPIO_InitStruct.GPIO_Pin        = GPIO_Pin_13;                //引脚13
        GPIO_InitStruct.GPIO_Mode        = GPIO_Mode_OUT;        //输出模式
        GPIO_InitStruct.GPIO_OType        = GPIO_OType_PP;        //推挽
        GPIO_InitStruct.GPIO_Speed        = GPIO_Speed_50MHz; //快速
        GPIO_InitStruct.GPIO_PuPd        = GPIO_PuPd_UP;     //上拉
        GPIO_Init(GPIOE, &GPIO_InitStruct);

        GPIO_InitStruct.GPIO_Pin        = GPIO_Pin_14;                //引脚9
        GPIO_InitStruct.GPIO_Mode        = GPIO_Mode_OUT;        //输出模式
        GPIO_InitStruct.GPIO_OType        = GPIO_OType_PP;        //推挽
        GPIO_InitStruct.GPIO_Speed        = GPIO_Speed_50MHz; //快速
        GPIO_InitStruct.GPIO_PuPd        = GPIO_PuPd_UP;     //上拉
        GPIO_Init(GPIOE, &GPIO_InitStruct);
      
        //熄灭所有LED灯
        GPIO_SetBits(GPIOF,GPIO_Pin_9);
        GPIO_SetBits(GPIOF,GPIO_Pin_10);
        GPIO_SetBits(GPIOE,GPIO_Pin_13);
        GPIO_SetBits(GPIOE,GPIO_Pin_14);

}
mian.c

#include "stm32f4xx.h"
#include "led.h"

//粗延时
void  delayms(int n)
{
        int i, j;
      
        for(i=0; i
                for(j=0; j<40000; j++);

}

int main(void)
{
        Led_Init();
      
        while(1)
        {
                GPIO_ResetBits(GPIOF, GPIO_Pin_9);
                delayms(500);
                GPIO_SetBits(GPIOF, GPIO_Pin_9);
                delayms(500);
                GPIO_ResetBits(GPIOF, GPIO_Pin_10);
                delayms(500);
                GPIO_SetBits(GPIOF, GPIO_Pin_10);
                delayms(500);
                GPIO_ResetBits(GPIOE, GPIO_Pin_13);
                delayms(500);
                GPIO_SetBits(GPIOE, GPIO_Pin_13);
                delayms(500);
                GPIO_ResetBits(GPIOE, GPIO_Pin_14);
                delayms(500);
                GPIO_SetBits(GPIOE, GPIO_Pin_14);
                delayms(500);
        }
        return 0;
}
举报

更多回帖

×
20
完善资料,
赚取积分