2. 使用库函数,让STM32点亮一盏灯

在嵌入式中,点亮一盏灯,犹如“hello word”般重要

点亮一盏灯,是小事,成功点亮是大事

而我本人习惯一类文件放在一个  .c  文件里

那么,我们先在USER 目录中,添加  gpio.c   和 gpio.h  以及  led.c  led.h文件

然后点击这个位置,将.c文件加入到项目中

2. 使用库函数,让STM32点亮一盏灯_第1张图片

添加成功之后

在gpio.c文件中写入

#include "stm32f10x.h"
#include "gpio.h"

/*电路设计是 GPIOB01 引脚控制LED */
void GPIO_Config_Init(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;	//GPIO 结构体
	
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); //开启GPIOB的时钟
	

	GPIO_InitStructure.GPIO_Pin = LED_GPIO;             //定时GPIO口
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//速度
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;//该GPIO的输出模式
	GPIO_Init(LED_GPIO_Port,&GPIO_InitStructure);
	

}

在gpio.h文件中写入

#ifndef __GPIO_H__
#define  __GPIO_H__


#define LED_GPIO		GPIO_Pin_1   //定义引脚宏
#define LED_GPIO_Port 		GPIOB

void GPIO_Config_Init(void); //函数声明

	
#endif

 

在led.c中输入

#include "led.h"

void LED_ON()
{
	GPIO_SetBits(GPIOB,GPIO_Pin_1);  //输出高电平
}

void LED_OFF()
{
	GPIO_ResetBits(GPIOB,GPIO_Pin_1);//输出低电平
}

在led.h中输入

#ifndef __LED_H__
#define __LED_H__
#include "stm32f10x.h"
#include "gpio.h"

void LED_ON();
void LED_OFF();

#endif

 

 

然后在main函数中,调用即可

在main.c文件中加入以下代码

#include "stm32f10x.h"
#include 

#include "led.h"
#include "gpio.h"

int main(void)
{
    GPIO_Config_Init();//GPIO 初始化

    while(1)
    {
        LED_ON();
        // LED_OFF();
    }
}

编译,烧录,看看效果

 

如果没有点亮,看看是不是时钟没有配置正确,是否enable了?

 

有啥问题,欢迎添加微信:LinLinux6_13 ,互相学习

你可能感兴趣的:(STM32)