/*****************************************************
This program was produced by the
CodeWizardAVR V2.03.4 Standard
Automatic Program Generator
Date    : 05.10.2009
Author  : Birukov N.
Chip type           : ATtiny2313V
Clock frequency     : 0,032768 MHz
Memory model        : Tiny
External RAM size   : 0
Data Stack size     : 32
*****************************************************/
#include <tiny2313.h>
#include <delay.h>
unsigned char a,b,c,i; //a-,

// Timer 1 output compare A interrupt service routine
interrupt [TIM1_COMPA] void timer1_compa_isr(void)
    { b++;
    MCUCR=0x00;
       };
void main(void)
{
// Crystal Oscillator division factor: 1
#pragma optsize-
CLKPR=0x80;
CLKPR=0x00;
#ifdef _OPTIMIZE_SIZE_
#pragma optsize+
#endif
// Input/Output Ports initialization
// Port A initialization
// Func2=In Func1=In Func0=In
// State2=T State1=T State0=T
PORTA=0x00;
DDRA=0x00;
// Port B initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=Out Func1=In Func0=Out
// State7=T State6=T State5=T State4=T State3=T State2=0 State1=T State0=0
PORTB=0x00;
DDRB=0x05;
// Port D initialization
// Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State6=T State5=T State4=T State3=T State2=P State1=P State0=P
PORTD=0x07;
DDRD=0x00;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: 32,768 kHz
// Mode: CTC top=OCR0A
// OC0A output: Toggle on compare match
// OC0B output: Disconnected
TCCR0A=0x00;
TCCR0B=0x01;
TCNT0=0x00;
OCR0A=0x04;
OCR0B=0x00;
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: 0,512 kHz
// Mode: CTC top=OCR1A
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer 1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: On
// Compare B Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x0B;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x77;
OCR1AL=0xFD;
OCR1BH=0x00;
OCR1BL=0x00;
// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
// Interrupt on any change on pins PCINT0-7: Off
GIMSK=0x00;
MCUCR=0x00;
// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x40;
// Universal Serial Interface initialization
// Mode: Disabled
// Clock source: Register & Counter=no clk.
// USI Counter Overflow Interrupt: Off
USICR=0x00;
// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
    c=~PIND;
    c=c&0x7; a=1;
    if (c==1) { a=1;
    goto m1;
    };
     if (c==2) { a=2;
    goto m1;
    };
     if (c==3) { a=3;
    goto m1;
    };
     if (c==4) { a=4;
    goto m1;
    };
     if (c==5) { a=5;
    goto m1;
    };
     if (c==6) { a=5;
    OCR1AH=0x3B;
    OCR1AL=0xFE;
    goto m1;
    };
     if (c==7) { a=3;
    OCR1AH=0x63;
    OCR1AL=0xFD;
    goto m1;
    };
// Global enable interrupts
m1:  #asm("sei")
  while (1)
{
    if (b>=a) {b=0;
    PORTB.0=1;
    TCCR0A=0x42;
    delay_ms(400);
    TCCR0A=0x00;
    PORTB.0=0;
    delay_ms(120);
   
    PORTB.0=1;
    TCCR0A=0x42;
    delay_ms(400);
    TCCR0A=0x00;
    PORTB.0=0;
    delay_ms(120);

    PORTB.0=1;
    TCCR0A=0x42;
    delay_ms(100);
    TCCR0A=0x00;
    PORTB.0=0;
    delay_ms(10);
   }
   PORTB.0=0;
 
    if (b<<a)
     {
    for (i=0;i<b;i++)
    {PORTB.0=1;
    TCCR0A=0x42;
    delay_ms(90);
    TCCR0A=0x00;
    PORTB.0=0;
    delay_ms(90);   
    }; 
   }
     MCUCR=0x20;
      #asm("sleep")    
};
}