使用库函数点亮LED

使用库函数点亮LED灯

1.资料使用

  1. 复制一份资料中的程序

  2. 打开Template.uvprojx

  3. 删除多余的.C文件

    仅保留:1.stm32f4xx_rcc.c //任何工程都不能删掉

    ​ 2.stm32f4xx_gpio.c

    ​ 3.stm32f4xx_ustart.c //串口的

    ​ 4.misc.c

2.程序书写规范

  1. 在Template.uvprojx所在的文件中新建APP文件
  2. 在APP文件中新建LED文件夹

3.新建初始化引脚

  1. 新建led.h

    #ifndef __LED_H
    #define __LED_H
    
    void LED_Init(void);//IO口的初始化
    
    #endif
    
  2. 新建led.c

    #include "led.h"
    #include "stm32f4xx.h"//stm32f4顶层头文件
    void LED_Init(void)
    {
    	//1.调用时钟使能函数
    	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF,ENABLE);
        
    	//2.GPIO初始化
        GPIO_InitTypeDef GPIO_InitStructure;
        GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9;//管脚设置
        GPIO_InitStructure.GPIO_Mode=GPIO_Mode_OUT;//输出模式
        GPIO_InitStructure.GPIO_Speed=GPIO_Speed_100MHz;//速度为100MHZ
        GPIO_InitStructure.GPIO_OType=GPIO_OType_PP;//推挽输出
        GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_UP;//上拉
        GPIO_Init(GPIOF,&GPIO_InitStructure);//初始化结构体
        GPIO_SetBits(GPIOF,GPIO_Pin_9);//设置高电平的函数
        
        GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;//管脚设置
        GPIO_Init(GPIOF,&GPIO_InitStructure);//初始化结构体
        GPIO_SetBits(GPIOF,GPIO_Pin_10);//设置高电平的函数
    }
    

4.编写主函数

//包含头文件
#include "stm32f4xx.h"
#include "led.h"//添加Path
#include "delay.h"
int main()
{
    //初始化延时函数
    delay_intit(168);//默认设置时钟
    LED_Init();
    while(1)
    {
        GPIO_SetBits(GPIOF,GPIO_Pin_9);//设置高电平的函数
        GPIO_SetBits(GPIOF,GPIO_Pin_10);//设置高电平的函数
        delay_ms(500);
        GPIO_ResetBits(GPIOF,GPIO_Pin_9);//点亮LED
        GPIO_ResetBits(GPIOF,GPIO_Pin_10);//点亮LED
        delay_ms(500);
    }
}

5.实验效果(不能放视频)

在这里插入图片描述

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值