#include <avr/io.h>
#include <inttypes.h>
#include <avr/interrupt.h>
#include <stdlib.h>
#include "mydefs.h"
#include "serial.h"

unsigned char ser_getc (void)
{
 unsigned char c;
while ( !(UCSRA & (1<<RXC)) );
c = UDR;
 return c;
}

void ser_putc(unsigned char c)
{
  //UCSRB &= ~(1<<RXEN);
while ( !( UCSRA & (1<<UDRE)) ); //warten bis letztes Byte gesendet wurde
  UDR=c;    //Byte in Sendepuffer
 // UCSRB |= (1<<RXEN);
}

void ser_puts(unsigned char * s)
{
 unsigned char c;

   while((c=*s++))
    {
     if(c == '\n') //CR und LF senden bei \n
      {
       ser_putc(0x0D); //CR
       ser_putc(0x0A); //LF
      }
     else ser_putc(c);
    }
}


void ser_init(void)
{
 UBRRH = (uint8_t) (UBRR_BAUD>>8);
     UBRRL = (uint8_t) (UBRR_BAUD & 0x0ff);

    // Aktivieren von receiver und transmitter
    UCSRB = (1<<RXEN)|(1<<TXEN);

    // Einstellen des Datenformats: 8 Datenbits, 1 Stoppbit
    UCSRC = (1<<URSEL)|(1<<UCSZ1)|(1<<UCSZ0);

}
