zoukankan      html  css  js  c++  java
  • STM32F4xx_GPIO常用设置

    1、初始化时钟:

      RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOB | RCC_AHB1Periph_GPIOC | RCC_AHB1Periph_GPIOD | RCC_AHB1Periph_GPIOE, ENABLE);

    2、输出设置:

      

      GPIO_InitTypeDef  GPIO_InitStructure;
      GPIO_StructInit(&GPIO_InitStructure);
      GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_9;
      GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
      GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
      GPIO_InitStructure.GPIO_Speed = GPIO_Speed_25MHz;
      GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
      GPIO_Init(GPIOB, &GPIO_InitStructure);

    3、输入设置:

      

      GPIO_InitTypeDef   GPIO_InitStructure;
      GPIO_StructInit(&GPIO_InitStructure);
      GPIO_InitStructure.GPIO_Speed = GPIO_Speed_25MHz;
      GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
      GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
      GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
      GPIO_Init(GPIOC, &GPIO_InitStructure);

    4、模拟输入:

      

        GPIO_InitTypeDef GPIO_InitStructure;
    
        GPIO_StructInit(&GPIO_InitStructure);
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
        GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
        GPIO_Init(GPIOA, &GPIO_InitStructure);

    5、管脚复用:

      

        GPIO_InitTypeDef GPIO_InitStruct;
    
        RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
    
        GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_TIM3);
        GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_TIM3);
    
        GPIO_InitStruct.GPIO_Pin =  GPIO_Pin_6 | GPIO_Pin_7;
        GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
        GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
        GPIO_InitStruct.GPIO_Speed = GPIO_Speed_25MHz;
        GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
        GPIO_Init(GPIOA, &GPIO_InitStruct);

     实例:初始化LED

    void led_init(void)
    {
        //关于变量的定义要放在函数的开头,否则会出错
        GPIO_InitTypeDef GPIO_init;
        
        //使能相应时钟
        RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);
        RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB,ENABLE);
        RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC,ENABLE);
        
        //初始化GPIO参数
        GPIO_init.GPIO_Mode=GPIO_Mode_OUT;
        GPIO_init.GPIO_Pin=GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15;
        GPIO_init.GPIO_OType=GPIO_OType_PP;
        GPIO_init.GPIO_PuPd=GPIO_PuPd_UP;  //上拉
        GPIO_init.GPIO_Speed=GPIO_Speed_100MHz;
        GPIO_Init(GPIOB,&GPIO_init);
        
        GPIO_init.GPIO_Pin=GPIO_Pin_8|GPIO_Pin_9;
        GPIO_Init(GPIOC,&GPIO_init);
            
        GPIO_init.GPIO_Pin=GPIO_Pin_15;
        GPIO_init.GPIO_PuPd=GPIO_PuPd_DOWN;    //下拉
        GPIO_Init(GPIOA,&GPIO_init);
        
        GPIO_init.GPIO_Pin=GPIO_Pin_3;
        GPIO_Init(GPIOB,&GPIO_init);    
        
    }

      

  • 相关阅读:
    单例模式(上)单例常见的实现方式
    Netty(六)揭开 BootStrap 的神秘面纱
    Netty(五)Netty 高性能之道
    Netty(四)基于Netty 的简易版RPC
    Netty(三)基于Bio和Netty 的简易版Tomcat
    Netty(二)Netty 与 NIO 之前世今生
    java学习笔记
    Java使用笔记之对象比较
    React学习之受控和非受控组件
    SpringBoot学习之常用注解
  • 原文地址:https://www.cnblogs.com/longxi/p/10607547.html
Copyright © 2011-2022 走看看