使用STM32 GPIO的三个步骤
1.使用RCC函数初始化时钟
2.使用GPIO_Init函数初始化GPIO
3.使用输出或者输入函数控制GPIO
初始化时钟:
void RCC_AHBPeriphClockCmd(uint32_t RCC_AHBPeriph, FunctionalState NewState);
void RCC_APB2PeriphClockCmd(uint32_t RCC_APB2Periph, FunctionalState NewState);
void RCC_APB1PeriphClockCmd(uint32_t RCC_APB1Periph, FunctionalState NewState);
变量1填具体外设,如GPIOA,GPIOB等。
变量2填ENABLE或DISABLE。
初始化GPIO
先定义结构体,赋值结构体,用结构体对对应GPIO进行初始化。
GPIO_InitTypeDef GPIO_InitStructure; //定义结构体
GPIO_InitStructure.GPIO_Mode = ; //赋值结构体//可以选择八种输入输出模式
GPIO_InitStructure.GPIO_Pin = ; //选中引脚,可以用|选中多个引脚
GPIO_InitStructure.GPIO_Speed =; //选中输出速度
GPIO_Init(GPIOX,&GPIO_InitStructure); //初始化GPIO
控制GPIO
使用一下八个GPIO输入输出函数进行控制,各自对应不同的用法,注意积累。
uint8_t GPIO_ReadInputDataBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);
uint16_t GPIO_ReadInputData(GPIO_TypeDef* GPIOx);
uint8_t GPIO_ReadOutputDataBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);
uint16_t GPIO_ReadOutputData(GPIO_TypeDef* GPIOx);
void GPIO_SetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);
void GPIO_ResetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);
void GPIO_WriteBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, BitAction BitVal);
void GPIO_Write(GPIO_TypeDef* GPIOx, uint16_t PortVal);