×
嵌入式 > 嵌入式开发 > 详情

STM32 GPIO使用步骤 ST3.0.0库

发布时间:2020-08-24 发布时间:
|
初始化一个普通IO的步骤

1:GPIO_InitTypeDef GPIO_InitStructure定义一个结构体,用于初始化IO

 

2:使能IO时钟,STM32 的IO都是高速IO。

分为两种情况

第一种是IO为高速时钟:

void RCC_APB2PeriphClockCmd(uint32_t RCC_APB2Periph, FunctionalState NewState)

第二种是IO为低速时钟

void RCC_APB1PeriphClockCmd(uint32_t RCC_APB1Periph, FunctionalState NewState)

3:GPIO_InitStructure选定具体IO端口,用户要使用哪个IO,是PC3还是PC4

4:GPIO_InitStructure设置IO的工作模式,输出输入还是?,一共有8中IO的工作模式,一般IO设置为推挽输出

5:GPIO_InitStructure设置IO的工作速率,2M还是50MHZ,这个只对输出模式有作用,如果IO设置为输入就没有作用了。

6:GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct) 调用函数初始化上面设置好的信息写入到IO外设中。

7:上面6步初始化好IO了,接下来就要设置IO为高电平还是低电平了。

IO设置为高电平函数为:

void GPIO_SetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);

IO设置为低电平函数为:

void GPIO_ResetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);

=====================================================================================================

实例:

GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOC, ENABLE); //使能高速外设IO

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);

GPIO_SetBits(GPIOC, GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5);

PC3 PC4 PC5都设为输出高电平

以上来自野火M3



『本文转载自网络,版权归原作者所有,如有侵权请联系删除』

热门文章 更多
发明专利在疫情影响下的逆势增长