Atmel UART

AVR UART register programming directly

This works on Arduino and saves lots of memory.. if you’re using ATTiny it’s well worth it.
Some chips have a UART  and some a USART – acronym below:

UART – Universal Asynchronous Receiver/Transmitter
USART – Universal Synchronous/Asynchronous Receiver/Transmitter)

Traditionally on an Arduino you would use Serial.begin(baud); and then Serial.write, println, etc
That is a nice easy way of communicating via serial and lots is done for you in the back end.

AVR UART register programming directly
Why? Because I use ATTiny a lot for small projects, 2 particular chips are the ATTiny 2313 and 4313, same features just the latter has more memory.. saying that they don’t have very much memory at all, both contain a UART but the 2313 only has 2KB flash and 128 bytes RAM. So using the traditional Serial.begin arduino command it takes up a bit of space!

Another reason is. Once you know how to program registers you can read the datasheet and open up so much more. 
You can then break away from Arduino and devlop using professional software as embedded engineers would.

Step 1: In the top section anywhere before the setup

#define BAUD 9600  // – – Change this to your BAUD rate
#define BAUD_PRESCALER (((F_CPU / (BAUD * 16UL))) – 1)

Step 2: Inside the setup

UBRRH = (uint8_t)(BAUD_PRESCALER >> 8);
UCSRB |= (1 << RXEN)|(1 << TXEN)|(0 << RXCIE);
UCSRC |= (1 << UCSZ1)|(1 << UCSZ0);

Step 3: Inside the loop to poll for data (inside the while loop)

while((UCSRA & (1<<RXC))) {
byte DataIn  = UDR;
//Your code here

Step 4: To transmit data (outide the loop as a subroutine)

static void uart_transmit(byte data){
while (!(UCSRA & (1<<UDRE))); // wait while register is free
UDR = data; // load data in the register

If you download the sketch ‘MIDI Beat Flasher’ from the downloads page it gives a good example of this being used to receive MIDI data.

Multi UART

Some chips like the ATMega 644 and 1284 have 2 UARTS, so you need to add a 0 or 1 to the end of each register command  (e.g UDR0) or it will error.
Except this section which is as follows:

UCSRC |= (1 << UCSZ01)|(1 << UCSZ00); //UART 0
UCSRC |= (1 << UCSZ11)|(1 << UCSZ10); //UART 1