You are on page 1of 2

#ifndef UARTInit

#define UARTInit
#define EVEN 0
#define ODD 1
#include <math.h>
#include <avr/io.h>
unsigned char ReceiveUART0(void)
{
while (! (UCSR0A & (1 << RXC0)) );
return UDR0;
}
void transmitUART0(unsigned char data)
{
//Wait until the Transmitter is ready
while (! (UCSR0A & (1 << UDRE0)) );
//Get that data out a here!
UDR0 = data;
}
void InicioUART0(int baud,char AsyncDoubleSpeed, char DataSizeInBits,char Parity
EVENorODO, char StopBits)
{
uint16_t UBBRValue = lrint((F_CPU / (16L * baud )) - 1);
if (AsyncDoubleSpeed==1) UCSR0A = (1 << U2X0); //setting the U2X bit to
1 for double speed asynchronous
//Put the upper part of the baud number here (bits 8 to 11)
UBRR0H = (unsigned char) (UBBRValue >> 8);
//Put the remaining part of the baud number here
UBRR0L = (unsigned char) UBBRValue;
//Enable the receiver and transmitter
UCSR0B = (1 << RXEN0) | (1 << TXEN0);
//Set 2 stop bits
if(StopBits==2)UCSR0C = (1 << USBS0);
if(ParityEVENorODO==EVEN) UCSR0C |= (1 << UPM01); //Sets parity to EVEN
if(ParityEVENorODO==ODD) UCSR0C |= (3 << UPM00); //Alternative way to se
t parity to ODD
if
if
if
if

(DataSizeInBits==6)
(DataSizeInBits==7)
(DataSizeInBits==8)
(DataSizeInBits==9)

UCSR0C
UCSR0C
UCSR0C
UCSR0C

|=
|=
|=
|=

(1
(2
(3
(7

<<
<<
<<
<<

UCSZ00);
UCSZ00);
UCSZ00);
UCSZ00);

//6-bit
//7-bit
//8-bit
//9-bit

data
data
data
data

length
length
length
length

}
void InicioUART1(int baud,char AsyncDoubleSpeed, char DataSizeInBits,char Parity
EVENorODO, char StopBits)

{
uint16_t UBBRValue = lrint((F_CPU / (16L * baud) ) - 1);
if (AsyncDoubleSpeed==1) UCSR0A = (1 << U2X0); //setting the U2X bit to
1 for double speed asynchronous
//Put the upper part of the baud number here (bits 8 to 11)
UBRR1H = (unsigned char) (UBBRValue >> 8);
//Put the remaining part of the baud number here
UBRR1L = (unsigned char) UBBRValue;
//Enable the receiver and transmitter
UCSR1B = (1 << RXEN1) | (1 << TXEN1);
//Set 2 stop bits
if(StopBits==2)UCSR1C = (1 << USBS1);
if(ParityEVENorODO==EVEN) UCSR1C |= (1 << UPM11); //Sets parity to EVEN
if(ParityEVENorODO==ODD) UCSR1C |= (3 << UPM10); //Alternative way to se
t parity to ODD
if
if
if
if
}
#endif

(DataSizeInBits==6)
(DataSizeInBits==7)
(DataSizeInBits==8)
(DataSizeInBits==9)

UCSR1C
UCSR1C
UCSR1C
UCSR1C

|=
|=
|=
|=

(1
(2
(3
(7

<<
<<
<<
<<

UCSZ10);
UCSZ10);
UCSZ10);
UCSZ10);

//6-bit
//7-bit
//8-bit
//9-bit

data
data
data
data

length
length
length
length

You might also like