//
// 16-bit timer with interrupt
// By PEK '2004
//
// Compiler: GCC
// MCU: ATtiny2313
// Clock: 8 MHz
//

#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/signal.h>

register unsigned char counter asm("r16");	// Counter

// Timer Compare Match ISR
SIGNAL(SIG_TIMER1_COMPA)
{
	counter++;
	PORTB = counter;
}

int main(void)
{	
	// Initiate Ports
	DDRB = 0xFF;	// PortB as output
	PORTB = 0x00;

	// Initiate Timer
	TCCR1B = _BV(WGM12) | _BV(CS12) | _BV(CS10);	// clk/1024 and Clear on match
	OCR1A = 8000000 / 1024;	// Compare register, 1 sec at 8 MHz
	
	TIMSK = _BV(OCIE1A);	// Enable Compare match interrupt
	
	counter = 0;	// Initialize counter

	sei();	// Enable global interrupts
	
	while(1);	// Loop forever
	
	return 1;
}
