当前位置:网站首页>第四讲—讲解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流水灯。
边栏推荐
- Text to image论文精读RAT-GAN:文本到图像合成中的递归仿射变换 Recurrent Affine Transformation for Text-to-image Synthesis
- 6.30联发科笔试
- 指针常量与常量指针详细讲解
- MySQL index
- 【volatile原理】volatile原理
- 6.29 众安暑期测开实习一面
- [untitled]
- ERROR! MySQL is not running, but PID file exists
- FID指标复现踩坑避坑 文本生成图像FID定量实验全流程复现(Fréchet Inception Distance )定量评价实验踩坑避坑流程
- MySQL backup recovery
猜你喜欢
![[paddleseg source code reading] paddleseg export static graph export Trace in py file](/img/63/ac9d1a569816e8addb23bf143364f5.png)
[paddleseg source code reading] paddleseg export static graph export Trace in py file
![[cann training camp] enter media data processing 1](/img/6c/76d3784203af18a7dee199c3a7fd24.png)
[cann training camp] enter media data processing 1

Talking about server virtualization & hyper convergence & Storage

ViTGAN:用视觉Transformer训练生成性对抗网络 Training GANs with Vision Transformers

Virtualization technology KVM
![Unity Huatuo revolutionary hot update series [1]](/img/bc/ed480fc979c08c362784a3956d7fe2.jpg)
Unity Huatuo revolutionary hot update series [1]

Solution: various error reporting and pit stepping and pit avoiding records encountered in the alchemist cultivation plan pytoch+deeplearning (III)

Unity Huatuo example project source code analysis and inspiration

MySQL view

Hands on experiment of network and VPC
随机推荐
力扣获取第二大的成绩
Specify that SQL only supports select syntax
--Project summary
[reprint] 6. Tensorrt advanced usage
【数据库课程设计】SQLServer数据库课程设计(学生宿舍管理),课设报告+源码+数据库关系图
[cann training camp] enter media data processing 1
Atcoder D - increment of coins (probability DP)
Transport layer --------- TCP (II)
D - Difference HDU - 5936
利用九天深度学习平台复现SSA-GAN
Solution: various error reports and pit stepping and pit avoidance records encountered in the alchemist cultivation plan pytoch+deeplearning (I)
Machine learning exercise 6 - Support Vector Machines
6.28同花顺笔试
Autojs learning - realize image cutting
Introduction to network - Introduction to home networking & basic network knowledge
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
count(*)为什么很慢
事务数据库及其四特性,原理,隔离级别,脏读,幻读,不可重复读?
2022年T2I文本生成图像 中文期刊论文速览-1(ECAGAN:基于通道注意力机制的文本生成图像方法+CAE-GAN:基于Transformer交叉注意力的文本生成图像技术)
JS 99 multiplication table