如题:
//---------------------STC12c5A08S2双串口控制---------------------------------------------
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
sfr WDT_CONTR=0XC1;
//-------串口2寄存器------------
sfr S2CON=0X9A;
sfr S2BUF=0X9B;
sfr BRT=0X9C;
sfr AUXR=0X8E;
sfr IE2=0XAF;
sfr IP2=0XB5;
//sfr AUXR1=0XA2;
sfr IPH2=0xB6;
//------------------------------
sbit P00=P0^0;
sbit P01=P0^1;
sbit P02=P0^2;
sbit P03=P0^3;
sbit P04=P0^4;
sbit P05=P0^5;
sbit P06=P0^6;
sbit P07=P0^7;
sbit P10=P1^0;
sbit P11=P1^1;
sbit P12=P1^2;
sbit P13=P1^3;
sbit P14=P1^4;
sbit P15=P1^5;
sbit P16=P1^6;
sbit P17=P1^7;
sbit P20=P2^0;
sbit P21=P2^1;
sbit P22=P2^2;
sbit P23=P2^3;
sbit P24=P2^4;
sbit P25=P2^5;
sbit P26=P2^6;
sbit P27=P2^7;
sbit P30=P3^0;
sbit P31=P3^1;
sbit P32=P3^2;
sbit P33=P3^3;
sbit P34=P3^4;
sbit P35=P3^5;
sbit P36=P3^6;
sbit P37=P3^7;
/*-------------------1号----------------------------------------------------------------*/
uchar Aaddress;
uchar Acc;
uchar Areceive_date1,Areceive_date2,Areceive_date3,Areceive_date4;
uchar Areceive_date1_back,Areceive_date2_back,Areceive_date3_back;
uchar Acalculate_all;
uchar Asend_date1,Asend_date2,Asend_date3,Asend_date4;
uchar Asend_date5;
/*-------------------1号结束----------------------------------------------------------------*/
/*-------------------2号----------------------------------------------------------------*/
uchar Baddress;
uchar Bcc;
uchar Breceive_date1,Breceive_date2,Breceive_date3,Breceive_date4;
uchar Breceive_date1_back,Breceive_date2_back,Breceive_date3_back;
uchar Bcalculate_all;
uchar Bsend_date1,Bsend_date2,Bsend_date3,Bsend_date4;
uchar Bsend_date5;
/*-------------------2号结束----------------------------------------------------------------*/
uchar date_room,date_room_back,date_room_back_buma;
uchar date_lamp_num,date_lamp_num_back,date_lamp_num_back_buma;
uchar t=0;
//#######################################延时子程序##############################################
void delay(uint dat) //63US
{
unsigned int k;
for(k=0;k<(dat*12);k++)
{
WDT_CONTR=0X3C;
}
}
//###############################################################################################
void init(void)
{
P0=0XFF;
P1=0XFF;
P2=0XFF;
P3=0XFF;
//------------串口1设置--------------------------------------
SCON=0X50; //串行方式1,允许接收
TMOD=0X21; //定时器1,方式2 定时器0 方式1
TH1=0XFD; //波特率9600
TL1=0XFD;
ET1=0;
TR1=0;
TI=0;
RI=0;
ES=1; //串行中断
//--------------------------------------------------------
//---------------串口2设置--------------------------------
S2CON=0X50;
BRT=0xfd; //9600
AUXR=0X10;
IE2=0X01; //ES2=1;
// S2CON=S2CON&0XFE; //S2RI=0;
// S2CON=S2CON|0X01; //S2RI=1;
// S2CON=S2CON&0XFD; //S2TI=0;
// S2CON=S2CON|0X02; //S2TI=1;
//---------------------------------------------------------
EX0=1;
IT0=1; //下降沿触发
EX1=0;
IT1=0; //低电平触发
TH0=0X3C; //50MS 12M
TL0=0XB0;
ET0=0;
TR0=0;
EA=1;
PS=1;
//----------------------------------------------------------------------------
/*-------------------1号----------------------------------*/
Acc=0;
Areceive_date1=Areceive_date2=Areceive_date3=0x00;
Areceive_date1_back=Areceive_date2_back=Areceive_date3_back=0x00;
Areceive_date4=0x00;
Acalculate_all=0x00;
Asend_date1=Asend_date2=Asend_date3=0x00;
Asend_date4=(Asend_date1^Asend_date2^Asend_date3)&0X7F;
Asend_date5=Asend_date1+Asend_date2+Asend_date3+Asend_date4;
Asend_date5=Asend_date5&0x7f;
/*------------------1号结束--------------------------------------*/
/*-------------------2号----------------------------------*/
Bcc=0;
Breceive_date1=Breceive_date2=Breceive_date3=0x00;
Breceive_date1_back=Breceive_date2_back=Breceive_date3_back=0x00;
Breceive_date4=0x00;
Bcalculate_all=0x00;
Bsend_date1=Bsend_date2=Bsend_date3=0x00;
Bsend_date4=(Bsend_date1^Bsend_date2^Bsend_date3)&0X7F;
Bsend_date5=Bsend_date1+Bsend_date2+Bsend_date3+Bsend_date4;
Bsend_date5=Bsend_date5&0x7f;
/*------------------2号结束--------------------------------------*/
date_room=date_room_back=date_room_back_buma=0x00;
date_lamp_num=date_lamp_num_back=date_lamp_num_back_buma=0x00;
}
//-------------------准备数据----------------------------------------
//-------------------------------------------------------------------
void shujufenxi(uchar num)
{
/*----------------------------1号---------------------------------------------*/
if(date_room==0x01)
{
if(num<=5)
{
Asend_date2=Areceive_date2;
Asend_date2=(Asend_date2<<(8-num))|(Asend_date2>>num);
if((date_lamp_num&0x0f)==0x0a) Asend_date2=Asend_date2|0x01; //开灯
if((date_lamp_num&0x0f)==0x0b) Asend_date2=Asend_date2&0xfe; //关灯
Asend_date2=(Asend_date2>>(8-num))|(Asend_date2<<num);
}
if(num>=6)
{
Asend_date3=Areceive_date3;
Asend_date3=(Asend_date3<<(14-num))|(Asend_date3>>(num-6));
if((date_lamp_num&0x0f)==0x0a) Asend_date3=Asend_date3|0x01; //开灯
if((date_lamp_num&0x0f)==0x0b) Asend_date3=Asend_date3&0xfe; //关灯
Asend_date3=(Asend_date3>>(14-num))|(Asend_date3<<(num-6));
}
}
/*----------------------------1号结束----------------------------------------------------*/
/*----------------------------2号--------------------------------------------------------*/
if(date_room==0x02)
{
if(num<=5)
{
Bsend_date2=Breceive_date2;
Bsend_date2=(Bsend_date2<<(8-num))|(Bsend_date2>>num);
if((date_lamp_num&0x0f)==0x0a) Bsend_date2=Bsend_date2|0x01; //开灯
if((date_lamp_num&0x0f)==0x0b) Bsend_date2=Bsend_date2&0xfe; //关灯
Bsend_date2=(Bsend_date2>>(8-num))|(Bsend_date2<<num);
}
if(num>=6)
{
Bsend_date3=Breceive_date3;
Bsend_date3=(Bsend_date3<<(14-num))|(Bsend_date3>>(num-6));
if((date_lamp_num&0x0f)==0x0a) Bsend_date3=Bsend_date3|0x01; //开灯
if((date_lamp_num&0x0f)==0x0b) Bsend_date3=Bsend_date3&0xfe; //关灯
Bsend_date3=(Bsend_date3>>(14-num))|(Bsend_date3<<(num-6));
}
}
/*----------------------------2号结束----------------------------------------------------*/
}
//----------------------------------------------------------------------------------
void zhunbeishuju(void)
{
/*----------------------------1号------------------------------------------------------*/
if(date_room==0x01)
{
if(date_lamp_num==0xbb) //bb时,表示全关
{
Asend_date1=(Areceive_date1&0x8f)|0x10;
Asend_date2=0x00;
Asend_date3=0x00;
Asend_date4=(Asend_date1^Asend_date2^Asend_date3)&0X7F;
Asend_date5=Asend_date1+Asend_date2+Asend_date3+Asend_date4;
Asend_date5=Asend_date5&0x7f;
}
if(date_lamp_num==0xaa) //aab时,表示全开
{
Asend_date1=Areceive_date1&0x8f;
Asend_date2=0x3f;
Asend_date3=0x3f;
Asend_date4=(Asend_date1^Asend_date2^Asend_date3)&0X7F;
Asend_date5=Asend_date1+Asend_date2+Asend_date3+Asend_date4;
Asend_date5=Asend_date5&0x7f;
}
if((date_lamp_num>>4)<0x09) //(date_lamp_num>>4)高4位为<11时,表示对应的灯开关 A表示开 B表示关
{
Asend_date1=Areceive_date1&0x8f;
shujufenxi(date_lamp_num>>4);
Asend_date4=(Asend_date1^Asend_date2^Asend_date3)&0X7F;
Asend_date5=Asend_date1+Asend_date2+Asend_date3+Asend_date4;
Asend_date5=Asend_date5&0x7f;
}
date_room=0x00;
}
else
{
Asend_date1=Areceive_date1&0x8f;
Asend_date2=Areceive_date2;
Asend_date3=Areceive_date3;
Asend_date4=(Asend_date1^Asend_date2^Asend_date3)&0X7F;
Asend_date5=Asend_date1+Asend_date2+Asend_date3+Asend_date4;
Asend_date5=Asend_date5&0x7f;
date_room=0x00;
}
/*----------------------------1号结束------------------------------------------------*/
/*----------------------------2号----------------------------------------------------*/
if(date_room==0x02)
{
if(date_lamp_num==0xbb) //bb时,表示全关
{
Bsend_date1=(Breceive_date1&0x8f)|0x10;
Bsend_date2=0x00;
Bsend_date3=0x00;
Bsend_date4=(Bsend_date1^Bsend_date2^Bsend_date3)&0X7F;
Bsend_date5=Bsend_date1+Bsend_date2+Bsend_date3+Bsend_date4;
Bsend_date5=Bsend_date5&0x7f;
}
if(date_lamp_num==0xaa) //aab时,表示全开
{
Bsend_date1=Breceive_date1&0x8f;
Bsend_date2=0x3f;
Bsend_date3=0x3f;
Bsend_date4=(Bsend_date1^Bsend_date2^Bsend_date3)&0X7F;
Bsend_date5=Bsend_date1+Bsend_date2+Bsend_date3+Bsend_date4;
Bsend_date5=Bsend_date5&0x7f;
}
if((date_lamp_num>>4)<0x09) //(date_lamp_num>>4)高4位为<11时,表示对应的灯开关 A表示开 B表示关
{
Bsend_date1=Breceive_date1&0x8f;
shujufenxi(date_lamp_num>>4);
Bsend_date4=(Bsend_date1^Bsend_date2^Bsend_date3)&0X7F;
Bsend_date5=Bsend_date1+Bsend_date2+Bsend_date3+Bsend_date4;
Bsend_date5=Bsend_date5&0x7f;
}
date_room=0x00;
}
else
{
Bsend_date1=Breceive_date1&0x8f;
Bsend_date2=Breceive_date2;
Bsend_date3=Breceive_date3;
Bsend_date4=(Bsend_date1^Bsend_date2^Bsend_date3)&0X7F;
Bsend_date5=Bsend_date1+Bsend_date2+Bsend_date3+Bsend_date4;
Bsend_date5=Bsend_date5&0x7f;
date_room=0x00;
}
/*---------------------------2号结束-------------------------------------------------*/
}
//----------------------------------------------------------
//串口1发送单字节
//---------------------------------------------------------
void send1(uchar date1)
{
TI=0;
P37=1;
ES=0;
// IE2=0X00; //ES2=0;
SBUF=date1;
while(!TI);
ES=1;
// IE2=0X01; //ES2=1;
P37=0;
}
/*----------------------------1号---------------------------------------------------*/
//串口1号
//-----------------------------------------------------------------------------------
void uart1() interrupt 4
{
if(RI==1)
{
RI=0;
if(((SBUF&0xf0)==0xf0)&&(Acc==0))
{
Acc=1;
Areceive_date1_back=SBUF;
}
else if(((SBUF&0xc0)==0x00)&&(Acc==1))
{
Acc=2;
Areceive_date2_back=SBUF;
}
else if(((SBUF&0xc0)==0x00)&&(Acc==2))
{
Acc=3;
Areceive_date3_back=SBUF;
}
else if(((SBUF&0X80)==0X00)&&(Acc==3))
{
Acc=4;
Areceive_date4=SBUF;
}
else if(((SBUF&0X80)==0X00)&&(Acc==4))
{
Acc=0;
Acalculate_all=Areceive_date1_back+Areceive_date2_back+Areceive_date3_back+Areceive_date4;
Acalculate_all=Acalculate_all&0x7f;
if((Areceive_date4==((Areceive_date1_back^Areceive_date2_back^Areceive_date3_back)&0X7F))&&(SBUF==Acalculate_all))
{
Areceive_date1=Areceive_date1_back;
Areceive_date2=Areceive_date2_back;
Areceive_date3=Areceive_date3_back;
if((Areceive_date1&0x0f)==Aaddress)
{
zhunbeishuju();
send1(Asend_date1);
send1(Asend_date2);
send1(Asend_date3);
send1(Asend_date4);
send1(Asend_date5);
}
}
}
else
{
Acc=0;
}
}
}
/*--------------------1号结束-------------------------------------------------------*/
//----------------------------------------------------------
//串口2发送单字节
//---------------------------------------------------------
void send2(uchar date2)
{
uchar temp;
S2CON=S2CON&0XFD; //S2TI=0;
P10=1;
// ES=0;
IE2=0X00;
S2BUF=date2;
do
{
temp=S2CON;
temp=temp&0x02;
}
while(temp==0);
// ES=1;
IE2=0X01;
P10=0;
}
/*--------------------2号-----------------------------------------------------------*/
//串口2号
//-----------------------------------------------------------------------------------
void uart2() interrupt 8
{
uchar k;
k=0;
k=S2CON;
k=k&0x01;
if(k==0x01) //S2RI==1
{
S2CON=S2CON&0XFE; //S2RI=0;
if(((S2BUF&0xf0)==0xf0)&&(Bcc==0))
{
Bcc=1;
Breceive_date1_back=S2BUF;
}
else if(((S2BUF&0xc0)==0x00)&&(Bcc==1))
{
Bcc=2;
Breceive_date2_back=S2BUF;
}
else if(((S2BUF&0xc0)==0x00)&&(Bcc==2))
{
Bcc=3;
Breceive_date3_back=S2BUF;
}
else if(((S2BUF&0X80)==0X00)&&(Bcc==3))
{
Bcc=4;
Breceive_date4=S2BUF;
}
else if(((S2BUF&0X80)==0X00)&&(Bcc==4))
{
Bcc=0;
Bcalculate_all=Breceive_date1_back+Breceive_date2_back+Breceive_date3_back+Breceive_date4;
Bcalculate_all=Bcalculate_all&0x7f;
if((Breceive_date4==((Breceive_date1_back^Breceive_date2_back^Breceive_date3_back)&0X7F))&&(S2BUF==Bcalculate_all))
{
Breceive_date1=Breceive_date1_back;
Breceive_date2=Breceive_date2_back;
Breceive_date3=Breceive_date3_back;
if((Breceive_date1&0x0f)==Baddress)
{
zhunbeishuju();
send2(Bsend_date1);
send2(Bsend_date2);
send2(Bsend_date3);
send2(Bsend_date4);
send2(Bsend_date5);
}
}
}
else
{
Bcc=0;
}
}
}
/*-----------------------2号结束--------------------------------------------------------*/
//----------------------------------------------------------------------------------------
//外部中断0
//----------------------------------------------------------------------------------------
void int0() interrupt 0
{
if(t==0) {date_room_back = P2; t=1; }
else if(t==1) {date_room_back_buma = P2; t=2; }
else if(t==2) {date_lamp_num_back = P2; t=3; }
else if(t==3) {date_lamp_num_back_buma = P2; t=4; }
if(t==4)
{
date_room_back_buma=~date_room_back_buma;
if(date_room_back_buma==date_room_back) date_room=date_room_back;
date_lamp_num_back_buma=~date_lamp_num_back_buma;
if(date_lamp_num_back_buma==date_lamp_num_back) date_lamp_num=date_lamp_num_back;
t=0;
}
}
//主程序#########################################################################################
void main()
{
init();
Aaddress=0X08; //地址
Baddress=0X08; //地址
TR1=1;
P37=0; //485控制线
P10=0;
//--------------------------------------------
send1(Aaddress);
send2(Baddress);
//-------------------------------------------
while(1)
{
WDT_CONTR=0X3C;
}
} |