zoukankan      html  css  js  c++  java
  • 第一个实验-LED灯闪烁

    led.h:

    #ifndef __LED_H
    #define __LED_H
    
    //LED端口定义
    #define LED0 PFout(9)	// DS0
    void LED_Init(void);
    
    #endif
    

    led.c:

    #include "led.h" 
    #include "stm32f4xx.h"
    
    //初始化PF9为输出口.并使能这个口的时钟		    
    //LED IO初始化
    void LED_Init(void)
    {    	 
      GPIO_InitTypeDef  GPIO_InitStructure;
    
      RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE);//使能GPIOF时钟
    
      //GPIOF9初始化设置
      GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 ;//LED0对应IO口
    
      GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;//普通输出模式
      GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//推挽输出
      GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz
      GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//上拉
    
      GPIO_Init(GPIOF, &GPIO_InitStructure);//初始化GPIO
    	
      GPIO_SetBits(GPIOF,GPIO_Pin_9 );//GPIOF9设置高,灯灭
    
    }
    

    GPIOx:哪一组GPIO端口

    #define IS_GPIO_ALL_PERIPH(PERIPH) (((PERIPH) == GPIOA) || 
                                        ((PERIPH) == GPIOB) || 
                                        ((PERIPH) == GPIOC) || 
                                        ((PERIPH) == GPIOD) || 
                                        ((PERIPH) == GPIOE) || 
                                        ((PERIPH) == GPIOF) || 
                                        ((PERIPH) == GPIOG) || 
                                        ((PERIPH) == GPIOH) || 
                                        ((PERIPH) == GPIOI) || 
                                        ((PERIPH) == GPIOJ) || 
                                        ((PERIPH) == GPIOK))
    

    1、GPIO_Pin:端口

    #define GPIO_Pin_0                 ((uint16_t)0x0001)  /* Pin 0 selected */
    #define GPIO_Pin_1                 ((uint16_t)0x0002)  /* Pin 1 selected */
    #define GPIO_Pin_2                 ((uint16_t)0x0004)  /* Pin 2 selected */
    #define GPIO_Pin_3                 ((uint16_t)0x0008)  /* Pin 3 selected */
    #define GPIO_Pin_4                 ((uint16_t)0x0010)  /* Pin 4 selected */
    #define GPIO_Pin_5                 ((uint16_t)0x0020)  /* Pin 5 selected */
    #define GPIO_Pin_6                 ((uint16_t)0x0040)  /* Pin 6 selected */
    #define GPIO_Pin_7                 ((uint16_t)0x0080)  /* Pin 7 selected */
    #define GPIO_Pin_8                 ((uint16_t)0x0100)  /* Pin 8 selected */
    #define GPIO_Pin_9                 ((uint16_t)0x0200)  /* Pin 9 selected */
    #define GPIO_Pin_10                ((uint16_t)0x0400)  /* Pin 10 selected */
    #define GPIO_Pin_11                ((uint16_t)0x0800)  /* Pin 11 selected */
    #define GPIO_Pin_12                ((uint16_t)0x1000)  /* Pin 12 selected */
    #define GPIO_Pin_13                ((uint16_t)0x2000)  /* Pin 13 selected */
    #define GPIO_Pin_14                ((uint16_t)0x4000)  /* Pin 14 selected */
    #define GPIO_Pin_15                ((uint16_t)0x8000)  /* Pin 15 selected */
    #define GPIO_Pin_All               ((uint16_t)0xFFFF)  /* All pins selected */
    

    2、GPIO_Mode:端口模式寄存器GPIOx_MODER

    typedef enum
    { 
      GPIO_Mode_IN   = 0x00, /*!< GPIO Input Mode */    //输入模式
      GPIO_Mode_OUT  = 0x01, /*!< GPIO Output Mode */    //输出模式
      GPIO_Mode_AF   = 0x02, /*!< GPIO Alternate function Mode */    //复用模式
      GPIO_Mode_AN   = 0x03  /*!< GPIO Analog Mode */    //模拟模式
    }GPIOMode_TypeDef;
    

    3、GPIO_OType:端口输出类型寄存器GPIOx_OTYPE

    typedef enum
    { 
      GPIO_OType_PP = 0x00,    //推挽
      GPIO_OType_OD = 0x01     //开漏
    }GPIOOType_TypeDef;
    

    4、GPIO_PuPd:端口上拉下拉寄存器GPIOx_PUPDR

    typedef enum
    { 
      GPIO_PuPd_NOPULL = 0x00,    //浮空
      GPIO_PuPd_UP     = 0x01,    //上拉
      GPIO_PuPd_DOWN   = 0x02     //下拉
    }GPIOPuPd_TypeDef;
    

    5、GPIO_Speed:端口输出速度寄存器GPIOx_OSPEEDR

    #define  GPIO_Speed_2MHz    GPIO_Low_Speed    
    #define  GPIO_Speed_25MHz   GPIO_Medium_Speed 
    #define  GPIO_Speed_50MHz   GPIO_Fast_Speed 
    #define  GPIO_Speed_100MHz  GPIO_High_Speed  
    

    main.c:

    #include "sys.h"
    #include "delay.h"
    #include "led.h"
    
    int main(void)
    { 
    	delay_init(168);		  //初始化延时函数
    	LED_Init();		        //初始化LED端口
    	
      /**下面是通过直接操作库函数的方式实现IO控制**/	
    	
    	while(1)
    	{
    	GPIO_ResetBits(GPIOF,GPIO_Pin_9);  //LED0对应引脚GPIOF.9拉低,亮  等同LED0=0;
                    //或PFout(9)=0
                    //或LED0=0
    	delay_ms(500);                     //延时300ms
    	GPIO_SetBits(GPIOF,GPIO_Pin_9);	   //LED0对应引脚GPIOF.9拉高,灭  等同LED0=1;
    	delay_ms(500);                     //延时300ms
    	}
    }
    
  • 相关阅读:
    外观模式
    适配器模式
    桥接模式
    中文词频统计
    英文词频统计
    字符串练习
    Python基础
    熟悉常用的Linux操作
    作业
    递归下降分析法
  • 原文地址:https://www.cnblogs.com/holaworld/p/12409638.html
Copyright © 2011-2022 走看看