在STM32微控制器上初始化GPIO(通用输入输出)是一个常见的任务,通常涉及到配置GPIO的模式、输出类型、速度和上下拉电阻等参数。下面是一个基于STM32 HAL库的GPIO初始化程序的示例,该程序将帮助你完成这些配置。 1. 引入必要的头文件和库 首先,需要引入STM32 HAL库的头文件以及特定GPIO端口的头文件。 c #include ...
/*Configure GPIO pin : PtPin */GPIO_InitStruct.Pin = LED4_Pin;GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;GPIO_InitStruct.Pull = GPIO_NOPULL;GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;HAL_GPIO_Init(LED4_GPIO_Port, &GPIO_InitStru...
讲解stm32接口源代码GPIO_InitTypeDefGPIO_InitStructure;// 使能时钟,这个是必要的,后续补上时钟树的概念RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);// 这个就是GPIO的电气属性配置,因为目前没有使用复用功能GPIO_InitStructure.GPIO_Pin=GPIO_Pin_13;// 输出模式GPIO_InitStructure.GPIO_Mode=GPIO_Mode_...
// 初始化复用开漏输出模式的GPIOGPIO_InitStruct.Pin = GPIO_PIN_0; GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;// 复用开漏输出模式GPIO_InitStruct.Pull = GPIO_NOPULL;// 不设置上拉或下拉GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;// GPIO速度设置为高速HAL_GPIO_Init(GPIOA, &GPIO_InitStruct)...
一 初始化GPIO 使用HAL库的优点在于不用手动添加初始化的代码了,CubeMX会根据软件设置自动生成。 自动生成的HAL库GPIO初始化代码: staticvoid MX_GPIO_Init(void) { GPIO_InitTypeDef GPIO_InitStruct;/*GPIO Ports Clock Enable*/__HAL_RCC_GPIOH_CLK_ENABLE(); ...
完整代码 #include "stm32f10x.h" int main() { //创建结构体 GPIO_InitTypeDef GPIO_InitStruct; //GPIOC位于APB2总线上,所以使用APB2外设时钟命令 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE); //初始化引脚为PIN_13 GPIO_InitStruct.GPIO_Pin=GPIO_Pin_13; ...
1.GPIO初始化配置 GPIO_InitTypeDefGPIO_InitStructure; 定义结构体变量 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);//开启对应时钟使能(注时钟可以用‘或’形式选中多个( RCC_APB2Periph_GPIOC|RCC_APB3Periph_GPIOC... ) GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;配置模式 GPIO...
GPIO_InitStruct.GPIO_Speed=GPIO_Speed_10MHz;//速度越快,功耗越高 设置输出速度,如果是输入的话可以直接忽略。速度越快,功耗越高 最后,再调用外设初始化函数,把配置好的结构体写到相应的寄存器中 GPIO_Init(GPIOA,&GPIO_InitStruct);//void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStru...