正式开始学习32,需要具有一定的C语言编程能力,必需掌握结构体变量和结构体指针,枚举类型,宏定义。csdn上有大量的教程解析,可以借助学习。
结构体变量:http://t.csdn.cn/a3QUL
GPIO的八种工作模式和寄存器的相关内容看这篇文章:STM32基础—GPIO的八种工作模式
库函数位置:
头文件:stm32f10x_gpio.h
源文件:stm32f10x_gpio.c
1.初始化函数GPIO_Init:
实现函数:void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct);
* GPIOx,* GPIO_InitStruct是两个结构体指针
作用:初始化一个或者多个IO口(同一组)的工作方式和速度
函数通过对寄存器的操作初始化串口
GPIO_TypeDef结构体, GPIO_InitTypeDef结构体
typedef struct
{
__IO uint32_t CRL;
__IO uint32_t CRH;
__IO uint32_t IDR;
__IO uint32_t ODR;
__IO uint32_t BSRR;
__IO uint32_t BRR;
__IO uint32_t LCKR;
} GPIO_TypeDef;
typedef struct
{
uint16_t GPIO_Pin; /*!< Specifies the GPIO pins to be configured.
This parameter can be any value of @ref GPIO_pins_define */
GPIOSpeed_TypeDef GPIO_Speed; /*!< Specifies the speed for the selected pins.
This parameter can be a value of @ref GPIOSpeed_TypeDef */
GPIOMode_TypeDef GPIO_Mode; /*!< Specifies the operating mode for the selected pins.
This parameter can be a value of @ref GPIOMode_TypeDef */
}GPIO_InitTypeDef;
使用实例:
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; //LED0-->PB.5 端口配置
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO口速度为50MHz
GPIO_Init(GPIOB, &GPIO_InitStructure); //根据设定参数初始化GPIOB.5
// GPIOB是宏定义,查看定义是指向GPIO_TypeDef的指针
2.读取函数GPIO_ReadInputDataBit :
作用:在固件库中操作 IDR 寄存器读取 IO 端口数据,返回值0,1
实现函数:uint8_t GPIO_ReadInputDataBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
//uint8_t是无符号字符类型,定义typedef unsigned char uint8_t;
GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_5);//读取PA5引脚电平
3.输出函数GPIO_Write
作用:在固件库中设置 ODR 寄存器的值来控制 IO 口的输出状态,0,1
实现函数:void GPIO_Write(GPIO_TypeDef* GPIOx, uint16_t PortVal);
4.设置函数GPIO_SetBits:
作用:在 STM32 固件库中设置BSRR 和 BRR 寄存器的值控制 GPIO 端口输出
实现函数:void GPIO_SetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);
GPIO_SetBits(GPIOB, GPIO_Pin_5);//设置 GPIOB.5 输出 1
5.清除函数GPIO_ResetBits:
作用:在 STM32 固件库中设置BSRR 寄存器的值控制 GPIO 端口输出
实现函数:void GPIO_ResetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
GPIO_ResetBits (GPIOB, GPIO_Pin_5);//设置 GPIOB.5 输出位 0
使用GPIO函数的操作步骤为:
1) 使能 IO 口时钟。调用函数为 RCC_APB2PeriphClockCmd()。
2) 初始化 IO 参数。调用函数 GPIO_Init();
3) 操作 IO。操作 IO 的方法就是上面我们讲解的方法。