当前位置:网站首页>第三讲--GPIO输入输出库函数使用以及相关例程
第三讲--GPIO输入输出库函数使用以及相关例程
2022-07-26 22:48:00 【若木·】
目录
1.GPIO_SetBits()函数与GPIO_ResetBits函数:
2.GPIO_WriteBit函数与GPIO_Write函数:
前言
上一讲介绍了GPIO是什么,GPIO的八种工作模式,详细介绍了GPIO初始化配置的方法,下面详细介绍GPIO输入相关库函数的用法以及用相关例程供大家参考
一、补充第一讲遗漏的内容
当建立好工程文件夹以及导入了相关库后,经过小伙伴的提问后才发现自己遗漏了一个比较重要的内容,那就是如何根据开发板型号选择user中的启动文件,下面给大家详细介绍:
1.stm32开发板的命名规则:
想知道如何选择正确的启动文件,首先要清楚stm32系列开发板的命名规则,以我使用的stm32f103c8t6为例,
将其分为 STM32 F 103C8 T6
Stm32即为32单片机,
F是产品类型,103代表产品子系列,101为基本型,102为USB基本型,103为增强型,105或107为互联型。
C代表引脚数,T=36脚,C=48脚,R=64脚,V=100脚,Z=144脚。
8代表FLASH大小(可以理解为闪存)6=32K,8=64K,B=128K,C=256K,D=384K,E=512K
T代表封装 H=BGA T=LQFP,U=VFQFPN
6代表工作温度范围 6=工业级,-40~+85℃ 7=工业级,-40~+105℃
2.型号分类
根据他们的型号进行分类
| 型号 | Flash容量 | 缩写 |
| STM32F100 | 16-32K | LD_VL |
| STM32F100 | 64-128K | MD_VL |
| STM32F100 | 256-512K | HD_VL |
| STM32F101/102/103 | 16-32K | LD |
| STM32F101/102/103 | 64-128K | MD |
| STM32F101/102/103 | 256-512K | HD |
| STM32F101/102/103 | >512K | XL |
| STM32F105/107 | -- | CL |
所以,咱们使用的F103C8T6是不是就是选择MD,s的启动文件呢:?
显然是的!
二.GPIO输入输出的相关库函数
首先跟大家说一下GPIO输入输出的相关库函数都在库的GPIO.h文件中,下拉到最后可以找到。
1.需要首先掌握的GPIO函数
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);
这些是初学者最常用的GPIO函数,下面我们逐个介绍:
1.GPIO_SetBits()函数与GPIO_ResetBits函数:
用来配置GPIO口为高电平,LED中的灭灯就可以使用该函数,其中函数的参数与GPIO初始化函数的参数相同,再次举例说明一下参数怎么选,首先看函数定义中关于参数的注释。
* @param GPIOx: where x can be (A..G) to select the GPIO peripheral.
* @param GPIO_Pin: specifies the port bits to be written.
举例说明,假设我们把外设接在了PA0口,那么GPIOx这个参数就使用GPIOA,而第二个参数选择GPIO_Pin_0,所以该函数的参数就为(GPIOA,GPIO_Pin_0)
下边的GPIO_ResetBits函数也是这种选参方法,LED中的灭灯就可以使用该函数,而该函数用来配置GPIO口为低电平。
2.GPIO_WriteBit函数与GPIO_Write函数:
GPIO_WriteBit该函数有三个参数,与上边函数不同的是,该函数需要三个参数,该参数也是实现GPIO口的高低电平设置,下面举例说明:
先看函数参数注释:
* @param GPIOx: where x can be (A..G) to select the GPIO peripheral.
* @param GPIO_Pin: specifies the port bit to be written.
* This parameter can be one of GPIO_Pin_x where x can be (0..15).
* @param BitVal: specifies the value to be written to the selected bit.
* This parameter can be one of the BitAction enum values:
* @arg Bit_RESET: to clear the port pin
* @arg Bit_SET: to set the port pin
我们还是以A0口为例:那么第一和第二个参数与上边介绍过的函数相同,分别为GPIOA和GPIO_PIN_0,重点看第三个参数,如果要将A0口置为高电平,那么也就是注释中所说的设置端口信号(to set the port pin),参数选择Bit_SET,反之,即为清除端口信号(to set the port pin),参数就是Bit_RESET。
当然了,如果你就是特别想要在第三个参数用1和0表示高低电平,也可以,但是需要做一个强制转换,转换为BitAction,第三个参数就为((BitAction)1)或((BitAction)0)。但是不建议使用。
而另一个函数GPIO_Write,可以对所以端口进行操作,这个我们下一篇单独详细介绍。
2.点亮LED和LED闪烁
学完了上边的几个函数,就可以进行点亮LED操作了,我们直接说LED闪烁,里边包含了点LED的操作。
首先把GPIO的初始化完成,上节课已经讲过了,这里我们直接用,不会的同学大家可以去我的上一篇博客看看。然后用GPIO_SetBits()灭灯,GPIO_ResetBits()点灯,加入延时就是闪烁了。
附代码:
#include "stm32f10x.h" // Device header
#include "Delay.h"
int main()
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);//使用RCC开启GPIO时钟
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初始化完成
while(1)
{
GPIO_ResetBits(GPIOA,GPIO_Pin_0);
GPIO_SetBits(GPIOA,GPIO_Pin_0);
Delay_ms(500);
GPIO_WriteBit(GPIOA,GPIO_Pin_0, Bit_RESET);
//GPIO_WriteBit(GPIOA,GPIO_Pin_0, (BitAction)0);
Delay_ms(500);
GPIO_WriteBit(GPIOA,GPIO_Pin_0, Bit_SET); //GPIO_WriteBit(GPIOA,GPIO_Pin_0, (BitAction)1);
Delay_ms(500);
}
}
边栏推荐
- (codeforce 807div2)C. Mark and his unfinished essay (thinking)
- Redis uses queues for consumption
- Homework 1-4 learning notes
- Talking about server virtualization & hyper convergence & Storage
- MySQL index
- uuid和索引建立规则
- Fastjson handles string escape characters
- 2022最新抖音直播监控(二)直播间流媒体下载
- Autojs learning - realize image cutting
- STM32 HAL库串口(UART/USART)调试经验(一)——串口通信基础知识+HAL库代码理解
猜你喜欢

Text to image论文精读RAT-GAN:文本到图像合成中的递归仿射变换 Recurrent Affine Transformation for Text-to-image Synthesis

2022最新抖音直播监控(二)直播间流媒体下载

Js九九乘法表

mysql的安装
![Unity Huatuo revolutionary hot update series [1]](/img/bc/ed480fc979c08c362784a3956d7fe2.jpg)
Unity Huatuo revolutionary hot update series [1]
![[详解C语言]一文带你认识C语言,让你醍醐灌顶](/img/37/205c1c6eb2ba704941e48ff89c6268.png)
[详解C语言]一文带你认识C语言,让你醍醐灌顶

Hash索引和B+树相关知识

科学计算库 —— Matplotlib

Unity Huatuo example project source code analysis and inspiration

Flink1.13.6 detailed deployment method
随机推荐
6.30滴滴面经(一面+二面)
Unity Huatuo revolutionary hot update series [1]
Text to image论文精读GR-GAN:逐步细化文本到图像生成 GRADUAL REFINEMENT TEXT-TO-IMAGE GENERATION
Js九九乘法表
【volatile原理】volatile原理
Makefile
ERROR! MySQL is not running, but PID file exists
2022最新抖音直播监控(二)直播间流媒体下载
MySQL single table query exercise
How can smart people leave without offending others?
JS逻辑运算符
[FPGA tutorial case 29] the second DDS direct digital frequency synthesizer based on FPGA - Verilog development
Unity Huatuo hot update environment installation and sample project
Redis uses queues for consumption
Enumerated valueof() method stepping on the pit
Three methods that can effectively fuse text and image information -- feature stitching, cross modal attention, conditional batch normalization
2022年T2I文本生成图像 中文期刊论文速览-1(ECAGAN:基于通道注意力机制的文本生成图像方法+CAE-GAN:基于Transformer交叉注意力的文本生成图像技术)
[translation] reduced precision in tensorrt
QoS quality of service - QoS overview
How does MySQL get the first three items in each group