STM32 GPIO 使用初始化

// At first, define a function named Init;

//结构体GPIO_INITSTRUCT

void LED_Init(void)

{

//step 1

/*********定义一个GPIO_InitTypeDef 类型的结构体**********/

GPIO_InitTypeDef GPIO_InitStruct;

//step 2 Assign values to the GPIO_InitStruct

//Set three Values to the InitStuct;

/*********选择要控制的GPIOx的引脚**********/

GPIO_InitStructure.GPIO_Pin =GPIO_Pin_4;

/*********设置引脚速率为50MHZ**********/

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;

/*********设置引脚模式为通用推完输出**********/

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;

//step 3 open RCC of GPIOx

/*打开LED使用的GPIO的时钟使能*/

RCC_APB2PeriphClockCmd(RCC__APB2Periph_GPIOA, ENABLE);

//STEP 4 Init GPIOx

//

/*初始化相应的GPIO*/

GPIO_Init(GPIOA, &GPIO_InitStruct); //初始化GPIO_LED

}

//Content of GPIO_InitTypeDef

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;

//Content of GPIO_Init(GPIOx, &InitStruct);

// Because GPIOx IS a Address InitStruct is a variable, Should Add & sign before InitStuct;

//

/*定义GPIOA-H 寄存器结构体指针*/
#define GPIOA ((GPIO_TypeDef *) GPIOA_BASE)
#define GPIOB ((GPIO_TypeDef *) GPIOB_BASE)
#define GPIOC ((GPIO_TypeDef *) GPIOC_BASE)
#define GPIOD ((GPIO_TypeDef *) GPIOD_BASE)
#define GPIOE ((GPIO_TypeDef *) GPIOE_BASE)
#define GPIOF ((GPIO_TypeDef *) GPIOF_BASE)
#define GPIOG ((GPIO_TypeDef *) GPIOG_BASE)
#define GPIOH ((GPIO_TypeDef *) GPIOH_BASE)

//void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct)

// Content of GPIO_Init( )

//Content of GPIO_TypeDef;

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;

相关推荐
信看2 小时前
SN650-STM32F103C8t6
单片机·嵌入式硬件
Funing72 小时前
无线充电原理——SS拓扑、LCC-S拓扑、LCC-LCC拓扑
嵌入式硬件·电路分析·无线传输·无线充电
chen_mangoo2 小时前
HDMI简介
android·linux·驱动开发·单片机·嵌入式硬件
菜鸟江多多3 小时前
【STM32 Rocket-Pi原理图分享】
c语言·stm32·嵌入式硬件·mcu·智能硬件·原理图
iYun在学C4 小时前
驱动程序(创建设备节点实验)
linux·c语言·嵌入式硬件
雾岛听风眠4 小时前
STM32HAL库学习笔记
笔记·stm32·学习
知南x4 小时前
【正点原子STM32MP157学习篇】A7和M4联合调试(通过STM32CubeIDE)
stm32·嵌入式硬件·学习
天骄t5 小时前
UART通信全解析:从原理到实战
linux·单片机
qq_411262425 小时前
Ml307Udp: Failed to send data chunk
单片机·嵌入式硬件