对STC单片机IO输出模式配置进行封装

配置51单片机有点麻烦,所以自己封装了一下
分为config.c和config.h文件,方便移植

config.h文件代码如下

#ifndef __CONFIG_H_
#define __CONFIG_H_
#include<reg52.h>
#include<intrins.h>


#define FOSC 11059200UL
typedef unsigned char u8;
typedef unsigned int u16;
enum //延时任务枚举  最后参数不要加,
{
	 task1_Delay,
	 task2_Delay,
    Time_Delay

};
#define Down_Task  (Time_Delay+1)//向下减数组长度
extern unsigned int Number_Down[Down_Task];     //延时-- 数组

sfr     P0M1    =   0x93;
sfr     P0M0    =   0x94;
sfr     P1M1    =   0x91;
sfr     P1M0    =   0x92;
sfr     P2M1    =   0x95;
sfr     P2M0    =   0x96;
sfr     P3M1    =   0xb1;
sfr     P3M0    =   0xb2;
sfr     P4M1    =   0xb3;
sfr     P4M0    =   0xb4;
sfr     P5M1    =   0xc9;
sfr     P5M0    =   0xca;
sfr P5=0xC8;
sfr WDT_CONTR = 0xc1;


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

#define GPIO_Mode_Out_IN 0//M1:M0=00为准双向口 01为推挽 10为高阻 11为开漏
#define GPIO_Mode_Out_PP 1
#define GPIO_Mode_AIN 2 
#define GPIO_Mode_Out_OD 3

#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

void SetGpioMode(unsigned char gpio,unsigned char pin,unsigned char mode);//设置GPIO模式
void IoModeInit();//IO口模式初始化
void TaskDelay();//延时  1S执行1次



#endif

config.c文件代码如下

#include "config.h"

//引脚配置数据缓存

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;
//引脚配置数据缓存

unsigned int Number_Down[Down_Task];     //延时-- 数组
/*******************************************************************************
* 函数功能 : 任务延时
* 函数说明 : 中断1ms调用一次
*******************************************************************************/

void TaskDelay()
{
    unsigned char i;

    for(i = 0; i < Down_Task; i++)
    {
        if(Number_Down[i])//不为0就--
        {
            Number_Down[i]--; //不为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;//
	P2M1=P2M1Buf;// M1:M0=00为准双向口 01为推挽 10为高阻 11为开漏
	P2M0=P2M0Buf;//
	P3M1=P3M1Buf;// M1:M0=00为准双向口 01为推挽 10为高阻 11为开漏
	P3M0=P3M0Buf;//
	P4M1=P4M1Buf;// M1:M0=00为准双向口 01为推挽 10为高阻 11为开漏
	P4M0=P4M0Buf;//
	P5M1=P5M1Buf;// M1:M0=00为准双向口 01为推挽 10为高阻 11为开漏
	P5M0=P5M0Buf;//
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

小白q_5793545

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

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

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

打赏作者

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

抵扣说明:

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

余额充值