使用库函数点亮LED灯
1.资料使用
-
复制一份资料中的程序
-
打开Template.uvprojx
-
删除多余的.C文件
仅保留:1.stm32f4xx_rcc.c //任何工程都不能删掉
2.stm32f4xx_gpio.c
3.stm32f4xx_ustart.c //串口的
4.misc.c
2.程序书写规范
- 在Template.uvprojx所在的文件中新建APP文件
- 在APP文件中新建LED文件夹
3.新建初始化引脚
-
新建led.h
#ifndef __LED_H #define __LED_H void LED_Init(void);//IO口的初始化 #endif
-
新建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);
}
}