#include <avr/io.h>
#include "uart.h"



void USART_Init(unsigned int ubrr) 
{
  /*Set baud rate */
  UBRR0H = ubrr>>8; 
  UBRR0L = ubrr;
  /*Enable receiver and transmitter */ 
  UCSR0B = (1<<TXEN0);
		 //| (1<<RXEN0)
		 //|(1<<RXCIE0);
  /* Set frame format: 8data, 1 stop bit */ 
  UCSR0C = (1<<UCSZ00) | (1 << UCSZ01);
}

void USART_Transmit(unsigned char data ) 
{
  /* Wait for empty transmit buffer */ 
  while ( !( UCSR0A & (1<<UDRE0)) )
    ;
  /* Put data into buffer, sends the data */ 
  UDR0 = data;
}

void uart_puts(char * str) {
    while (*str) {
        putchar(*str++);
    }
}


int putchar(int c){
  USART_Transmit((char) c);
  //while ( !( UCSR0A & (1<<UDRE0)) )
  //UDR0 = c;
  return 0;
}

/*
void search_i2c_devices(){
  char error, address;
  int nDevices;

  uart_puts("Scanning...");


  nDevices = 0;
  for (address = 1; address < 127; address++ )
  {
	uart_puts("\n");
    uart_puts("Scanning...");
    // The i2c_scanner uses the return value of
    // the Write.endTransmisstion to see if
    // a device did acknowledge to the address.
    i2c_start_wait(address);
    error = i2c_readNak();

    if (error == 0)
    {
      uart_puts("I2C device found at address 0x");
      if (address < 16)
       uart_puts("0");
      uart_puts(address);
      uart_puts("  !");

      nDevices++;
    }
    else if (error == 4)
    {
      uart_puts("Unknown error at address 0x");
      if (address < 16)
       uart_puts("0");
       uart_puts(address);
    }
  }
  if (nDevices == 0)
    uart_puts("No I2C devices found\n");
  else
    uart_puts("done\n");

//  delay();           // wait 5 seconds for next scan
	
}
*/
