怎么样把两个C语言程序叠加在一起?
#include<reg51.h>#define unchar unsigned char
#define uint unsigned int
/********************************
宏观定义
********************************/
#define _DEVICEFLAG_
uint num = 0;
uint Control = 0; //设置控制开关转变量
/***********************************
定义开关状态量
***********************************/
sbit Jiqi_Key = P0^0; //机器总开关
sbit Reset1 = P3^2; //复位开关1
sbit Reset2 = P3^3; //复位开关2
sbit Fuwuqi_Key = P0^3; //服务器+5V
/**********************************
继电器是否吸合状态判断引脚
**********************************/
sbit Jidian_KEY1 = P0^4; //无线路由器继电器开关KEY1
sbit Jidian_KEY2 = P0^5; //IPpbx继电器开关KEY2
/***********************************
定义开关控制量
***********************************/
sbit KEY5 = P2^0; //总电源指示灯;
sbit KEY2 = P2^1; //服务器 reset
sbit KEY3 = P2^2; //无线路由器reset
sbit KEY4 = P2^3;
sbit KEY1 = P2^4; //网络开关
sbit KEY6 = P2^5; //IPPBX开关
/***************************************
开关延迟
****************************************/
void delay_ms(uint del)
{
uint i,j;
for(i=0;i<del;i++)
for(j=0;j<1827;j++)
;
}
/**************************************
LED灯延迟
***************************************/
/*void char_delay_ms(void)
{
unsigned char j;
for(j=0;j<50;j++);
}
*/
/*****************************************
单片机初始化
*****************************************/
void CPU_Init(void)
{
P0 = 0x00;
P2 = 0x00;
}
/**************************************
定时器中断初始化
***************************************/
void Jiqi_Key_Init(void)
{
TMOD=0x01; //定时器0,工作方式1
TH0 = (65536-45872)/256;
TL0 = (65536-45872)%256;
ET0 = 1;
TR0 = 1;
EA = 1;
}
/****************************************
定时器0溢出中断
****************************************/
void Jiqi_Key_Int(void) interrupt 1 //机器总开关Jiqi_Key 启动定时中断
{
TH0 = (65536-45872)/256;
TL0 = (65536-45872)%256;
num++;
if(num == 5)
{
TR0 = 0;
if(Jiqi_Key == 1)
{
#ifdef _DEVICEFLAG_
delay_ms(10);
if(Jiqi_Key == 0)
{
Control = 2;
}
else if(Jiqi_Key == 1)
{
Control = 1;
}
#endif
}
num = 0;
TR0 = 1;
}
}
/*************************************
外部中断初始化
*************************************/
void reset_Init(void)
{
EX0 = 1; //INT0中断开
EX1 = 1; //INT1中断开
IT0 = 0; // INT0低电平触发(为1则为下降沿触发)
IT1 = 0; //INT1低电平触发(为1则为下降沿触发)
EA = 1; //CPU所有中断开(IE最高位MSB)
}
/****************************************
外部中断0
****************************************/
void reset1_Int(void) interrupt 0 //复位开关reset1路由器 启动中断
{
delay_ms(3);
EX0 = 0;
if(Reset1 == 0)
{
delay_ms(10);
if(Reset1 == 0)
{
Control = 3; //复位开关1长按
}
else if (Reset1 == 1)
{
Control = 4; //复位开关1短按
}
}
EX0 = 1; //清除interrupt 1中断
}
/****************************************
外部中断1
****************************************/
void reset2_Int(void) interrupt 2 //复位开关reset2 服务器 启动中断
{
delay_ms(3);
EX1 = 0;
if(Reset2 == 0)
{
delay_ms(10);
if(Reset2 == 0)
{
Control = 5; //复位开关2长按
}
else if(Reset2 == 1)
{
Control = 6; //复位开关2短按
}
}
EX1 = 1; //清除interrupt 2中断
}
/*******************************************
控制开关按键 路由+网络
*******************************************/
void main()
{
Jiqi_Key_Init();
CPU_Init();
reset_Init();
while(1)
{
#ifdef _DEVICEFLAG_
if((Jidian_KEY1 == 0)||(Fuwuqi_Key == 1)) //判断服务器+5V为1时,KEY5闭合
{
KEY5 = 1;
}
else if((Fuwuqi_Key == 0)&&(Jidian_KEY1 == 1)) //判断服务器+5V为0时,KEY5断开
{
KEY5 =0;
}
#endif
switch(Control)
{
case 1: //机器总开关长按
if(Fuwuqi_Key == 0) //判断服务器+5V为0时
{
if(Jidian_KEY1 == 1) //继电器K1为断开时
{
KEY1 = 1;
KEY4 = 1;
delay_ms(100);
KEY4 = 0;
}
if(Jidian_KEY1 == 0)
{
KEY1 = 0;
}
}
else if(Fuwuqi_Key == 1) //服务器+5V为1时
{
if(Jidian_KEY1 == 1) //继电器K1断开时
{
KEY4 =1;
delay_ms(100);
delay_ms(100);
delay_ms(100);
delay_ms(100);
KEY4 = 0;
}
else if(Jidian_KEY1 == 0) //继电器K1闭合时
{
KEY1 = 1;
KEY4 = 1;
delay_ms(100);
delay_ms(100);
delay_ms(100);
KEY1 = 0;
delay_ms(100);
KEY4 = 0;
}
}
Control = 0;
break;
case 2: //机器总开关短按
if(Fuwuqi_Key == 0) //判断服务器+5V为0时
{
if(Jidian_KEY1 == 1) //继电器K1为断开时
{
KEY1 = 1;
KEY4 = 1;
delay_ms(100);
KEY4 = 0;
}
else if (Jidian_KEY1 == 0) //继电器K1为闭合时
{
KEY4 = 1;
delay_ms(100);
KEY4 = 0;
}
}
else if(Fuwuqi_Key ==1) //服务器+5V为1时
{
if(Jidian_KEY1 == 1) //继电器K1断开时
{
KEY1 = 1;
}
else if(Jidian_KEY1 == 0) //继电器K1闭合时
{
;
}
}
Control = 0;
break;
case 3: //复位开关1长按
if(Jidian_KEY1 == 0) //继电器K1闭合
{
KEY2 = 1;
delay_ms(300);
KEY2 = 0;
}
Control = 0;
break;
case 4: //复位开关1短按
if(Jidian_KEY1 == 0) //继电器K1闭合
{
KEY1 = 0;
delay_ms(100);
KEY1 = 1;
}
Control = 0;
break;
case 5: //复位开关2长按
if(Fuwuqi_Key == 0)
{
KEY3 = 1;
delay_ms(100);
KEY3 = 0;
}
Control = 0;
break;
case 6:
if(Fuwuqi_Key == 0) //复位开关2短按
{
KEY3 = 1;
delay_ms(100);
KEY3 = 0;
}
Control = 0;
default: ;
break;
}
}
}
第二个
#include<reg51.h>
#define unchar unsigned char
#define uint unsigned int
//#define _DEVICEFLAG_
uint num = 0;
uint Control = 0; //设置控制开关转变量
/***********************************
定义开关状态量
***********************************/
sbit Jiqi_Key = P0^0; //机器总开关
sbit Reset1 = P3^2; //复位开关1
sbit Reset2 = P3^3; //复位开关2
/**********************************
继电器是否吸合状态判断引脚
**********************************/
sbit Jidian_KEY1 = P0^4; //无线路由器继电器开关KEY1
sbit Jidian_KEY2 = P0^5; //IPpbx继电器开关KEY2
/***********************************
定义开关控制量
***********************************/
sbit KEY5 = P2^0; //总电源指示灯;
sbit KEY2 = P2^1; //IPpbx reset
sbit KEY3 = P2^2; //无线路由器reset
sbit KEY4 = P2^3;
sbit KEY1 = P2^4; //网络开关
sbit KEY6 = P2^5; //IPPBX开关
/***************************************
开关延迟
****************************************/
void delay_ms(uint del)
{
uint i,j;
for(i=0;i<del;i++)
for(j=0;j<1827;j++)
;
}
/**************************************
LED灯延迟
***************************************/
/*void char_delay_ms(void)
{
unsigned char j;
for(j=0;j<50;j++);
}
*/
/*****************************************
单片机初始化
*****************************************/
void CPU_Init(void)
{
P0 = 0x00;
P2 = 0x00;
}
/**************************************
定时器中断初始化
***************************************/
void Jiqi_Key_Init(void)
{
TMOD=0x01; //定时器0,工作方式1
TH0 = (65536-45872)/256;
TL0 = (65536-45872)%256;
ET0 = 1;
TR0 = 1;
EA = 1;
}
/****************************************
定时器0溢出中断
****************************************/
void Jiqi_Key_Int(void) interrupt 1 //机器总开关Jiqi_Key 启动定时中断
{
TH0 = (65536-45872)/256;
TL0 = (65536-45872)%256;
num++;
if(num == 10)
{
TR0 = 0;
if(Jiqi_Key == 1)
{
#ifdef _DEVICEFLAG_
delay_ms(10);
if(Jiqi_Key == 1)
{
Control = 2;
}
else if(Jiqi_Key == 1)
{
Control = 1;
}
#else
delay_ms(3);
if(Jiqi_Key == 1)
{
Control = 1;
}
#endif
}
num = 0;
TR0 = 1;
}
}
/*************************************
外部中断初始化
*************************************/
void reset_Init(void)
{
EX0 = 1; //INT0中断开
EX1 = 1; //INT1中断开
IT0 = 0; // INT0低电平触发(为1则为下降沿触发)
IT1 = 0; //INT1低电平触发(为1则为下降沿触发)
EA = 1; //CPU所有中断开(IE最高位MSB)
}
/****************************************
外部中断0
****************************************/
void reset1_Int(void) interrupt 0 //复位开关reset1路由器 启动中断
{
delay_ms(3);
EX0 = 0;
if(Reset1 == 0)
{
delay_ms(10);
if(Reset1 == 0)
{
Control = 4; //继电器控制无线路由
}
else if (Reset1 == 1)
{
Control = 3;
}
}
EX0 = 1; //清除interrupt 1中断
}
/****************************************
外部中断1
****************************************/
void reset2_Int(void) interrupt 2 //复位开关reset2 IPpbx 启动中断
{
delay_ms(3);
EX1 = 0;
if(Reset2 == 0)
{
delay_ms(10);
if(Reset2 == 0)
{
Control = 6; //IPpbx继电器开关KEY2
}
else if(Reset2 == 1)
{
Control = 5; //控制IPpbx
}
}
EX1 = 1; //清除interrupt 2中断
}
/*******************************************
控制开关按键 IP pbx+网络
*******************************************/
void main()
{
Jiqi_Key_Init();
CPU_Init();
reset_Init();
while(1)
{
//#ifdef _DEVICEFLAG_
if((Jidian_KEY1 == 1)||(Jidian_KEY2 == 1)) //K1和K2任何一个接通时,K5接通
{
KEY5 = 0;
}
else if((Jidian_KEY1 == 0)&&(Jidian_KEY2 == 0)) //K1和K2全断开,K5断开
{
KEY5 = 1;
}
//#endif
switch(Control)
{
case 1: // if(Jiqi_Key == 1)
// {
if((Jidian_KEY1 == 1)&&(Jidian_KEY2 == 1)) //当K1、K2断开时,此时按总电源开关,K1,K2接通
{
KEY1 = 1;
KEY6 = 1;
}
else if((Jidian_KEY1 == 0)&&(Jidian_KEY2 == 0)) //当K1、K2接通时,此时按总电源开关,K1,K2断开
{
KEY1 = 0;
KEY6 = 0;
}
// }
Control = 0;
break;
case 2:
{;}
// if(Jiqi_Key == 1)
// {
// }
// Control = 0;
break;
case 3: //复位开关1短按
if((Jidian_KEY1 == 0)&&(Jidian_KEY2 == 0))
{
KEY1 = 0;
delay_ms(100);
KEY1 = 1;
}
Control = 0;
break;
case 4:
if((Jidian_KEY1 == 0)&&(Jidian_KEY2 == 0))
{
//复位开关1长按
KEY3 = 1;
delay_ms(400);
KEY3 = 0;
}
Control = 0;
break;
case 5: //复位开关2短按
if((Jidian_KEY1 == 0)&&(Jidian_KEY2 == 0))
{
KEY6 = 0;
delay_ms(100);
KEY6 = 1;
}
Control = 0;
break;
case 6:
if((Jidian_KEY1 == 0)&&(Jidian_KEY2 == 0)) //复位开关2长按
{
KEY4 = 1;
delay_ms(400);
KEY4 = 0;
}
Control = 0;
break;
default:
;
break;
}
}
}