stm32笔记 gpio

使能时钟

RCC_APB2PeriphClockCmd
使能或者失能 APB2 外设时钟

具体参考芯片手册
RCC_APB2Periph_AFIO 功能复用 IO 时钟
RCC_APB2Periph_GPIOA GPIOA 时钟
RCC_APB2Periph_GPIOB GPIOB 时钟
RCC_APB2Periph_GPIOC GPIOC 时钟
RCC_APB2Periph_GPIOD GPIOD 时钟
RCC_APB2Periph_GPIOE GPIOE 时钟
RCC_APB2Periph_ADC1 ADC1 时钟
RCC_APB2Periph_ADC2 ADC2 时钟
RCC_APB2Periph_TIM1 TIM1 时钟
RCC_APB2Periph_SPI1 SPI1 时钟
RCC_APB2Periph_USART1 USART1 时钟
RCC_APB2Periph_ALL 全部 APB2 外设时钟

例:
/* Enable GPIOA, GPIOB and SPI1 clocks */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB |
RCC_APB2Periph_SPI1, ENABLE);

初始化gpio

GPIO_Init
根据 GPIO_InitStruct 中指定的参数初始化外设 GPIOx 寄存器
void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct)

例:
/* Configure all the GPIOA in Input Floating mode */
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);

Table 184. GPIO_Speed 值
GPIO_Speed 描述
GPIO_Speed_10MHz 最高输出速率 10MHz
GPIO_Speed_2MHz 最高输出速率 2MHz
GPIO_Speed_50MHz 最高输出速率 50MHz

Table 185. GPIO_Mode 值
GPIO_Mode_AIN 模拟输入
GPIO_Mode_IN_FLOATING 浮空输入
GPIO_Mode_IPD 下拉输入
GPIO_Mode_IPU 上拉输入
GPIO_Mode_Out_OD 开漏输出
GPIO_Mode_Out_PP 推挽输出
GPIO_Mode_AF_OD 复用开漏输出
GPIO_Mode_AF_PP 复用推挽输出

例子:
1.

void Led_ConfigurAction(void) {
	GPIO_InitTypeDef* GPIO_InitStruct;   //设置结构体变量
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF,ENABLE);   
	GPIO_InitStruct->GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10;
	GPIO_InitStruct->GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStruct->GPIO_Mode = GPIO_Mode_OUT;
	GPIO_Init(GPIOF,GPIO_InitStruct);
}

2.
void Key_ConfigurAction(void) {
	 RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);
	 GPIO_InitTypeDef* GPIO_InitStruct;
	 GPIO_InitStruct->GPIO_Pin = GPIO_Pin_0;
	 GPIO_InitStruct->GPIO_Speed = GPIO_Speed_50MHz;
	 GPIO_InitStruct->GPIO_Mode = GPIO_Mode_IN;
	 GPIO_Init(GPIOA,GPIO_InitStruct);
}

设置或者清除指定的数据端口位

GPIO_WriteBit
void GPIO_WriteBit(GPIO_TypeDef* GPIOx, u16 GPIO_Pin, BitAction BitVal)

BitVal: 该参数指定了待写入的值
该参数必须取枚举 BitAction 的其中一个值
Bit_RESET: 清除数据端口位 //低电平
Bit_SET: 设置数据端口位 //高电平

GPIO_WriteBit(GPIOF,GPIO_Pin_9 | GPIO_Pin_10, Bit_SET);

读取指定的 GPIO 端口输入 | 输出

GPIO_ReadOutputDataBit //读取输入
u8 GPIO_ReadInputDataBit(GPIO_TypeDef* GPIOx, u16 GPIO_Pin)

例:
/* Reads the seventh pin of the GPIOB and store it in ReadValue
variable */
u8 ReadValue;
ReadValue = GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_7);

GPIO_ReadOutputDataBit //读取输出
u8 GPIO_ReadOutputDataBit(GPIO_TypeDef* GPIOx, u16 GPIO_Pin)

例:
/* Reads the seventh pin of the GPIOB and store it in ReadValue
variable */
u8 ReadValue;
ReadValue = GPIO_ReadOutputDataBit(GPIOB, GPIO_Pin_7);

例子:

int Key_Scan(GPIO_TypeDef* GPIOx,unsigned int Pin) {
		 if(GPIO_ReadInputDataBit(GPIOx,Pin) == 0) {
			  delay_ms(400); 
				if(GPIO_ReadInputDataBit(GPIOx,Pin) == 0) {
					while(GPIO_ReadInputDataBit(GPIOx,Pin) == 0);
					return 1;  //按键被按下
				}
				return 0;
		 }
		 return 0;
}

复用

stm32的gpio是可以复用的
例:
复用串口1

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);  //使能
RCC_APB2PeriphClockCmd(RCC_AHB1Periph_USART1,ENABLE);
GPIO InitStructure.GPIO_Mode = GPIO_Mode_AF;//复用功能  写在GPIO_Init里
GPIO_PinAFConfig(GPlOA,GPIO_PinSource9,GPIO_AF_USART1);   //PA9连接AF7,复用为USART1 TX
GPIO_PinAFConfig(GPlOA,GPIO_PinSource10,GPIO_AF_USART1);   //PA10连接AF7,复用为USART1 RX
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值