一、原理图
二、流程
(1)打开时钟
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOG , ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE , ENABLE);
(2)配置GPIO的输出模式
GPIO_InitTypeDef initValue;
initValue.GPIO_Mode = GPIO_Mode_OUT ; //输出模式
initValue.GPIO_OType = GPIO_OType_PP ; //推挽输出
initValue.GPIO_Pin = GPIO_Pin_9; //管脚9
initValue.GPIO_PuPd = GPIO_PuPd_NOPULL ; //无上拉下拉
initValue.GPIO_Speed = GPIO_Speed_50MHz; //输出速率
GPIO_Init(GPIOG , &initValue);
initValue.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_3 ; //管脚4和管脚3
GPIO_Init(GPIOE , &initValue);
(3)设置默认值(默认LED是关闭的)
GPIO_SetBits(GPIOG,GPIO_Pin_9); //将PG9设置为高电平,关闭LED1
GPIO_SetBits(GPIOE , GPIO_Pin_4 | GPIO_Pin_3); //将PE4和PE3设置为高电平,关闭LED2和LED3
(4)打开LED
GPIO_ResetBits(GPIOG,GPIO_Pin_9); //将PG9设置为低电平,点亮LED1
GPIO_ResetBits(GPIOE,GPIO_Pin_4 | GPIO_Pin_3); //将PE4和PE3设置为低电平,点亮LED2和LED3
(5)关闭LED
GPIO_SetBits(GPIOG,GPIO_Pin_9); //将PG9设置为高电平,关闭LED1
GPIO_SetBits(GPIOE , GPIO_Pin_4 | GPIO_Pin_3); //将PE4和PE3设置为高电平,关闭LED2和LED3
三、程序
/*led.h文件*/
#ifndef __LED_H__
#define __LED_H__
#include "stm32f4xx_conf.h"
extern void led_init(void);
extern void led_on(int num);
extern void led_off(int num);
#endif
/*led.c文件*/
#include"led.h"
#define LED_CON (*(volatile unsigned int *)0x)
//LED初始化。led1-PG9;led2-PE4;led3-PE3
void led_init(void)
{
GPIO_InitTypeDef initValue;
/*1、打开时钟*/
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE,ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOG,ENABLE);
/*2、配置GPIO的输出功能*/
initValue.GPIO_Mode = GPIO_Mode_OUT;
initValue.GPIO_OType = GPIO_OType_PP;
initValue.GPIO_Pin = GPIO_Pin_9;
initValue.GPIO_PuPd = GPIO_PuPd_NOPULL;
initValue.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOG,&initValue);
initValue.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_3;
GPIO_Init(GPIOE,&initValue);
/*3、设置默认值,默认LED是关闭的*/
GPIO_SetBits(GPIOG,GPIO_Pin_9);
GPIO_SetBits(GPIOE,GPIO_Pin_4 | GPIO_Pin_3);
}
//打开LED
void led_on(int num)
{
switch(num)
{
case 1: //led1
GPIO_ResetBits(GPIOG,GPIO_Pin_9);
break;
case 2: //led2
GPIO_ResetBits(GPIOE,GPIO_Pin_4);
break;
case 3: //led3
GPIO_ResetBits(GPIOE,GPIO_Pin_3);
break;
default:
break;
}
}
//关闭LED
void led_off(int num)
{
switch(num)
{
case 1: //led1
GPIO_SetBits(GPIOG,GPIO_Pin_9);
break;
case 2: //led2
GPIO_SetBits(GPIOE,GPIO_Pin_4);
break;
case 3: //led3
GPIO_SetBits(GPIOE,GPIO_Pin_3);
break;
default:
break;
}
}
/*main.c文件*/
#include"led.h"
int main(void)
{
led_init();
while(1)
{
led_on(1);
}
}
来源:https://blog.csdn.net/m0_37671794/article/details/102761059