//
// UART Example with Rx interrupt
// By PEK '2005
//
// Compiler: GCC
// MCU: ATmega88
// Clock: 32768 Hz
//

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

#ifndef BYTE
#define BYTE unsigned char 
#endif

SIGNAL(SIG_USART_RECV)
{
	BYTE data;
	
	data = UDR0;	// Store received byte

	while(!(UCSR0A & _BV(UDRE0)));	// Wait for empty transmit buffer
	
	UDR0 = data + 1;	// Send byte + 1
}

int main(void)
{
	// Initiate UART
	PRR = ~_BV(PRUSART0);	// Only enable the USART
	UBRR0 = 26;	// 150 bps (32768/(8*150)=26)
	UCSR0A = _BV(U2X0);	// Double speed
	UCSR0B = _BV(RXEN0) | _BV(TXEN0) | _BV(RXCIE0);	// Rx Complete Interrupt & Enable Rx/Tx
	UCSR0C = _BV(UCSZ01) | _BV(UCSZ00);	// 8N1

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

