

#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/signal.h>
#include <avr/sleep.h>
#include <stdlib.h>


unsigned int n;


void delay(unsigned int timer);
SIGNAL(SIG_OUTPUT_COMPARE1A);


int main(void)
{
	DDRB = 0xFF;
	PORTB = 0x00;
    
	set_sleep_mode(SLEEP_MODE_IDLE);
	
	delay(2000);
	
	for(;;);	
}


void delay(unsigned int timer)
{
	n = 0;
	
	TCCR1B |= (1<<WGM12) | (1<<CS10);		
	OCR1A = F_CPU / 1000 - 1;
	TCNT1 = 0;
	
	TIMSK |= (1<<OCIE1A);
	sei();
	
	//für Interrupt:
	while(n<timer)
	{
		PORTB |= (1<<PB0);
		sleep_mode();
	}
	
	
	//ohne Interrupt, Output-Compare-Match-Flag abfragen
	/*
	while(n<timer)
	{
		if (TIFR & (1<<OCF1A))
		{
			n++;
			TIFR = (1<<OCF1A);
			TCNT1 = 0;
			PORTB |= (1<<PB1);
		}
	}
	*/ 
	
	PORTB &= ~(1<<PB0);
	PORTB &= ~(1<<PB1);
	
	TCCR1B = 0;
}


SIGNAL(SIG_OUTPUT_COMPARE1A)
{
	n++;
	if (n == 2000) PORTB |= (1<<PB1);
}


