stm32_配置GPIO点亮led灯

#ifndef _LED_H_   //判断有没有存在一个LED.h文件
#define _LED_H_    //如果没有,声明一个LED.h文件
#define RED_ON red_on();  //宏定义RED_ON 与red_on();一致,后面直接就可以用RED_ON代替red_on();,主要是为了调用函数是方便
#define RED_OFF red_off();
#define GREEN_ON green_on();
#define GREEN_OFF green_off();
#define BLUE_ON blue_on();
#define BLUE_OFF blue_off();
#define YELLOW_ON yellow_on();
#define YELLOW_OFF yellow_off();
#define PURPLE_ON purple_on();
#define PURPLE_OFF purple_off();
void red_on();//红色亮
void red_off();//红色亮
void green_on();//绿色亮
void green_off();//绿色灭
void blue_on();//蓝色亮
void blue_off();//蓝色灭
void yellow_on();//黄色亮
void yellow_off();//黄色灭 void purple_on();//紫色亮 void purple_off();//紫色灭 #endif                                                 
#include "stm32f10x.h"
#include "stm32f10x_gpio.h"
#include "led.h"
/*点亮红灯函数*/
void red_on(){
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);//APB2外设时钟使能
        GPIO_InitTypeDef gpio;      //定义初始化结构体变量
gpio.GPIO_Mode = GPIO_Mode_Out_PP;    //配置GPIO选中管脚的工作状态 这里配置为推挽输出,详情参考下面
gpio.GPIO_Pin
= GPIO_Pin_5;        //选择GPIO的管脚,这里选择第五个引脚,有16个引脚
gpio.GPIO_Speed
= GPIO_Speed_50MHz;    //用以设置选中管脚的速率,这里选择50MHz
GPIO_Init(GPIOB,
&gpio);           //GPIO初始化
GPIO_ResetBits(GPIOB,GPIO_Pin_5);     //给GPIOB_PIN_5管脚低电平,点亮外设红色LED
}

void red_off(){ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);//配置时钟,使能
GPIO_InitTypeDef gpio;//定义初始化结构体变量
gpio.GPIO_Mode
= GPIO_Mode_Out_PP;//选择工作方式
gpio.GPIO_Pin
= GPIO_Pin_5;//选择引脚
gpio.GPIO_Speed
= GPIO_Speed_50MHz;//选择晶振速率
GPIO_Init(GPIOB,
&gpio);//初始化结构体
GPIO_SetBits(GPIOB,GPIO_Pin_5);//灭掉LED
}
void yellow_on()
{ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
GPIO_InitTypeDef gpio;
gpio.GPIO_Mode
= GPIO_Mode_Out_PP;
gpio.GPIO_Pin
= GPIO_Pin_5|GPIO_Pin_0;
gpio.GPIO_Speed
= GPIO_Speed_50MHz;
GPIO_Init(GPIOB,
&gpio);
GPIO_ResetBits(GPIOB,GPIO_Pin_5);
GPIO_ResetBits(GPIOB,GPIO_Pin_0);
}

void yellow_off()
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
GPIO_InitTypeDef gpio;
gpio.GPIO_Mode
= GPIO_Mode_Out_PP;
gpio.GPIO_Pin
= GPIO_Pin_5|GPIO_Pin_0;
gpio.GPIO_Speed
= GPIO_Speed_50MHz;
GPIO_Init(GPIOB,
&gpio);
GPIO_SetBits(GPIOB,GPIO_Pin_5);
GPIO_SetBits(GPIOB,GPIO_Pin_0);
}
void purple_on()
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
GPIO_InitTypeDef gpio;
gpio.GPIO_Mode
= GPIO_Mode_Out_PP;
gpio.GPIO_Pin
= GPIO_Pin_5|GPIO_Pin_1;
gpio.GPIO_Speed
= GPIO_Speed_50MHz;
GPIO_Init(GPIOB,
&gpio);
GPIO_ResetBits(GPIOB,GPIO_Pin_5);
GPIO_ResetBits(GPIOB,GPIO_Pin_1);
}

void purple_off()
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
GPIO_InitTypeDef gpio;
gpio.GPIO_Mode
= GPIO_Mode_Out_PP;
gpio.GPIO_Pin
= GPIO_Pin_5|GPIO_Pin_1;
gpio.GPIO_Speed
= GPIO_Speed_50MHz;
GPIO_Init(GPIOB,
&gpio);
GPIO_SetBits(GPIOB,GPIO_Pin_5);
GPIO_SetBits(GPIOB,GPIO_Pin_1);
}
#include "stm32f10x.h"   
#include "stm32f10x_gpio.h"

#include "led.h"



int main(void)
{
     RED_ON
    
}

 函数RCC_APB2PeriphClockCmd

RCC_APB2Periph
该参数被门控的APB2外设时钟,可以取下表的一个或者多个取值的组合作为该参数的值。

 10.2.3 函数GPIO_Init

参数一:GPIO_Pin

参数二:GPIO_Speed

GPIO_Speed用以设置选中管脚的速率。Table 184. 给出了该参数可取的值

参数三:GPIO_Mode

GPIO_Mode用以设置选中管脚的工作状态。Table 185. 给出了该参数可取的值

 

 

参考文献:STM32F10固件库使用手册中文版,有问题的同学可以加QQ1714066988,验证名字是我的博客名,一起交流学习

原文地址:https://www.cnblogs.com/liangjinjie/p/7691007.html