//
// UART Example with Rx interrupt
// By PEK '2004
//
// Compiler: GCC
// MCU: ATtiny2313
// Clock: 8 MHz
//

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

#ifndef BYTE
#define BYTE unsigned char 
#endif

SIGNAL(SIG_USART0_RX)
{
	BYTE data;
	
	data = UDR;	// Store received byte
	
	PORTB = data;	// Show byte on PORTB

	while(!(UCSRA & _BV(UDRE)));	// Wait for empty transmit buffer
	
	UDR = data + 1;	// Send byte + 1
}

int main(void)
{
	// Initiate UART
	UBRRH = 0;	// 19200 bps at 8 MHz
	UBRRL = 25;
	
	UCSRB = _BV(RXEN) | _BV(TXEN) | _BV(RXCIE);	// Rx Complete Interrupt & Enable Rx/Tx
	UCSRC = _BV(UCSZ1) | _BV(UCSZ0);	// 8N1
	
	// Initiate Ports
	DDRB =0xFF;	// PortB as output

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