#define F_CPU 16000000UL
#include <avr/io.h>
#include <stdio.h>
#include <stdint.h>
#include <util/delay.h>

void USART0inicializace()
{
  UBRR0 = 103;  //9615 Bd
  
  //8bitovy znak, bez parity, povoleni vysilace:
  UCSR0A = 0;
  UCSR0B = (1 << TXEN0);
  UCSR0C = (1 << UCSZ01) | (1 << UCSZ00);
}

inline uint8_t getbit(volatile uint8_t reg, uint8_t bit)
{
  return (reg & (1 << bit)) >> bit;
}

void USART0posli(const char *text)
{
  uint8_t i = 0;
  
  while(text[i] != '\0')  //test konce retezce
  {
    //ceka na uvolneni vysilaciho registru:
    while(!getbit(UCSR0A,UDRE0));
    
    UDR0 = text[i];  //vlozi znak pro odeslani
    i++; 
  }
}

int main(void)
{
  char zprava[40];
  uint8_t cislo = 1;
  
  USART0inicializace();
  
  while (1) 
  {
    //sestaveni textu:
    sprintf(zprava, "cislo = %u\n", cislo);
    //odeslani:
    USART0posli(zprava);
    cislo++;
    _delay_ms(500);
  }
}

