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;

相关推荐
Amrzs_hp1 小时前
stm32温度采集
stm32·单片机·嵌入式硬件
鄭郑5 小时前
STM32学习笔记--I2C封装与OLED(2026.2.1)
笔记·stm32·学习
想放学的刺客5 小时前
单片机嵌入式试题(第29期)嵌入式系统的电源完整性设计与去耦电容选型。抗干扰设计与EMC合规性
c语言·stm32·嵌入式硬件·物联网·51单片机
-Springer-6 小时前
STM32 学习 —— 个人学习笔记2-2(新建工程)
笔记·stm32·学习
代码游侠7 小时前
学习笔记——Linux字符设备驱动开发
linux·arm开发·驱动开发·单片机·嵌入式硬件·学习·算法
czhaii8 小时前
STC32G.H中文注释各寄存器特殊功能寄存器作用
单片机·嵌入式硬件
码农三叔8 小时前
(9-3)电源管理与能源系统:充电与扩展能源方案
人工智能·嵌入式硬件·机器人·能源·人形机器人
集芯微电科技有限公司8 小时前
15V/2A同步开关型降压单节/双节锂电池充电管理IC支持输入适配器 DPM 功能
c语言·开发语言·stm32·单片机·嵌入式硬件·电脑
零一iTEM8 小时前
TPS61088大功率升压模块
嵌入式硬件·硬件工程·学习方法
MAR-Sky9 小时前
ws2812幻彩灯珠使用stc/AI8G1K08控制
单片机·嵌入式硬件