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