Stm32 库函数 GPIO

您所在的位置:网站首页 gpio_init Stm32 库函数 GPIO

Stm32 库函数 GPIO

2023-04-08 04:55| 来源: 网络整理| 查看: 265

以STM32F103为例,记录一下自己对STM32中GPIO初始化的理解:

 

1 void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct); 

功能描述: 根据GPIO_InitStruct中指定的参数初始化外设GPIOx寄存器

输入参数1:GPIOx          //GPIOx:x 可以是 A,B,C,D 或者 E,来选择 GPIO 外设

输入参数2:GPIO_InitStruct      //GPIO_InitStruct:指向结构 GPIO_InitTypeDef 的指针,包含了外设 GPIO 的配置信息

                   如:管脚号,速度,模式等(GPIO_Pin,GPIO_Speed,GPIO_Mode)

                                  参阅 Section:GPIO_InitTypeDef 查阅更多该参数允许取值范围

GPIO_Init:有二个参数,均为结构体指针,右键Go To definition :可以查看函数的定义

 

/** * @brief General Purpose I/O */ typedef struct { __IO uint32_t CRL;   //端口配置低寄存器 __IO uint32_t CRH;   //端口配置高寄存器 __IO uint32_t IDR;   //端口输入数据寄存器 __IO uint32_t ODR;   //端口输出数据寄存器 __IO uint32_t BSRR;  //端口位设置/复位寄存器 __IO uint32_t BRR;   //端口位复位寄存器 __IO uint32_t LCKR;  //端口配置锁定寄存器 } GPIO_TypeDef; /** * @brief GPIO Init structure definition */ typedef struct { uint16_t GPIO_Pin; /*!< Specifies the GPIO pins to be configured. This parameter can be any value of @ref GPIO_pins_define */ GPIOSpeed_TypeDef GPIO_Speed; /*!< Specifies the speed for the selected pins. This parameter can be a value of @ref GPIOSpeed_TypeDef */ GPIOMode_TypeDef GPIO_Mode; /*!< Specifies the operating mode for the selected pins. This parameter can be a value of @ref GPIOMode_TypeDef */ }GPIO_InitTypeDef;

 

 

 

函数原型如下:

1 /** 2 * @brief Initializes the GPIOx peripheral according to the specified 3 * parameters in the GPIO_InitStruct. 4 * @param GPIOx: where x can be (A..G) to select the GPIO peripheral. 5 * @param GPIO_InitStruct: pointer to a GPIO_InitTypeDef structure that 6 * contains the configuration information for the specified GPIO peripheral. 7 * @retval None 8 */ 9 void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct) 10 { 11 uint32_t currentmode = 0x00, currentpin = 0x00, pinpos = 0x00, pos = 0x00; 12 uint32_t tmpreg = 0x00, pinmask = 0x00; 13 /* Check the parameters */ 14 assert_param(IS_GPIO_ALL_PERIPH(GPIOx)); 15 assert_param(IS_GPIO_MODE(GPIO_InitStruct->GPIO_Mode)); 16 assert_param(IS_GPIO_PIN(GPIO_InitStruct->GPIO_Pin)); 17 18 /*---------------------------- GPIO Mode Configuration -----------------------*/ 19 currentmode = ((uint32_t)GPIO_InitStruct->GPIO_Mode) & ((uint32_t)0x0F); 20 if ((((uint32_t)GPIO_InitStruct->GPIO_Mode) & ((uint32_t)0x10)) != 0x00) 21 { 22 /* Check the parameters */ 23 assert_param(IS_GPIO_SPEED(GPIO_InitStruct->GPIO_Speed)); 24 /* Output mode */ 25 currentmode |= (uint32_t)GPIO_InitStruct->GPIO_Speed; 26 } 27 /*---------------------------- GPIO CRL Configuration ------------------------*/ 28 /* Configure the eight low port pins */ 29 if (((uint32_t)GPIO_InitStruct->GPIO_Pin & ((uint32_t)0x00FF)) != 0x00) 30 { 31 tmpreg = GPIOx->CRL; 32 for (pinpos = 0x00; pinpos < 0x08; pinpos++) 33 { 34 pos = ((uint32_t)0x01) 引用结构体变量中的成员

1) 结构体变量名. 成员名: stu1.name 2) 结构体指针变量->成员名: ps->name 3) (*结构体指针变量). 成员名: (*ps).name 4) 结构体变量数组名. 成员名: stu[0].name

 

先写到这里 下次再补充

 



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3