模仿STM32设置引脚的输出输入

文章提供了两个C语言函数,一个用于设置GPIO端口的输出值(GPIO_Write),另一个用于读取GPIO端口的输入状态(GPIO_ReadInputDataBit)。函数通过枚举类型定义了GPIO端口和引脚编号,以及位操作状态。在函数中,根据GPIOx的值对P0到P3的端口进行位操作,实现了数据的写入和读取。
摘要由CSDN通过智能技术生成

输出

#ifndef C51_GPIO_H
#define C51_GPIO_H
#include "reg52.h"


typedef enum
{
	P_0 = 0,
	P_1,
	P_2,
	P_3
}GPIO_TypeDef;
typedef enum
{
	GPIO_Pin_0 = 0x00,
	GPIO_Pin_1 = 0x01,
	GPIO_Pin_2 = 0x02,
	GPIO_Pin_3 = 0x03,
	GPIO_Pin_4 = 0x04,
	GPIO_Pin_5 = 0x05,
	GPIO_Pin_6 = 0x06,
	GPIO_Pin_7 = 0x07,
 
}GPIO_PinNum_t;
typedef enum
{
	Bit_RESET = 0,
	Bit_SET
}BitAction;
 
 
void GPIO_Write(GPIO_TypeDef P_x, GPIO_PinNum_t GPIO_Pin, BitAction BitVal);
unsigned char GPIO_ReadInputDataBit(GPIO_TypeDef P_x, GPIO_PinNum_t GPIO_Pin);

#endif

输入

#include "c51_gpio.h"

void GPIO_Write(GPIO_TypeDef P_x, GPIO_PinNum_t GPIO_Pin, BitAction BitVal)
{
	switch(P_x)
	{
		case P_0:
			P0 = P0 & (~(0x01 << GPIO_Pin));
			P0 = P0 | (BitVal << GPIO_Pin);
			break;
		case P_1:
			P1 = P1 & (~(0x01 << GPIO_Pin));
			P1 = P1 | (BitVal << GPIO_Pin);
			break;
		case P_2:
			P2 = P2 & (~(0x01 << GPIO_Pin));
			P2 = P2 | (BitVal << GPIO_Pin);
			break;
		case P_3:
			P3 = P3 & (~(0x01 << GPIO_Pin));
			P3 = P3 | (BitVal << GPIO_Pin);
			break;
	default:
			break;
	}
 
}

unsigned char GPIO_ReadInputDataBit(GPIO_TypeDef P_x, GPIO_PinNum_t GPIO_Pin)
{
	unsigned char bitstatus = 0x00;
  
	switch(P_x)
	{
		case P_0:
			bitstatus = P0 & GPIO_Pin;
			break;
		case P_1:
			bitstatus = P1 & GPIO_Pin;
			break;
		case P_2:
			bitstatus = P2 & GPIO_Pin;
			break;
		case P_3:
			bitstatus = P3 & GPIO_Pin;
			break;
		default:
			break;
	}
	if(bitstatus != Bit_RESET)
	{
		bitstatus = Bit_SET;
	}
	return bitstatus;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

入门->放弃

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

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

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

打赏作者

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

抵扣说明:

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

余额充值