
#ifndef BAUD_RATE
#define BAUD_RATE 9600
#endif

void usart_init(void)
{
	// Set baud rate

	UBRRH = ((F_CPU / 16 + BAUD_RATE / 2) / BAUD_RATE - 1) >> 8;
	UBRRL = ((F_CPU / 16 + BAUD_RATE / 2) / BAUD_RATE - 1) & 0xff;

	// Set frame format to 8 data bits, no parity, 1 stop bit
//	UCSRC = (0<<USBS)|(1<<UCSZ1)|(1<<UCSZ0)|(1<<URSEL);
	UCSRC=(1<<URSEL)|(0<<UMSEL)|(0<<UPM1)|(0<<UPM0)|(0<<USBS)|(0<<UCSZ2)|(1<<UCSZ1)|(1<<UCSZ0);
	// Enable receiver and transmitter
//	UCSRB = (1<<RXEN)|(1<<TXEN)|(1<<RXCIE);
	UCSRB = (1<<RXEN)|(1<<TXEN);
}

void usart_sendbyte(uint8_t u8Data)
{

	// Wait if a byte is being transmitted
	while((UCSRA&(1<<UDRE)) == 0);

	// Transmit data
	UDR = u8Data;
}


uint8_t usart_receivebyte(void)
{

	// Wait until a byte has been received
	while((UCSRA&(1<<RXC)) == 0);

	// Return received data
	return UDR;

}

void usart_print(char* str) {
	while(*str != 0) {
//		if(*str == '\n') {
//			lcd_addr();
//		} else {
			usart_sendbyte(*str);
//		}
		str++;
	}
}
