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;//
}