//
// Example with external interrupts
// By PEK '2005
//
// Compiler: GCC
// MCU: ATMega88
// Clock: 32 kHz
//

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

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

// Delay of (uiDelay*4 + 8) clock cycles
void delay(unsigned int uiDelay)
{
	while(--uiDelay);
}

// External interrupt ISR
SIGNAL(SIG_INTERRUPT0)
{
	counter++;
	
	delay(817);	// Delay of 0.1 sec
	
	PORTC = counter;
}

int main(void)
{	
	// Initiate Ports
	PORTC = 0x00;
	DDRC = 0xFF;	// PortC as output

	// Initiate External Interrupt
	EICRA = _BV(ISC00);	// Logical change of INT0 generates an interrupt
	EIMSK = _BV(INT0);	// External inerrupt enable
	
	counter = 0;	// Initialize counter

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