/*
 * UART_328P.c
 *
 * Created: 19.02.2018 20:33:28
 * Author : yanni
 */ 

#define F_CPU 16000000UL //externer Quarz
#include <avr/io.h>
#include "avr/interrupt.h"
#include "util/delay.h"
#define Bautrate 9600
#define USART_RXC_bm 0x80
//#define Bautregister (F_CPU/(16 * Bautrate) -1)
volatile uint8_t Data;
uint8_t Richtig;
uint8_t Array[10] = {0x7E, 0xFF, 0x06, 0x3F, 0x00, 0x00, 0x02, 0xFE, 0xBA, 0xEF};
uint8_t Array1[10] = {0x7E, 0xFF, 0x06, 0x06, 0x00, 0x00, 0x1E, 0xFE, 0xD7, 0xEF};
uint8_t Array2[10] = {0x7E, 0xFF, 0x06, 0x0D, 0x00, 0x00, 0x00, 0xFE, 0xEE, 0xEF};
uint8_t Array3[10] = {0x7E, 0xFF, 0x06, 0x03, 0x00, 0x00, 0x04, 0xFE, 0xF7, 0xEF};
uint8_t Empfangen(void);

typedef enum  {Idle, Data0, Data1, Data2, Data3, Data4, Data5, Data6, Data7, Fine}eRXStates;

uint8_t usart_rx_is_complete()
{
	return UCSR0A & USART_RXC_bm;
}

unsigned char usart_getchar()
{
	while (usart_rx_is_complete() == 0) {}
	
	return UDR0;
}

void USART_putchar( unsigned char data )
{
	/* Wait for empty transmit buffer */
	while ( ( UCSR0A & (1<<UDRE0))== 0 ){}
	/* Put data into buffer, sends the data */
	UDR0 = data;
}

int main(void)
{
    /* Replace with your application code */
	DDRB |= (1<<PORTB0);
	uint8_t i;
	//uint32_t Bautregister = F_CPU/(16*Bautrate)-1;
	UBRR0L = 103;   //9600 Baud
	UBRR0H &= 0b11110000;
	UCSR0B |= ((1<<TXEN0)|(1<<RXEN0));//|(1<<RXCIE);
	//sei();
	//UCSRC |= (1<<URSEL)|(1<<UCSZ1)|(1<<UCSZ2);
    while (1) 
    {
		/*Data = usart_getchar();
		Richtig = Empfangen();
		if (Richtig == 1)
		{
			PORTB |= (1<<PORTB0);
		}*/
		for (i = 0; i <= 9; i++)
		{
			USART_putchar( Array3[i]);
		}
		_delay_ms(10);
		for (i = 0; i <= 9; i++)
		{
			USART_putchar( Array1[i]);
		}
		_delay_ms(10);
		for (i = 0; i <= 9; i++)
		{
			USART_putchar( Array2[i]);
		}
    }
}

