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;

相关推荐
项目題供诗12 小时前
51单片机入门(八)
单片机·嵌入式硬件·51单片机
羽获飞12 小时前
从零开始学嵌入式之STM32——9.STM32的时钟系统
stm32·单片机·嵌入式硬件
飞睿科技13 小时前
乐鑫智能开关方案解析:基于ESP32-C系列的低功耗、高集成设计
嵌入式硬件·物联网·esp32·智能家居·乐鑫科技
来自晴朗的明天14 小时前
13、NMOS 电源防反接电路
单片机·嵌入式硬件·硬件工程
17(无规则自律)15 小时前
深入浅出 Linux 内核模块,写一个内核版的 Hello World
linux·arm开发·嵌入式硬件
芯岭技术15 小时前
PY32MD310单片机:高性能、低功耗的32位电机控制微控制器
单片机·嵌入式硬件
wotaifuzao16 小时前
STM32 + FreeRTOS 的订阅通知组件架构
stm32·嵌入式硬件·架构·freertos·事件驱动·嵌入式架构
小龙报16 小时前
【51单片机】深度解析 51 串口 UART:原理、配置、收发实现与工程化应用全总结
c语言·开发语言·c++·stm32·单片机·嵌入式硬件·51单片机
Lester_11011 天前
STM32 高级定时器PWM互补输出模式--如果没有死区,突然关闭PWM有产生瞬间导通的可能吗
stm32·单片机·嵌入式硬件·嵌入式软件
小李独爱秋1 天前
“bootmgr is compressed”错误:根源、笔记本与台式机差异化解决方案深度指南
运维·stm32·单片机·嵌入式硬件·文件系统·电脑故障