//移植于AS89S52                        实现的三遥功能
#include
#include   
#include   
void TaskStartyya(void *yydata) reentrant;
void TaskStartyyg(void *yydata) reentrant;
void calccrc(unsigned char *bf,int n);
unsigned char ajcj();
void send1();
void send2();
void send3();
extern void DS18B20PRO(); //外部函数声明
extern unsigned char wd[3]; //外部变量声明
OS_STK TaskStartStkyya[MaxStkSize];
OS_STK TaskStartStkyyg[MaxStkSize];
sbit KEY = P3^2;
sbit JDQ = P1^4;
//OS_EVENT *sem1;
//OS_EVENT *Mbox1;
void *CommMsg[8];
OS_EVENT *CommQ;
unsigned char buf[29];
unsigned char buf1[2];
unsigned char c[2]={0x03,0xFD};
int nn=0;
 void int_com()
{
 TMOD=0X22;
 TH1=0xE8;
 TL1=0xE8;
 TR1=1;//定时
 PCON=0X00;
 SCON=0X50;
 SM2=1;
 ES=1;  //串口中断开
}
void main(void)
{
    OSInit();
 int_com();
    InitTimer0();
// sem1 = OSSemCreate(0);
//  Mbox1 = OSMboxCreate((void *)0);
 CommQ = OSQCreate(&CommMsg[0],8);
    OSTaskCreate(TaskStartyya, (void *)0, &TaskStartStkyya[0],2);
 OSTaskCreate(TaskStartyyg, (void *)0, &TaskStartStkyyg[0],3);
    OSStart();
}
 //OSSemPost(sem1);
 //OSSemPend(sem1, 0, &err);
//*************************************  中断接受  ******************************************
int serial2() reentrant
{
 unsigned char  *num;
 static int y=1; //串口发送计数
 
 if(RI)
    {   RI=0;
  num=malloc(1); 
  *num=SBUF;
  OSQPost(CommQ,num);       //(unsigned char*)
   }
 if(TI)
 { if(y>=nn)
   {y=1;
    TI=0;
   }
  if(TI)
   {SBUF=buf[y++];
    TI=0;
   }
   }
}
//*************************************  中转函数  ******************************************
void TaskStartyya(void *yydata) reentrant
 unsigned char *msg;
 int q=0;
 unsigned char err;
    yydata=yydata;
 *(unsigned char xdata *)0x8000=0xff;
    *(unsigned char xdata *)0x6000=0xff;
 *(unsigned char xdata *)0x4000=0xff;
 while(1)
    { 
  msg = OSQPend(CommQ, 0, &err);
  buf[q++]=*msg;//(unsigned char)
     free(msg);
 if(q==8)
      {
    q=0;  
  calccrc(buf,8);
        if((buf1[0]==buf[6])&&(buf1[1]==buf[7])&&(buf[0]==c[0]))//教研正确后发送程序    
    {   
    switch(buf[1])
   {
   case 0x02: send1();break;//遥信发
   case 0x05:
   case 0x01: send2();break;//遥控发
   case 0x03: send3();break;//遥测
   default: break;
   } 
    }
      } 
    }
}
//*************************************  校  验  ******************************************
void calccrc(unsigned char *bf,int n)

 unsigned int crc=0xFFFF;
 int i,j;
 for(i=0;i  { 
  crc=crc ^ bf[i];
  for(j=0;j<8;j++)
   {
   char TT;
   TT=crc&1;
   crc=crc>>1;
   if (TT==1)
   crc=crc^0xA001;
   }
   }
 buf1[0]=crc&0xFF;
 buf1[1]=(crc>>8)&0xFF;
}
//***************************************  按键采集  *************************************
unsigned char ajcj()
{
 unsigned char a;
 a=*(unsigned char xdata *)0x8000;
 return a;
}
//*************************************   遥 信 发 1  **************************************
void send1()
{
 buf[2]=0x01;
 buf[3]=ajcj();
 calccrc(buf,6);
 nn=6;
 buf[4]=buf1[0];
 buf[5]=buf1[1];
 SBUF=buf[0];
}
//*************************************   遥 信 发 2  **************************************
void send2()
{
 static unsigned char deng=0xff;
 if(buf[1]==0x05)
 {
    if(buf[3]==0x00)
       if(buf[4]==0xff){deng=deng&0xef;*(unsigned char xdata*)0x4000=deng;}
       else {deng=deng|0xf0;*(unsigned char xdata*)0x4000=deng;}
    else if(buf[3]==0x01)
    if(buf[4]==0xff){deng=deng&0xf7;*(unsigned char xdata*)0x4000=deng;}
    else {deng=deng|0x0f;*(unsigned char xdata*)0x4000=deng;};
   nn=8;
      SBUF=buf[0];
  }
 if(buf[1]==0x01)  
  {
  buf[2]=0x01;
  *(unsigned char xdata*)0x4000=deng;
  switch(deng)
  {
  case 0xff: buf[3]=0x00;break;
  case 0xef: buf[3]=0x01;break;
  case 0xf7: buf[3]=0x02;break;
  case 0xe7: buf[3]=0x03;break;
  default: break;
  }
  calccrc(buf,6);
  buf[4]=buf1[0];
  buf[5]=buf1[1];
  nn=6;
  SBUF=buf[0];
  }
}
//*****************************************   通信发 3   **************************************
void send3()
{  
 int j;
 DS18B20PRO();
 buf[2]=0x18;
 buf[3]=wd[1];
 buf[4]=wd[0];
    for(j=5;j<27;j++)
      buf[j]=0x00;
 calccrc(buf,29);
 buf[27]=buf1[0];
 buf[28]=buf1[1];
 nn=29;
 SBUF=buf[0];
}
//*************************************   收发显示  **************************************
void TaskStartyyg(void *yydata) reentrant
{   int s=0;
 //unsigned char shuma[11]={0xc0,0xf9,0xa4,0xb0, 0x99,0x92,0x82,0xf8,0x80,0x90, 0xff};
 unsigned char dz[8]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
 yydata=yydata;
 while(1)
 {
 *(unsigned char xdata *)0x2000=dz[s];
    *(unsigned char xdata *)0x6000=buf[s++];
    if(s==8)s=0;
 OSTimeDly(1);
 }
}