点亮第一盏LED

依然范特西╮ 提交于 2019-12-02 12:54:33

一、原理图

二、流程

    (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);
    }        
}
 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!