STM32F103(库函数)——点亮LED并且使用软件延时实现led闪烁

版权声明:让结局不留遗憾,让过程更加完美。 https://blog.csdn.net/Xiaomo_haa/article/details/86666005

当我们学过51单片机之后就知道在51单片机中点亮一个LED很简单一句代码就可以实现。只需要将LED所连接的IO口拉低就可以点亮LED。

但是在STM32中,点亮LED确实比51要麻烦很多。

一样,在STM32中点亮LED也是属于基本的IO口的使用,所以每当我们需要点亮LED时就需要正确配置IO口。STM32的IO口相比51单片机而言要复杂很多,所以用起来也很困难。

下面就是点亮LED的代码,并且使用延时实现LED闪烁。

main.c

#include "stm32f10x.h"
#include "led.h"
#include "delay.h"
#include "sys.h"

int main(void)
{
	LED_Init();
	delay_init();
	while(1)
	{
//		GPIO_SetBits(GPIOB,GPIO_Pin_5);        //点亮LED0
//		GPIO_ResetBits(GPIOE,GPIO_Pin_5);      //关闭LED1 
//		delay_ms(500);                         //软件延时500ms
//		GPIO_SetBits(GPIOE,GPIO_Pin_5);        //点亮LED1
//		GPIO_ResetBits(GPIOB,GPIO_Pin_5);      //关闭LED0
//		delay_ms(500);                         //软件延时500ms
		LED0 = 1;        //关闭LED0
		LED1 = 0;        //点亮LED1
		delay_ms(500);   //软件延时500ms
		LED0 = 0;        //点亮LED0
		LED1 = 1;        //关闭LED1
		delay_ms(500);   //软件延时500ms
	}
}

led.c

#include "led.h"

void LED_Init(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOE, ENABLE);//设能PB和PE口
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;			//LED0->PB5	端口配置
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;	//速度50MHz
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;	//推挽输出
	GPIO_Init(GPIOB, &GPIO_InitStructure);			//根据参数设定参数配置GPIO
	
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;			//LED1->PE5	端口配置
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;	//速度50MHz
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;	//推挽输出
	GPIO_Init(GPIOE, &GPIO_InitStructure);			//根据参数设定参数配置GPIO
}

led.h

#ifndef __LED_H__
#define __LED_H__

#include "sys.h"

#define LED0 PBout(5)
#define LED1 PEout(5)

void LED_Init(void);


#endif

猜你喜欢

转载自blog.csdn.net/Xiaomo_haa/article/details/86666005