赞
踩
1、使用RCC开启GPIO的时钟
2、使用GPIO_Init函数初始化GPIO
3、使用输出或输入的函数控制GPIO口
void RCC_AHBPeriphClockCmd(uint32_t RCC_AHBPeriph, FunctionalState NewState);
//RCC_AHB外设时钟控制
void RCC_APB2PeriphClockCmd(uint32_t RCC_APB2Periph, FunctionalState NewState);、
//RCC_APB2外设时钟控制
void RCC_APB1PeriphClockCmd(uint32_t RCC_APB1Periph, FunctionalState NewState);
//RCC_APB1外设时钟控制
AHB(Advanced High-performance Bus), 高速总线,用来接高速外设的。APB (Advanced Peripheral Bus) 低速总线,用来接低速外设的。
APB2一般是和AHB同频率,都是72MHz,APB1一般是36MHz。APB2连接的一般是外设中稍微重要的部分,比如GPIO端口,还有一些外设的一号,比如USART1、SPI1、TIM1、TIM8,还接ADC、EXTI、AFIO。其他的2、3、4…外设和DAC、PWR、BKP等次要点的外设都放到APB1。
void GPIO_DeInit(GPIO_TypeDef* GPIOx); //指定的GPIO外设会被复位 void GPIO_AFIODeInit(void); //指定的AFIO外设会被复位 void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct); //用结构体的参数来初始化GPIO口 void GPIO_StructInit(GPIO_InitTypeDef* GPIO_InitStruct); //把结构体变量赋一个默认值 //四个读取函数 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); //根据第三个参数的值设置指定端口,参数是16进制,转换为2进制看设置的高低电平 void GPIO_Write(GPIO_TypeDef* GPIOx, uint16_t PortVal); //可以同时对16个端口进行写入
GPIO_InitTypeDef GPIO_InitStructure;
//声明结构体,下面引出结构体的所有成员
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
//选择指定引脚的工作方式为推挽输出
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0;
//选要配置的引脚
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
//选输出速度
GPIO_Init(GPIOA,&GPIO_InitStructure);
//执行这一条才算真正写入到GPIO配置寄存器
在初始化GPIO口时,我们声明了一个结构体GPIO_InitStructure
,名字与void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct);
中第二个参数很像,但细看多了“ure”,是咱们自己起的一个名字。
GPIO_Mode_AIN 模拟输入
GPIO_Mode_IN_FLOATING 浮空输入
GPIO_Mode_IPD 下拉输入
GPIO_Mode_IPU 上拉输入
GPIO_Mode_Out_PP 推挽输出
GPIO_Mode_Out_OD 开漏输出
GPIO_Mode_AF_PP 复用推挽输出
GPIO_Mode_AF_OD 复用开漏输出
//方法1: GPIO_WriteBit(LEDPORT,LED1,(BitAction)(1)); //LED1接口输出高电平1 delay_us(50000); //延时1秒 GPIO_WriteBit(LEDPORT,LED1,(BitAction)(0)); //LED1接口输出低电平0 delay_us(50000); //延时1秒 //方法2: GPIO_WriteBit(LEDPORT,LED1,(BitAction)(1-GPIO_ReadOutputDataBit(LEDPORT,LED1))); //取反LED1 delay_ms(500); //延时1秒 //方法3: GPIO_SetBits(LEDPORT,LED1); //LED灯都为高电平(1) delay_s(1); //延时1秒 GPIO_ResetBits(LEDPORT,LED1); //LED灯都为低电平(0) delay_s(1); //延时1秒 //方法4 GPIO_Write(LEDPORT,0x0001); //直接数值操作将变量值写入LED delay_s(2); //延时1秒 GPIO_Write(LEDPORT,0x0000); //直接数值操作将变量值写入LED delay_s(2); //延时1秒
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。