#include <mega8.h>
unsigned char step_1, step_2; //счётчики для шагов
char on_off;// 0 - выкл, 1 - вкл


// External Interrupt 0 service routine - кнопка на PORTD.2
interrupt [EXT_INT0] void ext_int0_isr(void)
{
// Place your code here
if (on_off) //если был включен
{
on_off = 0; //выключаем, все выходы в ноль
PORTD.5 = 0;
PORTD.6 = 0;
PORTD.7 = 0;
step_1 = 0;
step_2 = 0;
} 
else //иначе включаем
{
on_off = 1;
step_1 = 50; //задаём 5 секунд для первого шага
}
}

// Timer1 output compare A interrupt service routine - 10 Гц
interrupt [TIM1_COMPA] void timer1_compa_isr(void)
{
// Place your code here
if (on_off && step_1) //если вкл и первый шаг
{
PORTD.5 = 1;
step_1 --;
if (!step_1) step_2 = 100; //задаём 10 секунд для второго шага
} 

if (on_off && step_2) //если вкл и второй шаг
{
PORTD.6 = 1;
step_2 --;
if (!step_2 && !PIND.3) PORTD.7 = 1; //если на входе ноль то выводим на третий выход единицу
else //иначе запускаем первый шаг
{
PORTD.6 = 0;
step_1 = 50;
}
}
}

// Declare your global variables here

void main(void)
{
// Declare your local variables here

// Input/Output Ports initialization
// Port B initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In 
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T 
PORTB=0x00;
DDRB=0x00;

// Port C initialization
// Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In 
// State6=T State5=T State4=T State3=T State2=T State1=T State0=T 
PORTC=0x00;
DDRC=0x00;

// Port D initialization - кнопка на 2 линии (подтяжка резистором), датчик на 3 (подтяжка), выходы на 5, 6, 7
// Func7=Out Func6=Out Func5=Out Func4=In Func3=In Func2=In Func1=In Func0=In 
// State7=0 State6=0 State5=0 State4=T State3=P State2=P State1=T State0=T 
PORTD=0x0C;
DDRD=0xE0;

// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
TCCR0=0x00;
TCNT0=0x00;

// Timer/Counter 1 initialization - 10 Гц
// Clock source: System Clock
// Clock value: 0.977 kHz
// Mode: CTC top=OCR1A
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: On
// Compare B Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x0D;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x62; // 977 Гц делим на 98 получаем близко к 10 Гц
OCR1BH=0x00;
OCR1BL=0x00;

// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer2 Stopped
// Mode: Normal top=FFh
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;

// External Interrupt(s) initialization - кнопка
// INT0: On
// INT0 Mode: Falling Edge
// INT1: Off
GICR|=0x40;
MCUCR=0x02;
GIFR=0x40;

// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x10;

// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;

// Global enable interrupts
#asm("sei")

while (1)
{
// Place your code here

};
}