stm32之IO操作(基于库函数)

// stm32学习笔记

对于stm32f10x系列芯片的库函数,GPIO的操作函数在stm32f10x_gpio.h中声明,在stm32f10x_gpio.c 中完成定义。

基于库函数的stm32的IO口操作步骤:

1.使能IO口时钟,

函数接口:

void RCC_APB2PeriphClockCmd(uint32_t RCC_APB2Periph, FunctionalState NewState);

参数列表:

RCC_APB2Periph:连接在APB2总线上的外设名称,如下图所示,GPIO位于APB2总线上,所以调用RCC_APB2PeriphClockCmd进行时钟使能;

NewState:ENANLE或者DISABLE

2、初始化IO口

函数接口:

void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct);

参数列表:

GPIOx:IO口的组别,如GPIOA、GPIOB;

GPIO_InitStruct :GPIO初始化结构体

需要完成该结构体的初始化(包括,输入输出模式、引脚号、读写速度’),并将该结构体地址传入GPIO_Init函数;

注意,该结构体要定义在时钟初始化之前的位置,不然会出现如下报错

error:  #268: declaration may not appear after executable statement in block


3、调用IO口控制函数对IO口电平进行控制:

函数接口

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);


猜你喜欢

转载自blog.csdn.net/haihui1996/article/details/78407578