ARM32入门学习笔记之 GPIO库
固件库中的GPIO函数
函数名 | 描述 |
---|---|
GPIO_Init | 根据GPIO_InitStruct中指定的参数初始化外设GPIOx寄存器 |
GPIO_ReadInputDataBit | 读取指定端口管脚的输入 |
GPIO_ReadInputData | 读取指定的GPIO端口输入 |
GPIO_ReadInputDataBit | 读取指定端口管脚的输出 |
GPIO_ReadOutputData | 读取指定的GPIO端口输出 |
GPIO_SetBits | 设置指定的数据端口位 |
GPIO_ResetBits | 清除指定的数据端口位 |
GPIO_WriteBit | 设置或者清除指定的数据端口位 |
GPIO_Write | 向指定GPIO数据端口写入数据 |
1.GPIO_Init函数
void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct);
根据GPIO_InitStruct中指定的参数初始化外设GPIOx寄存器
参数1:GPIOx:x可以是A、B、C、D、E,来选择GPIO外设;
参数2:GPIO_InitStruct:指向结构GPIO_InitTypeDef的指针,包含了外设GPIO的配置信息参阅Section:GPIO_InitTypeDef查阅更多该参数允许取值范围;
例如:
//声明一个名为GPIO_InitStructure的GPIO_InitTypeDef型结构体
GPIO_InitTypeDef GPIO_InitStructure;
//选中端口1
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
//选择端口的速度为10M
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
//选择端口为输出模式
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
//选择端口为推挽输出
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
//选择端口为无上拉和下拉
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA,&GPIO_InitStructure);
2.GPIO_ReadInputDataBit函数
uint8_t GPIO_ReadInputDataBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);
读取指定端口管脚的输入
参数1:GPIOx:同上
参数2:GPIO_Pin:待读取的端口位
例如:
uint8_t ReadValue;
ReadValue = GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_7);
3.GPIO_ReadInputData函数
uint16_t GPIO_ReadInputData(GPIO_TypeDef* GPIOx);
读取指定的GPIO端口输入
例如:
uint16_t ReadValue;
ReadValue = GPIO_ReadInputData(GPIOA);
4.GPIO_SetBits函数
void GPIO_SetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);
设置指定的数据端口位
例如:
//设置PA1和PA0为高电平
GPIO_SetBits(GPIOA,GPIO_Pin_1 |GPIO_Pin_0);
5.GPIO_ReSetBits函数
void GPIO_ResetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);
清除指定的数据端口位
例如:
//设置PA1和PA0为低电平
GPIO_ReSetBits(GPIOA,GPIO_Pin1 | GPIO_Pin_0);
6.GPIO_WriteBit函数
void GPIO_WriteBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, BitAction BitVal);
设置或清除指定的数据端口位
例如:
//设置PA1和PA0为低电平
GPIO_WriteBit(GPIOA,GPIO_Pin_1 | GPIO_Pin_0, Bit_RESET);
7.GPIO_Write函数
void GPIO_Write(GPIO_TypeDef* GPIOx, uint16_t PortVal);
向指向GPIO数据端口写入数据
例如:
//向PA口写入数据
GPIO_Write(GPIOA,0x0101);
注意:在初始化某端口时需要打开该端口的时钟以及配置系统的时钟源,与之相关的两个函数
RCC_AHBPeriphClockCmd(RCC_AHBENR_GPIOAEN,ENABLE);
SystemInit();