#define F_CPU 16000000UL					// frekvence krystalu
#include <math.h>
#include <avr/io.h>						// I/O vstupy ATmega16
#include <util/delay.h>					// knihovna casu
#include <avr/interrupt.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

#define LCD
#include "mojelib1.h"					// knihovna pro LCD

//---------hlavicky fce a promenne

void configUART();
char ReceiveUART(void);
void TransmitUART(char data);
void uart_send_string(char *string);

char data;
int i=1;
int k=1;

int main(void)
{
	DDRB=0xff;
	DDRC = 0xff;//B a C je vystup na displej
	PORTC = 0b11000010; //povoleni lcd

	init_lcd();			//inicializace LCD
	_delay_ms(10);
	cls();				// smazání LCD displeje
	_delay_ms(10);

	configUART();	// initialize UART

	sei();			// enable interrupt
	uart_send_string("M 1\r\n");//pull mode
	_delay_ms(500);
	uart_send_string("M 1\r\n");//pull mode jeste jednou
	while(1)
    {
	_delay_ms(1000);
	uart_send_string("%\r\n");//jen kyslik
    }
	return 1;
}



ISR (USART_RXC_vect)
{
	data=ReceiveUART();//UDR;	// receive char a zobrazi na displeji

	gotoxy(i,k);

	lcd_c(data);
	k++;
	if(k==17)
	{
		i++;
		k=1;
	}
	if(i==3)
	{
		i=1;
	}
}


void configUART()					// UART settings
{
   //--Set baud rate--
   	UCSRA = 0b00100000;	// vymazání zásobníku UDR
	//UBRRH = 0b00000000; //(UBRR_VALUE>>8);
	UBRRL = 0b01100111; //UBRR_VALUE; 103
	UCSRB = 0b10011000; //UCSRC |= (1 << URSEL)| (1 << UCSZ0) | (1 << UCSZ1); //8bit data, 1-stop
	UCSRC = 0b10000110; //UCSRB |= (1 << TXEN) | (1 << RXEN); //povolit prijmac i vysilac a preruseni na prijem
}

char ReceiveUART(void){				// function for receive from uart
	while(!(UCSRA & (1<<RXC)));
	return UDR;
}

void TransmitUART(char data){		// function for transmit via uart
	while(!(UCSRA & (1<<UDRE)));
	UDR = data;
}

void uart_send_string(char *string) //function for sending string via uart
{
	int length = strlen(string);
    for(int i=0; i<length; i++){
    TransmitUART(string[i]);
    }
}


