/*
   7521ODM3.C
   Demo program for 7188X as an Addressable RS-485 to RS-232 converter.
    using COM1 as RS-232
	  COM2 as RS-485
   convert non-addressable RS-232 to addressable RS-485
   On 7188x, RS-485 is "Self-Tuner" inside, so need not to change the direction of RS-485

   Setting COMMAND:
    (1) $AAM ----> Read Module name
    (2) $AAA[addr] ---->Read/Set Module address
    *(3) $AABn[baudrate] -->Read/Set RS-485(n=0) RS-232(n=1) baudrate
    (4) $AAC[delimiter] -->Read/Set Delimiter
    *(5) $AADn[data bit] -->Read/Set Data bit
    (6) $AAFn[CRLF]
    *(6) $AAPn[parity]	 -->Read/Set Parity
    *(7) $AAOn[parity]	 -->Read/Set stOp bit
    (8) (delimiter)AA[bypass data]
    (9) $AAD -----> Read Delimiter
    (10) $AA6(ID) -> Set ID(max length is 50)
    (11) $AA7 -----> Read ID
    (12) @AA6(ID) --> clear all 8 StopWatch
    (13) @AA7N --> read start_value of channel_N
    (14) $AA8N --> read stop_value of channel_N
 *:when change baudrate,data bit,parity,stop bit must short INIT* pin to GND

   EEPROM data use block 7
   addr 0,1: save value 7521(=0x1D61) as a flag
	2  : RS-485 baudrate(1-10)for(300-115200)
	3  : RS-485 parity(0,1,2)for(N,E,O)
	4  : RS-485 data bits (7,8)
	5  : RS-485 stop bit (always 1)
	6  : RS-232 baudrate(1-10)for(300-115200)
	7  : RS-232 parity(0,1,2)for(N,E,O)
	8  : RS-232 data bits (7,8)
	9  : RS-232 stop bit (always 1)
    ****
	21 : MODE (0,1,2)
    ****
	31 : delimiter
    ****
	40 : RS-485 CRLF mode
	41 : RS-232 CRLF mode
    ****
	50 : address
    ****
	100-149: ID

Release Note:
[11/02/1999]
  1. User must send command from COM2(RS-485), if the command is use to
     setting configuration, it will direct echo message from COM2.
     If the command is for pass data, the data will be send to COM1(RS-232),
     and any message come from COM1(RS-232) will be echo to COM2(RS-485) at
     any time.
  2. Send command $AAQ will stop the program(7521.exe),
*/

#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<conio.h>
#include"..\lib\7188x.h"

unsigned long far * timetick=(unsigned long far *)0x0040006CL;
int DEBUG_7521=0;

int quit=0;
long BAUD[11]={
9600L, /* default baudrate */
300L,
600L,
1200L,
2400L,
4800L,
9600L,
19200L,
38400L,
57600L,
115200L
};

int Baud485=0;
int Parity485=0;
int Data485=8;
int Stop485=1;

int Baud232=0;
int Parity232=0;
int Data232=8;
int Stop232=1;
unsigned char Delimiter=':';
int Address=1; /* default address is 01 */
int CurAddress;
int ModeCrLf485=0;
int ModeCrLf232=0;
int CurModeCrLf485=0;
int CurModeCrLf232=0;
/* int BypassMode=0; */  /* remove this mode, that is always BypassMode=0 */
char ID[50]="ICPDAS-7521-ODM3-1"; /* default ID */



int DI;
long start[8],stop[8];
char flag[8];




void Open7521(void)
{ int Is7521;
  unsigned char *dataptr=(unsigned char *)&Is7521;
  int addr;
  long baud1;
  int data1,p1;

  dataptr[0]=ReadEEP(7,0);
  dataptr[1]=ReadEEP(7,1);
  if(Is7521==7521){
    /* Not first time use it, it has been used. So read the configuration  */
     Baud485=ReadEEP(7,2);
     Parity485=ReadEEP(7,3);
     Data485=ReadEEP(7,4);
     Stop485=ReadEEP(7,5);
     Baud232=ReadEEP(7,6);
     Parity232=ReadEEP(7,7);
     Data232=ReadEEP(7,8);
     Stop232=ReadEEP(7,9);
/*     BypassMode=ReadEEP(7,21); */ /* not use now*/
     Delimiter=ReadEEP(7,31);
     Address=ReadEEP(7,50);
     ModeCrLf485=ReadEEP(7,40);
     ModeCrLf232=ReadEEP(7,41);
  }
  else{
    /* first time run 7521, set to default mode */
    EnableEEP();
    Is7521=7521;
    WriteEEP(7,0,dataptr[0]);
    WriteEEP(7,1,dataptr[1]);
    WriteEEP(7,2,6); /* 9600 */     /* RS-485 */
    WriteEEP(7,3,0); /* No parity */
    WriteEEP(7,4,8); /* 8 data bits */
    WriteEEP(7,5,1); /* 1 stop bit */
    WriteEEP(7,6,6); /* 9600 */     /* RS-232(1) */
    WriteEEP(7,7,0); /* No parity */
    WriteEEP(7,8,8); /* 8 data bits */
    WriteEEP(7,9,1); /* 1 stop bit */

    WriteEEP(7,21,0); /* default mode */

    WriteEEP(7,31,':'); /* delimiter=':' */

    WriteEEP(7,40,0); /* CRLF mode =CR */
    WriteEEP(7,41,0); /* CRLF mode =CR */

    WriteEEP(7,50,1); /* Address=1 */
    Address=1;
    ModeCrLf485=0;
    ModeCrLf232=0;
    dataptr=ID;
    addr=100;
    for(addr=100; addr<150 && *dataptr; addr++){
	WriteEEP(7,addr,*dataptr++);
    }
    WriteEEP(7,addr,0);
    ProtectEEP();
  }

  if(ReadInitPin()){  /* INIT* connect to GND, use 9600,N,8,1 */
/*     Print("Cur Addr=%02X\n\r",CurAddress); */
     CurAddress=0;
     CurModeCrLf485=0;		/* CR */
     CurModeCrLf232=0;		/* CR */
     baud1=BAUD[6];
     data1=8;
     p1=0;
     InstallCom2(BAUD[6],8,0);	/* RS-485 */
  }
  else {
     CurAddress=Address;
/*     Print("Cur-Addr=%02X\n\r",CurAddress);   */
     CurModeCrLf485=ModeCrLf485;
     CurModeCrLf232=ModeCrLf232;
     /* On 7521(7188x) Stop bit always 1 bit */
     baud1=BAUD[Baud232];
     data1=Data232;
     p1=Parity232;
     InstallCom2(BAUD[Baud485],Data485,Parity485);  /* RS-485 */
  }
/* test */
  if(DEBUG_7521){
     Print("Address=%02X Baud=%ld Data=%d Parity=%d Stop=%d CRmode=%d\n\r",
		CurAddress,BAUD[Baud485],Data485,Parity485,Stop485,ModeCrLf485);
     Print("232: Baud=%ld Data=%d Parity=%d Stop=%d CRmode=%d\n\r",
		BAUD[Baud232],Data232,Parity232,Stop232,ModeCrLf232);
     WaitTransmitOver1();
  }
  else InstallCom1(baud1,data1,p1);
  Init5DigitLed();
  Show5DigitLed(1,CurAddress>>4);
  Show5DigitLedWithDot(2,CurAddress&0x0F);
  Show5DigitLedWithDot(3,Baud485); /*  */


//Show5DigitLedWithDot(4,Baud232);

DI=inp(0);
Show5DigitLedWithDot(4,(DI>>4)&0x0F);
Show5DigitLedWithDot(5,DI&0x0F);
}

void Close7521(void)
{
  if(!DEBUG_7521) RestoreCom1();
  RestoreCom2();
  Show5DigitLed(1,7);
  Show5DigitLed(2,1);
  Show5DigitLed(3,8);
  Show5DigitLedWithDot(4,8);
  Show5DigitLedSeg(5,0x37);
}

#define WaitTransmitOver485  WaitTransmitOver2
#define ToCom485(a)  ToCom2(a)
#define ToCom485Str(str)  ToCom2Str(str)
#define IsCom485     IsCom2
#define ReadCom485   ReadCom2
void SendCmdToCom485Str(unsigned char *cmd)
{
    ToCom485Str(cmd);
    if(DEBUG_7521) Print(cmd); /**/
    switch(CurModeCrLf485){
      case 0:
	   ToCom485('\r');
	   if(DEBUG_7521) Print("Cr\n"); /**/
	   break;
      case 1:
	   ToCom485Str("\r\n");
	   if(DEBUG_7521) Print("CrLf\n"); /**/
	   break;
      case 2:
	   ToCom485('\n');
	   if(DEBUG_7521) Print("Lf\n");/**/
	   break;
      case 3:
	   ToCom485Str("\n\r");
	   if(DEBUG_7521) Print("LfCr\n"); /**/
	   break;
    }
}

#define IsCom232     IsCom1
#define ReadCom232   ReadCom1
#define ToCom232(a)  ToCom1(a)
#define ToCom232Str(str)  ToCom1Str(str)
unsigned char Cmd1[300];
unsigned char Echo1[80];
int Idx1=0;
void DoCom232(void) /* RS-232 */
{ int data;

  while(IsCom232()){
    data=ReadCom232();
    Cmd1[Idx1++]=data;
    if(data=='\r'){
       if(CurModeCrLf232==0){
	  Cmd1[Idx1-1]=0;
	  SendCmdToCom485Str(Cmd1);
	  Idx1=0;
       }
       else if(CurModeCrLf232==3){
	       if(Cmd1[Idx1-2]=='\n'){
		  Cmd1[Idx1-2]=0;
		  SendCmdToCom485Str(Cmd1);
		  Idx1=0;
	       }
       }
    }
    else if(data=='\n'){
	    if(CurModeCrLf232==2){
	       Cmd1[Idx1-1]=0;
	       SendCmdToCom485Str(Cmd1);
	       Idx1=0;
	    }
	    else if(CurModeCrLf232==1){
		    if(Cmd1[Idx1-2]=='\r'){
		       Cmd1[Idx1-2]=0;
		       SendCmdToCom485Str(Cmd1);
		       Idx1=0;
		    }
	    }
    }
  }
}

unsigned char Cmd2[300];
unsigned char Echo2[80];
int Idx2=0;

void EchoErrorCmd(void)
{
  sprintf(Echo2,"?%02X",CurAddress);
  SendCmdToCom485Str(Echo2);
}

void EchoOkCmd(void)
{
  sprintf(Echo2,"!%02X",CurAddress);
  SendCmdToCom485Str(Echo2);
}
void ProcessCom485(void)
{ int addr;
  int InitPin=ReadInitPin();  /* 1: Init* connect to GND */
  unsigned long time;

 if(Idx2>=4){
  addr=(ascii_to_hex(Cmd2[1])<<4)+ascii_to_hex(Cmd2[2]);
  if(addr==CurAddress){

     if(Cmd2[0]=='$'){
	switch(Cmd2[3]){
	  case 'M': /* read module name */
	       if(Idx2==4){
		 sprintf(Echo2,"!%02X7521-ODM3-1",CurAddress);
		 SendCmdToCom485Str(Echo2);
	       }
	       else {
	       }
	       break;
	  case 'A': /* Set new address/Read address setting */
	       if(Idx2==4){
		 sprintf(Echo2,"!%02X",Address);
		 SendCmdToCom485Str(Echo2);
	       }
	       else if(Idx2==6){
		       if(isxdigit(Cmd2[4]) && isxdigit(Cmd2[5])){
			  Address=(ascii_to_hex(Cmd2[4])<<4)+ascii_to_hex(Cmd2[5]);
			  EnableEEP();
			  WriteEEP(7,50,Address);
			  ProtectEEP();
			  CurAddress=Address;
			  EchoOkCmd();
			  Show5DigitLed(1,CurAddress>>4);
			  Show5DigitLedWithDot(2,CurAddress&0x0F);
		       }
		       else {
			  EchoErrorCmd();
		       }
	       }
	       break;
	  case 'B': /* Read/Set new baudrate */
	       if(Idx2==5){
		  long data;
		  if(Cmd2[4]=='0') data=BAUD[Baud485];
		  else if(Cmd2[4]=='1') data=BAUD[Baud232];
		  else {
		     EchoErrorCmd();
		     break;
		  }
		  sprintf(Echo2,"!%02X%ld",Address,data);
		  SendCmdToCom485Str(Echo2);
	       }
	       else if(Idx2>5){
		       long data;
		       int i;

		       if(!InitPin){
			  EchoErrorCmd();
			  break;
		       }
		       data=atol(Cmd2+5);
		       for(i=1;i<=10;i++){
			   if(data==BAUD[i]) goto SetBaud;
		       }
		       EchoErrorCmd();
		       break;
     SetBaud:
		       if(Cmd2[4]=='0'){
			  Baud485=i;
			  EnableEEP();
			  WriteEEP(7,2,Baud485);
			  ProtectEEP();
		       }
		       else if(Cmd2[4]=='1'){
			  Baud232=i;
			  EnableEEP();
			  WriteEEP(7,6,Baud232);
			  ProtectEEP();
		       }
		       else {
			  EchoErrorCmd();
			  break;
		       }
		       EchoOkCmd();
	       }
	       break;
	  case 'C': /* Set new delimiter */
	       if(Idx2==4){
		 /* $AAC: Read Delimiter */
		 sprintf(Echo2,"!%02X%c",CurAddress,Delimiter);
		 SendCmdToCom485Str(Echo2);
	       }
	       else if(Idx2==5){
		       if(Cmd2[4]!='$' && Cmd2[4]!='\r' && Cmd2[4]!='\n'){
			  Delimiter=Cmd2[4];
			  EnableEEP();
			  WriteEEP(7,31,Delimiter);
			  ProtectEEP();
			  EchoOkCmd();
		       }
		       else {
			  EchoErrorCmd();
		       }
	       }
	       break;
	  case 'D': /* Set new data bits */
	       if(Idx2==4){
		  /* $AAD : Read Delimiter */
		 sprintf(Echo2,"!%02X%c",CurAddress,Delimiter);
		 SendCmdToCom485Str(Echo2);
	       }
	       else if(Idx2==5){
		  int data;
		  if(Cmd2[4]=='0') data=Data485;
		  else if(Cmd2[4]=='1') data=Data232;
		  else {
		     EchoErrorCmd();
		     break;
		  }
		  sprintf(Echo2,"!%02X%d",CurAddress,data);
		  SendCmdToCom485Str(Echo2);
	       }
	       else if(Idx2>5){
		       int data;

		       if(!InitPin){
			  EchoErrorCmd();
			  break;
		       }
		       if(Cmd2[5]=='7') data=7;
		       else if(Cmd2[5]=='8') data=8;
		       else {
			  EchoErrorCmd();
			  break;
		       }
		       if(Cmd2[4]=='0'){
			  Data485=data;
			  EnableEEP();
			  WriteEEP(7,4,Data485);
			  ProtectEEP();
		       }
		       else if(Cmd2[4]=='1'){
			  Data232=data;
			  EnableEEP();
			  WriteEEP(7,8,Data232);
			  ProtectEEP();
		       }
		       else {
			  EchoErrorCmd();
			  break;
		       }
		       EchoOkCmd();
	       }
	       break;
	  case 'F': /* Set new CRLF mode */
	       if(Idx2==5){
		  int data;
		  if(Cmd2[4]=='0') data=ModeCrLf485;
		  else if(Cmd2[4]=='1') data=ModeCrLf232;
		  else {
		     EchoErrorCmd();
		     break;
		  }
		  sprintf(Echo2,"!%02X%d",CurAddress,data);
		  SendCmdToCom485Str(Echo2);
	       }
	       else if(Idx2>5){
		       int data;

		       if(!InitPin){
			  EchoErrorCmd();
			  break;
		       }
		       if(Cmd2[5]>='0' && Cmd2[5]<='3') data=Cmd2[5]-'0';
		       else {
			  EchoErrorCmd();
			  break;
		       }
		       if(Cmd2[4]=='0'){
			  ModeCrLf485=data;
			  EnableEEP();
			  WriteEEP(7,40,ModeCrLf485);
			  ProtectEEP();
		       }
		       else if(Cmd2[4]=='1'){
			  ModeCrLf232=data;
			  EnableEEP();
			  WriteEEP(7,41,ModeCrLf232);
			  ProtectEEP();
		       }
		       else {
			  EchoErrorCmd();
			  break;
		       }
		       EchoOkCmd();
	       }
	       break;
	  case 'P': /* Set new parity mode */
	       if(Idx2==5){
		  int data;
		  if(Cmd2[4]=='0') data=Parity485;
		  else if(Cmd2[4]=='1') data=Parity232;
		  else {
		     EchoErrorCmd();
		     break;
		  }
		  sprintf(Echo2,"!%02X%d",CurAddress,data);
		  SendCmdToCom485Str(Echo2);
	       }
	       else if(Idx2>5){
		       int data;

		       if(!InitPin){
			  EchoErrorCmd();
			  break;
		       }
		       if(Cmd2[5]=='0') data=0;
		       else if(Cmd2[5]=='1') data=1;
		       else if(Cmd2[5]=='2') data=2;
		       else {
			  EchoErrorCmd();
			  break;
		       }
		       if(Cmd2[4]=='0'){
			  Parity485=data;
			  EnableEEP();
			  WriteEEP(7,3,Parity485);
			  ProtectEEP();
		       }
		       else if(Cmd2[4]=='1'){
			  Parity232=data;
			  EnableEEP();
			  WriteEEP(7,7,Parity232);
			  ProtectEEP();
		       }
		       else {
			  EchoErrorCmd();
			  break;
		       }
		       EchoOkCmd();
	       }
	       break;
	  case 'O': /* Set new Stop bit */
/*	       for 7521 always stopbit=1
	       if(Idx2==5){
		  int data;
		  if(Cmd2[4]=='0') data=Stop485;
		  else if(Cmd2[4]=='1') data=Stop232;
		  else break;
		 sprintf(Echo2,"!%02X%d",Address,data);
		 SendCmdToCom485Str(Echo2);
	       }
	       else if(Idx2>5){
		       int data;

		       if(!InitPin){
			  sprintf(Echo2,"?%02X",Address);
			  SendCmdToCom485Str(Echo2);
			  break;
		       }
		       if(Cmd2[5]=='1') data=1;
		       else if(Cmd2[5]=='2') data=2;
		       else break;
		       if(Cmd2[4]=='0'){
			  Stop485=data;
			  EnableEEP();
			  WriteEEP(7,5,Stop485);
			  ProtectEEP();
		       }
		       else if(Cmd2[4]=='1'){
			  Stop232=data;
			  EnableEEP();
			  WriteEEP(7,9,Stop232);
			  ProtectEEP();
		       }
		       else break;
		       sprintf(Echo2,"!%02X",Address);
		       SendCmdToCom485Str(Echo2);
	       }
*/	       break;
	}
     }
     else if(Cmd2[0]=='@')
     {
       switch(Cmd2[3])
	  {
/* --------------------------------------------------------------------- */

	  case '6':  /* @AA6(ID) --> clear all 8 StopWatch */
	       if (Idx2==4)
		  {
		  int i;

		  init_stop_watch();

		  _asm cli
		  *timetick=0;
		  _asm sti

		  sprintf(Echo2,"!%02X",Address);
		  SendCmdToCom485Str(Echo2);
		  }
	       else
		  {
		  EchoErrorCmd();
		  }
	       break;

/* --------------------------------------------------------------------- */

	  case '7': /* @AA7N --> read start_value of channel_N */
	       if (Idx2==5) /* command len = 5 */
		  {
		  unsigned long ll;
		  if ((Cmd2[4]<='7') && (Cmd2[4]>='0'))
		     {
		     ll=start[Cmd2[4]-'0'];
		     sprintf(Echo2,"!%010ld",ll);
		     SendCmdToCom485Str(Echo2);
		     }
		  else if (Cmd2[4]=='8')
		     {
		     _asm cli
		     ll=(*timetick);
		     _asm sti

		     sprintf(Echo2,"!%010ld",ll);
		     SendCmdToCom485Str(Echo2);
		     }
		  else EchoErrorCmd();	/* valid for 0 to 7 */
		  }
	       else EchoErrorCmd();	/* len must = 5     */
	       break;
	  case '8': /* $AA8N --> read stop_value of channel_N */
	       if (Idx2==5) /* command len = 5 */
		  {
		  unsigned long ll;
		  if ((Cmd2[4]<='7') && (Cmd2[4]>='0'))
		     {
		     ll=stop[Cmd2[4]-'0'];
		     sprintf(Echo2,"!%010ld",ll);
		     SendCmdToCom485Str(Echo2);
		     }
		  else if (Cmd2[4]=='8')
		     {
		     _asm cli
		     ll=(*timetick);
		     _asm sti

		     sprintf(Echo2,"!%010ld",ll);
		     SendCmdToCom485Str(Echo2);
		     }
		  else EchoErrorCmd();	/* valid for 0 to 7 */
		  }
	       else EchoErrorCmd();	/* len must = 5     */
	       break;
	  default: break;
	  }
     }
     else if(Cmd2[0]==Delimiter){
	     ToCom1Bufn(Cmd2+3,Idx2-3);
	     switch(CurModeCrLf232){
	       case 0:
		    ToCom1Str("\r");
		    break;
	       case 1:
		    ToCom1Str("\r\n");
		    break;
	       case 2:
		    ToCom1Str("\n");
		    break;
	       case 3:
		    ToCom1Str("\n\r");
		    break;
	     }
     }
  }
 }
 Idx2=0;
}

int HasDataToSend=0;
int DestAddr=0;
int DataLength=0;
void DoCom485(void) /* RS-485 */
{ int data;

  while(IsCom485()){
    data=ReadCom485();
    if(DEBUG_7521)    Putch(data); /**/

DoAsNormalMode:
       Cmd2[Idx2]=data;
       if(data=='\r'){
	  switch(CurModeCrLf485){
	    case 0: /* CR */
		 Cmd2[Idx2]=0;
		 if(DEBUG_7521) Print("[CR]\n");
		 ProcessCom485();
		 return;
	    case 1: /* CR+LF */
	    case 2: /* LF */
		 Idx2++;
		 break;
	    case 3: /* LF+CR */
		 if(Cmd2[Idx2-1]=='\n'){
		    Idx2--;
		    Cmd2[Idx2]=0;
		    if(DEBUG_7521) Print("[LF+CR]\n");
		    ProcessCom485();
		    return;
		 }
		 break;
	  }
       }
       else if(data=='\n'){
	  switch(CurModeCrLf485){
	    case 0: /* CR */
		 Idx2++;
		 break;
	    case 1: /* CR+LF */
		 if(Cmd2[Idx2-1]=='\r'){
		    Idx2--;
		    Cmd2[Idx2]=0;
		    if(DEBUG_7521) Print("[CR+LF]\n");
		    ProcessCom485();
		    return;
		 }
		 break;
	    case 2: /* LF */
		 Cmd2[Idx2]=0;
		    if(DEBUG_7521) Print("[LF]\n");
		 ProcessCom485();
		 return;
	    case 3: /* LF+CR */
		 Idx2++;
		 break;
	  }
       }
       else {
	  Idx2++;
       }
  }
}

/* ------------------------------------------------------------- */

void ShowDI(void);
void main(int argc,char *argv[])
{ int i;

/*  Print("InitPin=%d\n\r",ReadInitPin()); */
  for(i=1;i<argc;i++) {
      if(argv[i][0]=='/'){
	 switch(argv[i][1]){
	   case 'd': case 'D':
		DEBUG_7521=1;
		break;
	   case 'b': case 'B':
		break;

	 }
      }
  }
  if(DEBUG_7521) Print("Start 7521!\n\r");
  Open7521();
  EnableWDT();

/*  TimerOpen(); */
  init_stop_watch();

  while(!quit){
    RefreshWDT();
    DoCom232();
    DoCom485();

    ProcessStopWatch();
  }

  DisableWDT();
/*  for (i=0; i<8; i++) StopWatchStop(i);*/
/*  TimerClose(); */
  Close7521();
}

init_stop_watch()
{
int i;

TimerResetValue();
for (i=0; i<8; i++) start[i]=stop[i]=0;
for (i=0; i<8; i++) flag[i]=1;
}

void ShowDI(void)
{
DI=inp(0);
Show5DigitLedWithDot(4,(DI>>4)&0x0F);
Show5DigitLedWithDot(5,DI&0x0F);
}

ProcessStopWatch()
{
unsigned long ll;
char cc;
int i;
unsigned long time;

_asm cli
  time=*timetick;
_asm sti

ShowDI();  /* now DI=input value */
cc=1;
for (i=0; i<8; i++)
{
if (flag[i]==1)
   {
   if (!(DI&cc))
      {
      start[i]=time;
      flag[i]=2;
      }
   }
else if (flag[i]==2)
   {

   if ((time)<(start[i]+30)) break;

   if ( (DI&cc))
      {
      stop[i]=(time);
      flag[i]=0;
      }
   }
cc=cc<<1;
}
}

