/*
 SS: pin 10
 MOSI: pin 11
 MISO: pin 12
 SCK: pin 13
*/
#include <math.h>
const int SLS = 10;
const int SDI = 11;
const int SDO = 12;
const int SCLK = 13;
const int LEDCTRL = 8;
const int LEDCTRL2 = 9;
void send();
void setup() 
{
  pinMode(SLS, OUTPUT);
  pinMode(SDI, OUTPUT);
  pinMode(SCLK, OUTPUT);
  pinMode(SDO, INPUT);
  pinMode(SDO, INPUT_PULLUP);
  pinMode(LEDCTRL, OUTPUT);
  pinMode(LEDCTRL2, OUTPUT);

  digitalWrite(SLS, HIGH);
  digitalWrite(SLS, HIGH);
  digitalWrite(SCLK, LOW);
  digitalWrite(LEDCTRL, LOW);
  digitalWrite(LEDCTRL2, LOW);
  delay(5);
  Serial.begin(9600);
}
  //vlozit zadost pro cteni do pole
                           
 bool poleinitboost[6][16] ={{0,1,1,0,0,0,1,0,0,0,0,0,0,0,0,0},     //18h
                             {0,1,1,0,0,0,1,0,0,0,0,0,0,0,0,0},     //18h
                             {1,0,0,0,1,1,0,1,1,1,1,1,1,0,0,0},     //01h BOOST_OTA_GAIN[1:0] | BOOST_FREQ[4:0] | BOOST_SLPCTRL[1:0] | BOOST_SRC
                             {1,0,0,1,1,0,1,1,1,0,1,1,1,1,0,1},     //03h VDRIVE_BST_EN | BOOST_VLIMTH[1:0] | BOOST_VSETPOINT[6:0]                              puvodne: 1,0,0,1,1,1,1,1,1,1,0,0,0,0,0,1
                             {1,1,0,0,0,0,1,1,1,1,0,0,1,1,0,0},     //08h VDRIVE_SETPOINT[3:0] | BOOST_MULTI_MD[1:0] | DIM_SRC[1:0] | PWM_FREQ[1:0]
                             {1,0,0,1,0,1,0,1,0,0,1,0,1,0,0,1}};    //02h BOOST_TOFF_MIN[1:0] | VBOOST_VGATE_THR | BOOST_SCRINV | BOOST_EN | BOOST_OV_REACT[1:0] | BOOST_OV_SD [2:0]
                             
  bool poleinitbuck2[4][16]={{1,0,1,0,0,1,1,1,0,1,0,1,1,0,1,0},     //04h BUCK1_OFF_CMP_EN | BUCK2_OFF_CMP_EN | BUCK1_VTHR[7:0]
                            {1,0,1,0,1,0,0,0,0,1,0,1,1,0,1,0},     //05h
                            {1,0,1,1,0,1,0,0,0,0,0,0,0,0,0,1},     //06h BUCK1_TOFF_VLED[3:0] | BUCK2_TOFF_VLED[3:0] | BUCK1_EN | BUCK2_EN            predtim_bylo: 1,0,1,1,0,1,1,1,1,1,0,0,0,0,0,1
                            {1,1,0,0,0,0,1,1,1,1,0,0,1,1,0,1}};    //08h VDRIVE_SETPOINT[3:0] | BOOST_MULTI_MD[1:0] | DIM_SRC[1:0] | PWM_FREQ[1:0]

  bool poleinitbuck1[3][16]={{1,0,1,0,0,1,1,1,0,1,0,1,1,0,1,0},     //04h BUCK1_OFF_CMP_EN | BUCK2_OFF_CMP_EN | BUCK1_VTHR[7:0]
                             {1,0,1,1,0,1,0,0,0,0,0,0,0,0,1,0},     //06h BUCK1_TOFF_VLED[3:0] | BUCK2_TOFF_VLED[3:0] | BUCK1_EN | BUCK2_EN           predtim_bylo: 1,0,1,1,0,1,1,1,1,1,0,0,0,0,1,0
                             {1,1,0,0,0,0,1,1,1,1,0,0,1,1,0,1}};    //08h VDRIVE_SETPOINT[3:0] | BOOST_MULTI_MD[1:0] | DIM_SRC[1:0] | PWM_FREQ[1:0]
                             
                             
  bool polediagnostika[2][16]={{0,1,0,1,1,1,1,0,0,0,0,0,0,0,0,0},    //17h
                              {0,1,1,0,0,0,1,0,0,0,0,0,0,0,0,0}};   //18h
                             
  bool poleoff[16]={1,0,0,1,0,1,0,0,0,0,0,0,1,0,0,1};           //02h
                       

  bool polemereni[7][16]={{0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0},
                         {0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0},
                         {0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0},         //10 VLED1  0100000000000000
                         {0,1,0,0,0,1,1,0,0,0,0,0,0,0,0,0},         //11 VLED2  0100011000000000
                         {0,1,0,0,1,0,1,0,0,0,0,0,0,0,0,0},         //12 VTEMP
                         {0,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0},         //13 VBOOST
                         {0,1,0,1,0,0,1,0,0,0,0,0,0,0,0,0}};        //14 VBAT
                                                  
  bool pole[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; //pole pro vyslac data
  bool prijem[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; //prijata data
  bool diagnostika17[8] = {0,0,0,0,0,0,0,0};
  bool diagnostika18[8] = {0,0,0,0,0,0,0,0};
  float napeti=0;
  bool mereni[9] = {0,0,0,0,0,0,0,0,0,};
  char cteni[2];
  int i=0;
  short pom=0;
  short pom2=0;
  short pom3=0;
  short pom4=0;
  short pom6=0;
  int ctenizapis=0;
  int adress1=0;
  int adress2=0;
  int adresa=0;
  int hodnota[10];
  int prv=0;
  short pom5=0;
  int mocnina[8] = {1,2,4,8,16,32,64,128};
  int vboost=0;
  short vboostpole[2] = {0,0};
  bool ctenibuck[16] = {0,0,0,1,1,0,1,0,0,0,0,0,0,0,0,0};
  bool cteniboost[16] ={0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0};
  bool currentbuck1[16] = {0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0};
  bool currentbuck2[16] = {0,0,0,1,1,0,1,0,0,0,0,0,0,0,0,0};
  short currentpole[3]={0,0,0};
  short current=0;
  double spi=0;
  double VTHR=0;

  //**********************************Diagnostika******************************************************************************
 void diagnostika()
 {
  for(int m=0;m<2;m++) //cteni z 2 registru
    {
      for(int o=0;o<2;o++) //pro jistotu cist 2x
      {
        for(int n=0;n<16;n++)       
        {
          pole[n]=polediagnostika[m][n];
        }
        send();
        delay(10);
      }
      for(int a=0;a<8;a++)
      {
        if(m==0)
        {           
          diagnostika17[a]=prijem[a+8];
        }
        if(m==1)
        {
          diagnostika18[a]=prijem[a+8]; 
        }
      }
    }
    Serial.print("BUCKACTIVE1:  ");
    Serial.println(diagnostika17[0]);
    Serial.print("BUCKACTIVE2:  ");
    Serial.println(diagnostika17[1]);
    Serial.print("OPENLED1:     ");
    Serial.println(diagnostika17[2]);
    Serial.print("SHORTLED1:    ");
    Serial.println(diagnostika17[3]);
    Serial.print("OCLED1:       ");
    Serial.println(diagnostika17[4]);
    Serial.print("OPENLED2:     ");
    Serial.println(diagnostika17[5]);
    Serial.print("SHORTLED2:    ");
    Serial.println(diagnostika17[6]);
    Serial.print("OCLED2:       ");
    Serial.println(diagnostika17[7]);
    Serial.println("");
    Serial.print("BOOST_STATUS: ");
    Serial.println(diagnostika18[0]);
    Serial.print("BOOST_OV:     ");
    Serial.println(diagnostika18[1]);
    Serial.print("TEST_FAIL:    ");
    Serial.println(diagnostika18[2]);
    Serial.print("LEDCTRL1VAL:  ");
    Serial.println(diagnostika18[3]);
    Serial.print("LEDCTRL2VAL:  ");
    Serial.println(diagnostika18[4]);
    Serial.print("SPIERR:       ");
    Serial.println(diagnostika18[5]);
    Serial.print("TSD:          ");
    Serial.println(diagnostika18[6]);
    Serial.print("TW:           "); 
    Serial.println(diagnostika18[7]);
    
  Serial.println("");
 }
 //**********************************Inicializace boostu******************************************************************************
 void initboost()
 {
  int pom=0;
  for(int a=0;a<16;a++)
  {
    pole[a]=cteniboost[a];  
  } 
  send();
  pom=prijem[10];
  if(pom==1)
  {
    for(int n=0;n<16;n++)
    {
      pole[n]=poleoff[n];
    }
    send();
  }
  if(pom==0)
  {
    for(int m=0;m<6;m++)
    { 
      for(int n=0;n<16;n++)
      {
        pole[n]=poleinitboost[m][n];
      }
      send();
    }
    Serial.println("");
  }
  
 }


 //**********************************ON/OFF buck1*************************************************************************************
 void onoffbuck1()
 {
  for(int a=0;a<16;a++)
  {
    pole[a]=poleinitbuck1[0][a];  
  } 
  send();
  for(int a=0;a<16;a++)
  {
    pole[a]=ctenibuck[a];  
  } 
  send();
  for(int n=0;n<16;n++)
  {
    pole[n]=poleinitbuck1[1][n];
  }
  if((prijem[14]==0)and(prijem[15]==0))
  {
    pole[14]=1;
    pole[15]=0;
    digitalWrite(LEDCTRL,HIGH);
  }
  if((prijem[14]==0)and(prijem[15]==1))
  {
    pole[14]=1;
    pole[15]=1;
    digitalWrite(LEDCTRL,HIGH);
  }
  if((prijem[14]==1)and(prijem[15]==0))
  {
    pole[14]=0;
    pole[15]=0;
    digitalWrite(LEDCTRL,LOW);
  }
  if((prijem[14]==1)and(prijem[15]==1))
  {
    pole[14]=0;
    pole[15]=1;
    digitalWrite(LEDCTRL,LOW);
  }
  pole[5]= not(pole[0] xor pole[1] xor pole[2] xor pole[3] xor pole[4] xor pole[6] xor pole[7] xor pole[8] xor pole[9] xor pole[10] xor pole[11] xor pole[12] xor pole[13] xor pole[14] xor pole[15]);
  send();
  for(int a=0;a<16;a++)
  {
    pole[a]=poleinitbuck1[2][a];  
  } 
  send();   
}


//**********************************ON/OFF buck2*************************************************************************************
 void onoffbuck2()
 {
  for(int a=0;a<16;a++)
  {
    pole[a]=poleinitbuck2[0][a];  
  } 
  send();
  for(int a=0;a<16;a++)
  {
    pole[a]=poleinitbuck2[1][a];  
  } 
  send();
  for(int a=0;a<16;a++)
  {
    pole[a]=ctenibuck[a];  
  } 
  send();
  for(int n=0;n<16;n++)
  {
    pole[n]=poleinitbuck2[2][n];
  }
  if((prijem[14]==0)and(prijem[15]==0))
  {
    pole[14]=0;
    pole[15]=1;
    digitalWrite(LEDCTRL2,HIGH);
  }
  if((prijem[14]==0)and(prijem[15]==1))
  {
    pole[14]=0;
    pole[15]=0;
    digitalWrite(LEDCTRL2,LOW);
  }
  if((prijem[14]==1)and(prijem[15]==0))
  {
    pole[14]=1;
    pole[15]=1;
    digitalWrite(LEDCTRL2,HIGH);
  }
  if((prijem[14]==1)and(prijem[15]==1))
  {
    pole[14]=1;
    pole[15]=0;
    digitalWrite(LEDCTRL2,LOW);
  }
  pole[5]= not(pole[0] xor pole[1] xor pole[2] xor pole[3] xor pole[4] xor pole[6] xor pole[7] xor pole[8] xor pole[9] xor pole[10] xor pole[11] xor pole[12] xor pole[13] xor pole[14] xor pole[15]);
  send();
  for(int a=0;a<16;a++)
  {
    pole[a]=poleinitbuck2[3][a];  
  } 
  send();   
}

 

//**********************************Nastaveni napeti boostu***************************************************************************
 void napetibst()
 {
 pom3==0;
   Serial.print("Zadejte hodnotu napeti: ");
   while(pom3==0)
     {  
       while(Serial.available()>0)
       {
          for(int a=0;a<2;a++)
          {       
            vboostpole[a] = Serial.read()-48;
            delay(10);
          }
          vboost=(vboostpole[0]*10)+(vboostpole[1]);
          if(vboost>0)
          {
            pom3=1;
          }
          Serial.println(vboost);
          if((vboost<15)or(vboost>64))
          {
            Serial.println("ERROR");  
            vboost=15;
          }
          vboost=vboost*100;
          vboost=vboost/39;
          vboost=vboost-38;
          send();
          for(int a=0;a<16;a++)
          {
            pole[a]=prijem[a];
          }
          for(int a=9;a<16;a++)
          {
            pole[a]=0;
          }
          if(vboost>=64)
          {
            pole[9]=1;
            vboost=vboost-64;
          }
          if(vboost>=32)
          {
            pole[10]=1;
            vboost=vboost-32;
          }
          if(vboost>=16)
          {
            pole[11]=1;
            vboost=vboost-16;
          }
          if(vboost>=8)
          {
            pole[12]=1;
            vboost=vboost-8;
          }
          if(vboost>=4)
          {
            pole[13]=1;
            vboost=vboost-4;
          } 
          if(vboost>=2)
          {
            pole[14]=1;
            vboost=vboost-2;
          }
          if(vboost>=1)
          {
            pole[15]=1;
          }
          if(vboost<1)
          {
            pole[15]=1;
          }
          pole[0]=1;
          pole[1]=0;
          pole[2]=0;
          pole[3]=1;
          pole[4]=1;
          pole[5]= not(pole[0] xor pole[1] xor pole[2] xor pole[3] xor pole[4] xor pole[6] xor pole[7] xor pole[8] xor pole[9] xor pole[10] xor pole[11] xor pole[12] xor pole[13] xor pole[14] xor pole[15]);
          send();  
          loop();     
       }    
   }
   pom3=0;
 }
 //**********************************Nastaven proudu buckem 1******************************************************************************
 void current1()
 {
  pom3=0;
     for(int a=0;a<16;a++)
     {
       pole[a]=currentbuck1[a]; 
     }
     send();
     pole[6]=prijem[6];
     pole[7]=prijem[7];
     Serial.print("Zadejte hodnotu proudu mA: ");
     while(pom3==0)
       {  
         while(Serial.available()>0)
         {
           for(int a=0;a<3;a++)
           {       
             currentpole[a] = Serial.read()-48;
             delay(10);
           }
           current=((currentpole[0]*100)+(currentpole[1]*10)+(currentpole[2]));
           
           
           if(current>0)
           {
             pom3=1;
           }
           
           Serial.println(current);
           if((current<120)or(current>990))
           {
             Serial.println("ERROR");  
             current=120;
           }
           current=current+50;
           VTHR=current*0.33;
          
           Serial.println(VTHR);
           spi=log(VTHR/31.5);
           spi=spi/0.01;
           Serial.println(spi);
           spi*100;
           Serial.println(spi);
           for(int a=0;a<8;a++)
           {
             if(spi>mocnina[7-a])
             {
               pole[8+a]=1;
               spi=spi-mocnina[7-a]; 
             }
           }
           pole[0]=1;
           pole[1]=0;
           pole[2]=1;
           pole[3]=0;
           pole[4]=0;
           pole[5]= not(pole[0] xor pole[1] xor pole[2] xor pole[3] xor pole[4] xor pole[6] xor pole[7] xor pole[8] xor pole[9] xor pole[10] xor pole[11] xor pole[12] xor pole[13] xor pole[14] xor pole[15]);
           send();  
           for(int b=0;b<16;b++)
           {
             Serial.print(pole[b]); 
           }
           loop();     
          
              
       }    
   }
   pom3=0;
     
     
 }




 //***********************************************************************Nastaven proudu na Bucku 2************************************
  void current2()
 {
  pom3=0;
     for(int a=0;a<16;a++)
     {
       pole[a]=currentbuck2[a]; 
     }
     send();
     pole[6]=prijem[6];
     pole[7]=prijem[7];
     Serial.print("Zadejte hodnotu proudu mA: ");
     while(pom3==0)
       {  
         while(Serial.available()>0)
         {
           for(int a=0;a<3;a++)
           {       
             currentpole[a] = Serial.read()-48;
             delay(10);
           }
           current=((currentpole[0]*100)+(currentpole[1]*10)+(currentpole[2]));
           if(current>0)
           {
             pom3=1;
           }
           
           Serial.println(current);
           if((current<100)or(current>990))
           {
             Serial.println("ERROR");  
             current=100;
           }
           current=current+50;
           VTHR=current*0.33;
          
           Serial.println(VTHR);
           spi=log(VTHR/31.5);
           spi=spi/0.01;
           Serial.println(spi);
           spi*100;
           Serial.println(spi);
           for(int a=0;a<8;a++)
           {
             if(spi>mocnina[7-a])
             {
               pole[8+a]=1;
               spi=spi-mocnina[7-a]; 
             }
           }
           for(int b=0;b<16;b++)
           {
             Serial.print(pole[b]); 
           }
           pole[0]=1;
           pole[1]=0;
           pole[2]=1;
           pole[3]=0;
           pole[4]=1;
           pole[5]= not(pole[0] xor pole[1] xor pole[2] xor pole[3] xor pole[4] xor pole[6] xor pole[7] xor pole[8] xor pole[9] xor pole[10] xor pole[11] xor pole[12] xor pole[13] xor pole[14] xor pole[15]);
           send();  
           loop();     
          
              
       }    
   }
   pom3=0;
     
     
 }
 //**********************************Mereni napeti******************************************************************************
 void zmereno()
 {
  for(int m=0;m<7;m++)
    { 
      for(int n=0;n<16;n++)
      {
        pole[n]=polemereni[m][n];
      }
      send();
      delay(10);
      if(m==0)
      {
        napeti=prijem[15]+(2*prijem[14])+(4*prijem[13])+(8*prijem[12])+(16*prijem[11])+(32*prijem[10])+(64*prijem[9])+(128*prijem[8]);
        napeti=napeti*274;
        napeti=napeti/1000;
        Serial.print("VLED1= ");
        Serial.println(napeti);
      }
      if(m==1)
      {
        napeti=prijem[15]+(2*prijem[14])+(4*prijem[13])+(8*prijem[12])+(16*prijem[11])+(32*prijem[10])+(64*prijem[9])+(128*prijem[8]);
        napeti=napeti*274;
        napeti=napeti/1000;
        Serial.print("VLED2= ");
        Serial.println(napeti);       
      }
      if(m==2)
      {
        napeti=prijem[15]+(2*prijem[14])+(4*prijem[13])+(8*prijem[12])+(16*prijem[11])+(32*prijem[10])+(64*prijem[9])+(128*prijem[8]);
        napeti=napeti*274;
        napeti=napeti/1000;
        Serial.print("VLED1= ");
        Serial.println(napeti);
      }
      if(m==3)
      {
        napeti=prijem[15]+(2*prijem[14])+(4*prijem[13])+(8*prijem[12])+(16*prijem[11])+(32*prijem[10])+(64*prijem[9])+(128*prijem[8]);
        napeti=napeti*274;
        napeti=napeti/1000;
        Serial.print("VLED2= ");
        Serial.println(napeti);       
      }
      if(m==4)
      {
        napeti=prijem[15]+(2*prijem[14])+(4*prijem[13])+(8*prijem[12])+(16*prijem[11])+(32*prijem[10])+(64*prijem[9])+(128*prijem[8]);
        napeti=napeti-20;
        Serial.print("VTEMP= ");
        Serial.println(napeti);       
      }
      if(m==5)
      {
        napeti=prijem[15]+(2*prijem[14])+(4*prijem[13])+(8*prijem[12])+(16*prijem[11])+(32*prijem[10])+(64*prijem[9])+(128*prijem[8]);
        Serial.print("VBOOST= ");
        napeti=napeti*274; 
        napeti=napeti/1000;
        Serial.println(napeti);       
      }
      if(m==6)
      {
        napeti=prijem[15]+(2*prijem[14])+(4*prijem[13])+(8*prijem[12])+(16*prijem[11])+(32*prijem[10])+(64*prijem[9])+(128*prijem[8]);
        napeti=napeti*157;
        napeti=napeti/1000;
        Serial.print("VBAT= ");
        Serial.println(napeti);       
      }
      for(int g=0;g<16;g++)
      {
        prijem[g] = 0;
      }
      
    }
    
  
  Serial.println("");
 }
 //********************************Funkce pro komunikaci****************************************************************************
 void send()
 {
   for(int b=0;b<16;b++)
    {
      prijem[b]=0;
    }
    digitalWrite(SLS, LOW);
    delayMicroseconds(10);
    for(int k=0;k<16;k++)
    {
      digitalWrite(SDI, pole[k]);
      digitalWrite(SCLK, HIGH);
      delayMicroseconds(10);
      prijem[k]=digitalRead(SDO);
      digitalWrite(SCLK, LOW);
      delayMicroseconds(10);
    }

    delayMicroseconds(5);
    digitalWrite(SLS, HIGH);
    delayMicroseconds(5);
    digitalWrite(SLS, LOW);

    
    if(prv==99)
    {
      Serial.print("Pijat data:  ");
      for(int b=0;b<16;b++)
      {
        Serial.print(prijem[b]);
      }
    } 
 }


//Hlavni smycka
//**********************************************************************************************************************************************************
 void loop()
 {
 
  if(ctenizapis==0)
  {
    while(pom2==0)
    {
      while(Serial.available()>0)
      { 
        prv=Serial.read();
        delay(10); 
          if(prv==98)
          {
            Serial.println("b");
            onoffbuck1();
            pom2=1;
          }
          if(prv==99)
          {
            Serial.println("c");
            ctenizapis=1;
            pom2=1;
          }
          if(prv==105)
          {
            Serial.println("i");
            initboost();
            pom2=1;
          }
          if(prv==122)
          {
            Serial.println("z");
            ctenizapis=2; 
            pom2=1; 
          }
          if(prv==102)
          {
            Serial.println("f");
            current1(); 
            pom2=1; 
          }
          if(prv==103)
          {
            Serial.println("g");
            current2(); 
            pom2=1; 
          }
          if(prv==100)
          {
            Serial.println("d");
            onoffbuck2();
            pom2=1;
          }
          if(prv==120)
          {
            Serial.println("x");
            diagnostika();
            pom2=1;
          }
          if(prv==121)
          {
            Serial.println("y");
            zmereno();
            pom2=1;
          }
          if(prv==101)
          {
            Serial.println("e");
            napetibst();
            pom2=1;
          }
          
      }
    }
    pom2=0;
  }
 
  

  //Zadn adresy registru
  //**********************************************************************************************************************************************************
 if((ctenizapis==1)or(ctenizapis==2))
 {
   while(pom3==0)
   {
     Serial.print("Zadejte adresu registru v HEX:  ");
     pom3=1;
   }
   while(Serial.available()>0)
   {
     pom=1;
     cteni[i]=Serial.read();
     i++; 
     delay(10);
   }
 
   i=0;
   if(pom==1)
   {
     adress1=int(cteni[0])-48;
     if(adress1>9)
     {
       adress1=adress1-7;
     }
     adress2=int(cteni[1])-48;
     if(adress2>9)
     {
       adress2=adress2-7;
     }
     adresa=(adress1*16)+adress2;

    
     Serial.print(adress1);
     Serial.print(adress2);
     Serial.println("h");
     if(ctenizapis==1)
     {
       if(adresa>=16)
       {
         pole[1]=1;
         adresa=adresa-16;
       }
       if(adresa>=8)
       {
         pole[2]=1;
         adresa=adresa-8;
       }
       if(adresa>=4)
       {
         pole[3]=1;
         adresa=adresa-4;
       }
       if(adresa>=2)
       {
         pole[4]=1;
         adresa=adresa-2;
       }
       if(adresa>=1)
       {
         pole[5]=1;
       }
     }
     if(ctenizapis==2)
     {
       if(adresa>=8)
       {
         pole[1]=1;
         adresa=adresa-8;
       }
       if(adresa>=4)
       {
         pole[2]=1;
         adresa=adresa-4;
       }
       if(adresa>=2)
       {
         pole[3]=1;
         adresa=adresa-2;
       }
       if(adresa>=1)
       {
         pole[4]=1;
       }
     }
     pom4=2;
     pom=0;
   }
 }
  //**********************************************************************************************************************************************************



 //Nulovn zbytku registru pro ten,vypocet parity
 //**********************************************************************************************************************************************************
 if((ctenizapis==1)and(pom4==2))
 {
    i=0;
    while(i<9)
    {
        pole[i+7]=0;
        i++;
  
    }
    Serial.println("");
    Serial.print("Odeslan data: ");
    pole[0]=0;
    pole[6]= (pole[0] xor pole[1] xor pole[2] xor pole[3] xor pole[4] xor pole[5]);
    if((pole[6]==0) and (pom6==0))
    {
      pole[6]=1;
      pom6=1;
    }

    if((pole[6]==1) and (pom6==0))
    {
      pole[6]=0;
      pom6=1;
    }
    for(int a=0;a<16;a++)
    {
      Serial.print(pole[a]);  
    }
   pom4=1;
 } 
 //**********************************************************************************************************************************************************
 


 //Zpis datovch bit do registru, vypocet parity
 //**********************************************************************************************************************************************************
 if((ctenizapis==2)and(pom4==2))
 {
  Serial.println("Zadejte hodnotu pro dan registr (10b):");
    i=0;
    while(i<10)
    {
     while(Serial.available()>0)
     {
        hodnota[i]=Serial.read()-48;
        pole[i+6]=hodnota[i];
        i++;
        delay(10);
     }
    }
    Serial.println("");
    Serial.print("Odeslan data: ");
    pole[0]=1;
    pole[5]= not(pole[0] xor pole[1] xor pole[2] xor pole[3] xor pole[4] xor pole[6] xor pole[7] xor pole[8] xor pole[9] xor pole[10] xor pole[11] xor pole[12] xor pole[13] xor pole[14] xor pole[15]);
    for(int a=0;a<16;a++)
    {
      Serial.print(pole[a]);  
    }
   pom4=1;
 }
  //**********************************************************************************************************************************************************


 //Komunikace a vpis daj
 //**********************************************************************************************************************************************************
 if((pom4==1)and(ctenizapis!=3))
 {
    send();
    pom5=1;
 }
  //********************************************************************************************************************************************************** 
  


  //Reset promnnch
 //**********************************************************************************************************************************************************
 if(pom5==1)
 {
  Serial.println("");
  Serial.println("-------------------------------------------------");
  Serial.println("");
  i=0;
  pom=0;
  pom2=0;
  pom3=0;
  pom4=0;
  pom5=0;
  pom6=0;
  ctenizapis=0;
  adress1=0;
  adress2=0;
  adresa=0;
  prv=0;
  for(int g=0;g<16;g++)
  {
    pole[g] = 0;
    prijem[g] = 0;
  }
  for(int h=0;h<12;h++)
  {
    cteni[h] = 0;
  }
  for(int j=0;j<10;j++)
  {
    hodnota[j] = 0; 
  }
  }
 }