STC单片机引脚模式配置函数封装

STC单片机配置GPIO引脚模式有点麻烦,所以就自己写个比较方便的配置函数

void SetGpioMode(unsigned char gpio,unsigned char pin,unsigned char mode)第一个参数选择哪组GPIOGPIOP0至GPIOP5第二个参数选择引脚PIN_0至PIN_7第三个参数设置引脚模式GPIO_Mode_Out_IN准双向 GPIO_Mode_Out_PP推挽 GPIO_Mode_AIN高阻GPIO_Mode_Out_OD开漏	```


头文件代码

#include <reg52.h>

sfr P0M1        =   0x93;
sfr P0M0        =   0x94;
sfr P1M0 = 0x92;
sfr P1M1 = 0x91;
sfr P3M0 = 0xb2;
sfr P3M1 = 0xb1;
sfr P5M0 = 0xca;
sfr P5M1 = 0xc9;
sfr P5=0xC8;

//GPIO封装
#define GPIOP0 0x00
#define GPIOP1 0x01
#define GPIOP2 0x02
#define GPIOP3 0x03
#define GPIOP4 0x04
#define GPIOP5 0x05

#define GPIO_Mode_Out_IN 0x00//M1:M0=00为准双向口 01为推挽 10为高阻 11为开漏
#define GPIO_Mode_Out_PP 0x01
#define GPIO_Mode_AIN 0x02 
#define GPIO_Mode_Out_OD 0x03

#define PIN_0 0x01
#define PIN_1 0x02
#define PIN_2 0x04
#define PIN_3 0x08
#define PIN_4 0x10
#define PIN_5 0x20
#define PIN_6 0x40
#define PIN_7 0x80
#define PIN_L4 0x0F
#define PIN_H4 0xF0
#define PIN_ALL 0xFF
//GPIO封装






源文件代码

//引脚配置数据缓存
unsigned char P0M1Buf=0;
unsigned char P0M0Buf=0;
unsigned char P1M1Buf=0;
unsigned char P1M0Buf=0;
unsigned char P2M1Buf=0;
unsigned char P2M0Buf=0;
unsigned char P3M1Buf=0;
unsigned char P3M0Buf=0;
unsigned char P4M1Buf=0;
unsigned char P4M0Buf=0;
unsigned char P5M1Buf=0;
unsigned char P5M0Buf=0;
//引脚配置数据缓存


void SetGpioMode(unsigned char gpio,unsigned char pin,unsigned char mode)//设置GPIO模式  自己写的函数  为了方便配置引脚
{

	if(gpio==0)
	{
		if(mode==0)//准双向
		{
			P0M1Buf&=~pin;// M1:M0=00为准双向口
			P0M0Buf&=~pin;//
		}else if(mode==1)//推挽
		{
			P0M1Buf&=~pin;// M1:M0=01为推挽
			P0M0Buf|=pin;//
		}else if(mode==2)//模拟输入
		{
			P0M1Buf|=pin;// M1:M0=10为高阻
			P0M0Buf&=~pin;//
		}else if(mode==3)//开漏
		{
			P0M1Buf|=pin;// M1:M0=11为开漏
			P0M0Buf|=pin;//
		}
	}else if(gpio==1)
	{
		if(mode==0)//准双向
		{
			P1M1Buf&=~pin;// M1:M0=00为准双向口
			P1M0Buf&=~pin;//
		}else if(mode==1)//推挽
		{
			P1M1Buf&=~pin;// M1:M0=01为推挽
			P1M0Buf|=pin;//
		}else if(mode==2)//模拟输入
		{
			P1M1Buf|=pin;// M1:M0=10为高阻
			P1M0Buf&=~pin;//
		}else if(mode==3)//开漏
		{
			P1M1Buf|=pin;// M1:M0=11为开漏
			P1M0Buf|=pin;//
		}
	}else if(gpio==2)
	{
		if(mode==0)//准双向
		{
			P2M1Buf&=~pin;// M1:M0=00为准双向口
			P2M0Buf&=~pin;//
		}else if(mode==1)//推挽
		{
			P2M1Buf&=~pin;// M1:M0=01为推挽
			P2M0Buf|=pin;//
		}else if(mode==2)//模拟输入
		{
			P2M1Buf|=pin;// M1:M0=10为高阻
			P2M0Buf&=~pin;//
		}else if(mode==3)//开漏
		{
			P2M1Buf|=pin;// M1:M0=11为开漏
			P2M0Buf|=pin;//
		}
	}else if(gpio==3)
	{
		if(mode==0)//准双向
		{
			P3M1Buf&=~pin;// M1:M0=00为准双向口
			P3M0Buf&=~pin;//
		}else if(mode==1)//推挽
		{
			P3M1Buf&=~pin;// M1:M0=01为推挽
			P3M0Buf|=pin;//
		}else if(mode==2)//模拟输入
		{
			P3M1Buf|=pin;// M1:M0=10为高阻
			P3M0Buf&=~pin;//
		}else if(mode==3)//开漏
		{
			P3M1Buf|=pin;// M1:M0=11为开漏
			P3M0Buf|=pin;//
		}
	}else if(gpio==4)
	{
		if(mode==0)//准双向
		{
			P4M1Buf&=~pin;// M1:M0=00为准双向口
			P4M0Buf&=~pin;//
		}else if(mode==1)//推挽
		{
			P4M1Buf&=~pin;// M1:M0=01为推挽
			P4M0Buf|=pin;//
		}else if(mode==2)//模拟输入
		{
			P4M1Buf|=pin;// M1:M0=10为高阻
			P4M0Buf&=~pin;//
		}else if(mode==3)//开漏
		{
			P4M1Buf|=pin;// M1:M0=11为开漏
			P4M0Buf|=pin;//
		}
	}else if(gpio==5)
	{
		if(mode==0)//准双向
		{
			P5M1Buf&=~pin;// M1:M0=00为准双向口
			P5M0Buf&=~pin;//
		}else if(mode==1)//推挽
		{
			P5M1Buf&=~pin;// M1:M0=01为推挽
			P5M0Buf|=pin;//
		}else if(mode==2)//模拟输入
		{
			P5M1Buf|=pin;// M1:M0=10为高阻
			P5M0Buf&=~pin;//
		}else if(mode==3)//开漏
		{
			P5M1Buf|=pin;// M1:M0=11为开漏
			P5M0Buf|=pin;//
		}
	}

}

void IoModeInit()//IO口模式初始化
{
	P0M1=P0M1Buf;// M1:M0=00为准双向口 01为推挽 10为高阻 11为开漏
	P0M0=P0M0Buf;//
	P1M1=P1M1Buf;// M1:M0=00为准双向口 01为推挽 10为高阻 11为开漏
	P1M0=P1M0Buf;//
	P3M1=P3M1Buf;// M1:M0=00为准双向口 01为推挽 10为高阻 11为开漏
	P3M0=P3M0Buf;//
	P5M1=P5M1Buf;// M1:M0=00为准双向口 01为推挽 10为高阻 11为开漏
	P5M0=P5M0Buf;//
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

小白q_5793545

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

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

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

打赏作者

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

抵扣说明:

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

余额充值