STM32F104ZET6 GPIO

1.GPIO初始化

  • 1).创建GPIO初始化结构体
GPIO_InitTypeDef GPIO_InitStructure;
  • 2).使能所需要使用的GPIO的端口时钟,这里使用的是PF端口
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE);
  • 3).设置GPIO端口的引脚,模式等
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  • 4).初始化
GPIO_Init(GPIOF, &GPIO_InitStructure);
  • 5).将端口引脚设置为高电平(根据需要设置,可以不设置)
GPIO_SetBits(GPIOF, GPIO_Pin_9);

2.使用

将端口PF的引脚9置为0(低电平)

GPIO_ResetBits(GPIOF, GPIO_Pin_9);

注意:如果用的是keil写程序,不能将局部变量的定义放在函数调用的后面。所以,需要我们将局部变量的定义放在代码块的最前面。
放在函数调用之后出现:

//在块({}一对大括号)中,定义不能出现在执行语句后面
Usermain.c(20): error:  #268: declaration may not appear after executable statement in block
原文地址:https://www.cnblogs.com/risesource/p/11837693.html