#include <mega8.h>
#include <delay.h>
#include <stdio.h>

volatile int tik = 0;         //  
volatile char Byt;            //   
volatile bit Repeat_flag;     // 
volatile bit Start_flag=0;    //  
volatile char Address;        //
volatile char Command;        //
volatile char Address_1;      //  
volatile char Command_1;      // 
volatile char Address_0;      // 
volatile char Command_0=0x31; // 

interrupt [TIM0_OVF] void timer0_ovf_isr(void)
{
    TCNT0=253;               //253 (12/256)/(256-253)=15625 Hz 64us  12   256
    //TCNT0=254;             //254  (8/256)/(256-254)=15625 Hz 64us  8   256
    tik++;                   //3*? = 0,000064 sec
    if(tik >= 1200)          //if 0,000064*1200 = 0,0768sec
    { 
        TCCR0=0x00;                                                     //    
        TCNT0=0x00;  //  
        tik = 0;
        Repeat_flag = 0;
        Start_flag = 0;
        Address_1 = 0;
        Command_1 = 0;
        Address_0 = 0;
        Command_0 = 0;
        Address = 0;
        Command = 0;
    } 
}

// External Interrupt 0 service routine
interrupt [EXT_INT0] void ext_int0_isr(void)
{
    TCNT0=253;                                  //253 (12/256)/(256-253)=15625 Hz 64us  12   256
    TCCR0=(1<<CS02) | (0<<CS01) | (0<<CS00);    //  F/256   
    
    if((tik >= 139) && (tik < 150)) //13 - 14,4ms 
    { //   139  150 ,   
        Address = 1;
        Repeat_flag = 0;
        Start_flag = 1;
        Address_1 = 0;
        Command_1 = 0;
        Address_0 = 0;
        Command_0 = 0;   
    }          

    if((tik >= 116) && (tik < 139)) //  11.1- 13.3ms
    { //  116  138,  
        Address = 2;
        Repeat_flag = 1;
        Start_flag = 0;
        TCNT0=0x00;
    }

    //  22  115 - "1"
    if((tik >= 22) && (tik < 116) && (Start_flag == 1))   //2,1- 11.1ms
    {
        Byt++;
        if(Byt < 9) 
        {
            Address_1 = Address_1 << 1;
            Address_1 = Address_1 + 1;
        }
                
        if(Byt >= 9 && Byt < 17) 
        {
            Address_0 = Address_0 << 1;
            Address_0 = Address_0 + 1;
        }

        if(Byt >= 17 && Byt < 25)
        {
            Command_1 = Command_1 << 1;
            Command_1 = Command_1 + 1;
        }

        if(Byt >= 25) 
        {
            Command_0 = Command_0 << 1;
            Command_0 = Command_0 + 1;
        }
    }     
    
    // 10 - 21 teaks - "0"
    if(tik >= 10 && tik < 22 && Start_flag == 1)
    { 
        Byt++;

        if(Byt < 9) 
        {
            Address_1 = Address_1 << 1;
        }

        if(Byt >= 9 && Byt < 17) 
        {
            Address_0 = Address_0 << 1;
        }

        if(Byt >= 17 && Byt < 25)
        {
            Command_1 = Command_1 << 1;
        }

        if (Byt >= 25) 
        {
            Command_0 = Command_0 << 1;
        }
    }

    tik = 0;
    
    if(Byt == 32)
    { //'if have taken 4 bytes, check correctness a receiving a command
      //  'if address or command 16-bit, check does not pass
      // 'Summa = Address_0 + Address_1
      //  'If Summa = 255 Then
        Address = Address_1;
        //'Else
        //'Address = 0
        //'End If

        //'Summa = Command_0 + Command_1
        //'If Summa = 255 Then
        Command = Command_1;
        //'Else
        //'Command = 0
        //'End If
        Byt = 0;
        Repeat_flag = 0;
        Start_flag = 0;
        TCCR0=0x00;        // 
        TCNT0=0x00; 
        printf("%X-%X\n\r",Address,Command); // \n- , \r-     
    }    
}

void main(void)
{
PORTB=0x00;
DDRB=0xFF;

// Port D initialization
// Function: Bit7=In Bit6=In Bit5=In Bit4=In Bit3=In Bit2=In Bit1=In Bit0=In 
DDRD=(0<<DDD7) | (0<<DDD6) | (0<<DDD5) | (0<<DDD4) | (0<<DDD3) | (0<<DDD2) | (0<<DDD1) | (0<<DDD0);
// State: Bit7=T Bit6=T Bit5=T Bit4=T Bit3=T Bit2=P Bit1=T Bit0=T 
PORTD=(0<<PORTD7) | (0<<PORTD6) | (0<<PORTD5) | (0<<PORTD4) | (0<<PORTD3) | (0<<PORTD2) | (0<<PORTD1) | (0<<PORTD0);


// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: 46,875 kHz
TCCR0=0x00;                                                     // 
TCNT0=0x00;

// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=(0<<OCIE2) | (0<<TOIE2) | (0<<TICIE1) | (0<<OCIE1A) | (0<<OCIE1B) | (0<<TOIE1) | (1<<TOIE0);

// External Interrupt(s) initialization
// INT0: On
// INT1: Off
GICR|=(0<<INT1)  | (1<<INT0);
MCUCR=(0<<ISC11) | (0<<ISC10) | (1<<ISC01) | (0<<ISC00); // INT0 Mode:   
//MCUCR=(0<<ISC11) | (0<<ISC10) | (1<<ISC01) | (1<<ISC00); // INT0 Mode:   
GIFR= (0<<INTF1) | (1<<INTF0);


// USART initialization 
// Communication Parameters: 8 Data, 1 Stop, No Parity
// USART Receiver: Off
// USART Transmitter: On
// USART Mode: Asynchronous
// USART Baud Rate: 9600
UCSRA=(0<<RXC)   | (0<<TXC)   | (0<<UDRE)  | (0<<FE)   | (0<<DOR)  | (0<<UPE)   | (0<<U2X)   | (0<<MPCM);
UCSRB=(0<<RXCIE) | (0<<TXCIE) | (0<<UDRIE) | (0<<RXEN) | (1<<TXEN) | (0<<UCSZ2) | (0<<RXB8)  | (0<<TXB8);
UCSRC=(1<<URSEL) | (0<<UMSEL) | (0<<UPM1)  | (0<<UPM0) | (0<<USBS) | (1<<UCSZ1) | (1<<UCSZ0) | (0<<UCPOL);
UBRRH=0x00;
//UBRRL=0x33; //8
UBRRL=0x4D; //12


// Global enable interrupts
#asm("sei")
printf("Hello");


while (1)
      {  
        /*
       if(Command == 0xe0)PORTD.0=1;
       if(Command == 0xd0)PORTD.0=0;           //If pressed key "P+" - to include LED
    */    //if (Command == 13) PORTC.2 = 0;      //If pressed key "P-" - to switch off LED
        delay_ms(100); 

      }
}
