当前位置:网站首页>第四讲—讲解GPIO_Write函数以及相关例程
第四讲—讲解GPIO_Write函数以及相关例程
2022-07-26 22:48:00 【若木·】
前言
之前讲解了初学GPIO的几个函数,下面介绍又一个函数,此函数对所有端口进行操作
GPIO_Write函数,与上一篇里边讲解的其他函数相比,最大的特点是它可以对多个端口进行赋值操作。
先看函数定义:
void GPIO_Write(GPIO_TypeDef* GPIOx, uint16_t PortVal)
{
/* Check the parameters */
assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
GPIOx->ODR = PortVal;
}
第一个参数与之前一样,因为是对多个端口操作,我们以GPIOA的所有端口为例,第一个参数还是GPIOA,重点看第二个参数,看参数定义:
* @param PortVal: specifies the value to be written to the port output data register.
它的意思是,指定要写入端口输出数据寄存器的值。
以LED流水灯为例,用该函数我们可以直接配置PA0-PA15 16个端口
直接上代码:
#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_All;//把16个端口全部配置为推挽输出
//GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 ;//按位“或”选中多个端口
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz ;
GPIO_Init(GPIOA,&GPIO_InitStructure);
//到上边为止,GPIO初始化完成
while(1)
{
GPIO_Write(GPIOA,~0X0001);//因为低电平点亮,所以按位取反,此时第一个LED点亮其他为灭
//0000 0000 0000 0001 对应PA0-PA15 16个端口,最低位PA0
Delay_ms(500);
GPIO_Write(GPIOA,~0X0002);//0000 0000 0000 0010
Delay_ms(500);
GPIO_Write(GPIOA,~0X0004);//0000 0000 0000 0100
Delay_ms(500);
GPIO_Write(GPIOA,~0X0008);//0000 0000 0000 1000
Delay_ms(500);
GPIO_Write(GPIOA,~0X0010);//0000 0000 0001 0000
Delay_ms(500);
GPIO_Write(GPIOA,~0X0020);//0000 0000 0010 0000
Delay_ms(500);
GPIO_Write(GPIOA,~0X0040);//0000 0000 0100 0000
Delay_ms(500);
GPIO_Write(GPIOA,~0X0080);//0000 0000 1000 0000
Delay_ms(500);
}
}
需要注意的是,在GPIO_Init中结构体的Pin要改为全部端口,即为GPIO_Pin_All 。
在死循环中,第二个参数用十六进制数表示,因为我们选择LED为低电平点亮,所以我们按位取反,此时第一个LED点亮其他为灭,按位取反操作依次点亮每个端口上的LED,加入延时即可实现LED流水灯。
边栏推荐
- Harmonyos image processing application development live broadcast notes
- Freytek central computing platform 360 degree sensing system solves the challenges behind NOA mass production
- 6.28同花顺笔试
- Shell course summary
- 详解文本生成图像的仿射变换模块(Affine Transformation)和条件批量标准化(CBN)
- Machine learning exercise 7 - K-means and PCA (principal component analysis)
- 【数据库课程设计】SQLServer数据库课程设计(学生宿舍管理),课设报告+源码+数据库关系图
- Virtualization technology KVM
- 读写分离、主从同步一文搞定
- [详解C语言]一文带你认识C语言,让你醍醐灌顶
猜你喜欢

Harmonyos image processing application development live broadcast notes

FID index reproduction step on the pit to avoid the pit text generation image FID quantitative experiment whole process reproduction (FR é Chet inception distance) quantitative evaluation experiment s
![[FPGA tutorial case 28] one of DDS direct digital frequency synthesizers based on FPGA -- principle introduction](/img/bf/ce4bc33d2a0fc7fe57105e20fbafcf.png)
[FPGA tutorial case 28] one of DDS direct digital frequency synthesizers based on FPGA -- principle introduction

解决方案:炼丹师养成计划 Pytorch+DeepLearning遇见的各种报错与踩坑避坑记录(二)

Removal and addition of reference types in template and generic programming

Hands on experiment of network and VPC

【mysql】mysql启动关闭命令以及一些报错解决问题

Flink1.13.6 detailed deployment method

MySQL单表查询练习
![[reprint] GPU compute capability table](/img/7c/87be1131f52f21fe080c36f0834467.png)
[reprint] GPU compute capability table
随机推荐
uuid和索引建立规则
指针常量与常量指针详细讲解
Text to image论文精读GR-GAN:逐步细化文本到图像生成 GRADUAL REFINEMENT TEXT-TO-IMAGE GENERATION
Homework 1-4 learning notes
选择器的使用语法与场景以及背景图片大小、文字盒子阴影、过度效果的使用方法
解决方案:读取两个文件夹里不同名的文件,处理映射不对应的文件
Virtualization technology KVM
索引失效原理讲解及其常见情况
25pxe efficient batch network installation
Three methods that can effectively fuse text and image information -- feature stitching, cross modal attention, conditional batch normalization
解决方案:炼丹师养成计划 Pytorch+DeepLearning遇见的各种报错与踩坑避坑记录(二)
2022最新直播监控24小时监控(三)直播间弹幕解析
Shell programming specifications and variables
2022年最新文本生成图像研究 开源工作速览(Papers with code)
行,表,页,共享,排他,悲观,乐观,死锁
超出隐藏显示省略号
mysql存储引擎及其区别
mysql优化概论
Realize data interaction between two apps through fileprovider
[cann training camp] enter media data processing 1