【STC15库函数上手笔记】2、GPIO

STC实验箱4
IAP15W4K58S4
Keil uVision V5.29.0.0
PK51 Prof.Developers Kit Version:9.60.0.0


硬知识

摘自《STC库函数使用参考》

IO口初始化函数

GPIO_Inilize
在这里插入图片描述
GPIO_InitTypeDef的定义见于文件"GPIO.H"。

typedef struct
{
	u8 Mode;
	u8 Pin;		//要设置的端口
}GPIO_InitTypeDef;

Mode:配置IO的模式,取值见下表:
在这里插入图片描述
Pin:要配置的某一个回多个IO,取值见下表:
在这里插入图片描述
以上参数可以使用或运算,比如:

GPIO_InitStructure.Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_7;

测试

在这里插入图片描述

在这里插入图片描述

main.c

P1.7、P1.6、P4.7、P4.6流水灯,P3.2、P3.3按键改变方向。

#include "./Drivers/config.h"
#include "./Drivers/delay.h"

#include "./Drivers/GPIO.h"

void GPIO_config(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;					//结构定义
	GPIO_InitStructure.Mode = GPIO_OUT_PP;					//指定IO的输入或输出方式,GPIO_PullUp,GPIO_HighZ,GPIO_OUT_OD,GPIO_OUT_PP
	GPIO_InitStructure.Pin  = GPIO_Pin_6 | GPIO_Pin_7;		//指定要初始化的IO, 或操作
	GPIO_Inilize(GPIO_P1,&GPIO_InitStructure);				//P1.6、P1.7初始化为推挽输出
	GPIO_Inilize(GPIO_P4,&GPIO_InitStructure);				//P4.6、P4.7初始化为推挽输出
	GPIO_InitStructure.Pin  = GPIO_Pin_2 | GPIO_Pin_3;		//指定要初始化的IO, 或操作
	GPIO_InitStructure.Mode = GPIO_PullUp;					//指定IO的输入或输出方式,GPIO_PullUp,GPIO_HighZ,GPIO_OUT_OD,GPIO_OUT_PP
	GPIO_Inilize(GPIO_P3,&GPIO_InitStructure);				//P3.2、P3.3初始化为上拉输入
	P32 = 1;
	P33 = 1;
}

void main(void)
{
	u8 Dir_Flag = 0;
	u8 LED_Port = 0;
	u8 i = 0;
	GPIO_config();
	while(1)
	{
		if (P32 == 0)
		{
			delay_ms(20);
			if (P32 == 0)
			{
				Dir_Flag = 0;
			}
			while (P32 == 0);
		}
		if (P33 == 0)
		{
			delay_ms(20);
			if (P33 == 0)
			{
				Dir_Flag = 1;
			}
			while (P33 == 0);
		}
		if (Dir_Flag)
		{
			LED_Port >>= 1;
			if ((LED_Port & 0x0f) == 0x00)
				LED_Port = 0x08;
		}
		else
		{
			LED_Port <<= 1;
			if ((LED_Port & 0x0f) == 0x00)
				LED_Port = 0x01;
		}
		P17 = (((~LED_Port) & 0x01) != 0);
		P16 = (((~LED_Port) & 0x02) != 0);
		P47 = (((~LED_Port) & 0x04) != 0);
		P46 = (((~LED_Port) & 0x08) != 0);
		i = 25;
		while (P32 && P33 && i--)
			delay_ms(20);
	}
}

实验现象

在这里插入图片描述

  • 5
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

乙酸氧铍

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值