#include <tiny2313.h>
#include <delay.h>

 bit detect_munchestrer=0, use=0;
 unsigned char  Adres, Data, nAdres, nData, b=12, simvol=0, slovo=0, n=0, input=0; 
 volatile unsigned char DSS=20;
 int j=2, TimeDeff=0; 

interrupt [TIM1_OVF] void timer1_ovf_isr(void)
 {
  
      j=1;
      use=0;
      TIMSK=0x02;
      TCNT1=0;
      TCCR0B=0x06;
      TCNT0=255;

 }

interrupt [TIM0_OVF] void timer0_ovf_isr(void)
{
 
 if (input==5)
  {
   slovo=0; 
   
   if(TCNT1>b)
    {
     slovo=1;
    }
       
                   
   nData=nData+slovo*n;     
                            
   simvol=simvol+1;
   n=n*2;
   
   if (simvol==8)
    {
     input=7; 
     if (Adres==~nAdres)
      {
       if(Data==~nData)
        {
         DSS=Data;
         use=0;
      
        }
      }
     
     
    }
    
    TCNT1=0;
    TCNT0=0xFF; 
  } 

 if (input==4)
  {
   slovo=0; 
   
   if(TCNT1>b)
    {
     slovo=1;
    }
       
                  
   Data=Data+slovo*n;     
                
   simvol=simvol+1;
   n=n*2;
   
   if (simvol==8)
    {
     simvol=0;
     input=5;
     n=1; 
     
    }
    
    TCNT1=0;
    TCNT0=0xFF; 
  }

 if (input==3)
  {
   slovo=0; 
   
   if(TCNT1>b)
    {
     slovo=1;
    }
       

                   
   nAdres=nAdres+slovo*n;     

   simvol=simvol+1;
   n=n*2;
   
   if (simvol==8)
    {
     simvol=0;
     input=4;
     n=1; 
      
    }
    
    TCNT1=0;
    TCNT0=0xFF; 
  }
 
 if (input==2)
  {
   slovo=0;

   if(TCNT1>b)
    {
     slovo=1;
    }
             

           
   Adres=Adres+slovo*n;     
                               
   simvol=simvol+1;
   n=n*2;
   
   if (simvol==8)
    {
     simvol=0;
     input=3;
     n=1;  
          
       
    }
    
    TCNT1=0;
    TCNT0=0xFF; 
  }

 if (detect_munchestrer==1 && input==1)
  {
   input=2;
   if (TCNT1<(2*b))
    {
     input=7;
    }
   TCNT1=0;
   TCNT0=0xFF;
    
   n=1; 
       
  }

 if (detect_munchestrer==0 && input==1)
  {
   if (TCNT1>=69)
    {
     detect_munchestrer=1;
     TCCR0B=0x06;
     TCNT0=0xFF;  
     simvol=0; 
     Adres=0;
     Data=0; 
     nAdres=0;
     nData=0;
     TCNT1=0; 
    }
   else
    {
     input=7;
    }  
    
                                  
  } 

 if (detect_munchestrer==0 && input==0)
  {
   TCNT1=0;
   TCCR1B=0x05;
   TIMSK=0x82;
   TCCR0B=0x07;
   TCNT0=0xFF;
   input=1;
  } 


 if (input==7)
  {
  TIMSK=0x02;
  TCNT1=0;
  TCCR0B=0x06;
  TCNT0=0xFF;
  detect_munchestrer=0;
  input=0;
  }

}

 
 void main(void)
 {  

  
  #pragma optsize-
  CLKPR=0x80;
  CLKPR=0x00;
  #ifdef _OPTIMIZE_SIZE_
  #pragma optsize+
  #endif

  DDRB=0xFF;
  DDRD=0x00;
  PORTB=0x255;
  PORTD=0xFE;
  DDRD.6=1;
  PORTD.6=0;  
  DDRD.2=1;
  PORTD.2=1;
  
  TCCR0A=0x00;
  TCCR0B=0x06; // Clock source: T0 pin Falling Edge // Timer 0 Overflow Interrupt: On
  TCNT0=0x00;
  OCR0A=0x00;
  OCR0B=0x00;
  TCNT0=0xFF; 
  
  TCCR1A=0x00;
  TCCR1B=0x05;
  TCNT1=0;
  
  TIMSK=0x02;  // Timer(s)/Counter(s) Interrupt(s) initialization

  // Global enable interrupts
  #asm("sei")

  
  while(1)
   {   

   

      if(use==0 && DSS==20) 
       {
         use=1;
        while(use)
         {  
          PORTD.2=1; 
          delay_us(30);
          PORTD.2=0;
          delay_us(5);        
         }
       }
   
     
      if(use==0 && DSS==28) 
       {
         use=1;
        while(use)
         {  
          PORTD.2=1; 
          delay_us(28);
          PORTD.2=0;
          delay_us(4);        
         }
       }
       
   
       }        
       
        

 
 
