#include<reg51.h>
#include<intrins.h>
sfr AUXR = 0x8E;
sfr AUXR1 = 0xA2;
sfr BRT = 0x9c;
sbit RE=P3^5;
//sbit JINYIN=P0^4;
//sbit KEY0=P1^0;
unsigned char MAIN_标志寄存器;
unsigned char MAIN_BUF;
unsigned int flag;
unsigned char MingLing[12]; //命令储存数组
unsigned char MLcount=0; //命令数组计数
unsigned char HYMingLing[12]; //命令储存数组
unsigned char f68;
#define INC(x,y) if(++x>=y) x=0
/*函数功能:延时1ms*/
void delay1ms()
{
unsigned char i,j;
for(i=0;i<10;i++)
for(j=0;j<33;j++)
;
}
/*函数功能:延时若干秒,入口参数:n*/
void delay (unsigned char n)
{
unsigned char i;
for (i=0;i<n;i++)
delay1ms();
}
void UartInit(void) //9600bps@11.0592MHz
{
// PCON&=0x7f; //波特率不倍速
SCON=0x50; //8位数据,可变波特率
BRT=0xFD; //设定独立波特率发生器重装值
AUXR&=0xfb; //独立波特率发生器时钟为Fosc/12,即12T
AUXR|=0x01; //串口1选择独立波特率发生器为波特率发生器
AUXR|=0x10; //启动独立波特率发生器
AUXR1|=0x80; //串口P1
ES=1;
EA=1;
}
void USART_TI(void) interrupt 4
{
unsigned char MLcount=0; //命令数组计数
/* if(TI==1)
{
TI=0;
f68=0;
}*/
if(RI==1)
{
RI=0;
MingLing[MLcount]=SBUF; //把命令保存在数组里
MLcount++;
INC(MLcount,12); //防止数组溢出
if(MingLing[0]==0x68)
{
if(MingLing[1]==0x77)
{
if((MingLing[2]==0x00)&&(MingLing[3]==0x00)&&(MingLing[4]==0xdf)&&(MingLing[5]==0x0d))
{
f68=1;
}
}
}
MLcount=0;
}
}
void jiexinmingling()
{
HYMingLing[0]=MingLing[1];
HYMingLing[1]=MingLing[0];
HYMingLing[2]=MingLing[2];
HYMingLing[3]=MingLing[3];
HYMingLing[4]=MingLing[5];
}
void main ()
{
unsigned int i;
delay(100);
UartInit();
delay(10);
while(1)
{
if(f68==1)
{
f68=0;
jiexinmingling();
RE=1;
for(i=0;i<5;i++)
{
SBUF=HYMingLing[i]; //查询地址回应
}
while(!TI);
TI=0;
delay(50);
}
else
{
RE=0;
}
}
} |