当前位置:网站首页>第四讲—讲解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流水灯。
边栏推荐
- [详解C语言]一文带你玩转函数
- Solution: various error reports and pit stepping and pit avoidance records encountered in the alchemist cultivation plan pytoch+deeplearning (II)
- Unity Huatuo revolutionary hot update series [1]
- In regular expressions (?: pattern), (?! pattern), (? < =pattern) and (?
- Text to image论文精读GR-GAN:逐步细化文本到图像生成 GRADUAL REFINEMENT TEXT-TO-IMAGE GENERATION
- Autojs learning - realize image cutting
- 21dns domain name resolution
- Freytek central computing platform 360 degree sensing system solves the challenges behind NOA mass production
- Harmonyos image processing application development live broadcast notes
- [reprint] GPU compute capability table
猜你喜欢

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

Pseudo class of a element

Talking about server virtualization & hyper convergence & Storage

left join 、inner join 、right join区别

23nfs shared storage service
![[translation] explicit and implicit batch in tensorrt](/img/17/3f00697d53ff43cd881960849da5f7.png)
[translation] explicit and implicit batch in tensorrt

Machine learning exercise 7 - K-means and PCA (principal component analysis)

MySQL master-slave replication and read-write separation

STM32 HAL库串口(UART/USART)调试经验(一)——串口通信基础知识+HAL库代码理解

Text to image intensive reading of paper gr-gan: gradually refine text to image generation
随机推荐
shell课程总结
Beyond hidden display ellipsis
[translation] explicit and implicit batch in tensorrt
2022zui新抖音24小时循环值守直播监控(一)直播间开播监控
Desktop solution summary
三种能有效融合文本和图像信息的方法——特征拼接、跨模态注意、条件批量归一化
Tinyint type map is received and returned as Boolean
2022年最新文本生成图像研究 开源工作速览(Papers with code)
Docker高级篇之Mysql主从复制、Redis集群扩容缩容配置案例详解
Solution: various error reporting and pit stepping and pit avoiding records encountered in the alchemist cultivation plan pytoch+deeplearning (III)
7.8 锐捷网络笔试
SQL anti injection regular expression
Text to image intensive reading df-gan:a simple and effective baseline for text to image synthesis
Text to image论文精读SSA-GAN:基于语义空间感知的文本图像生成 Text to Image Generation with Semantic-Spatial Aware GAN
mysql优化概论
6.30联发科笔试
Mysql数据库-面试题
JS logical operator
[详解C语言]一文带你玩转函数
Unity Huatuo example project source code analysis and inspiration