You are on page 1of 15

USART and Asynchronous Communication

The USART is used for synchronous and asynchronous serial communication.


USART = Universal Synchronous/Asynchronous Receiver Transmitter
Our focus will be on asynchronous serial communication.
Asynchronous communication does not use a clock to validate data.
Serial data is transferred one bit at a time.
Asynchronous serial interfaces are chea! easy to use! and until recently! very
common. US" is well on its way to relace the serial comm orts on #Cs.
The USART communicates in a full$dule% mode &simultaneous %mit! rcv'
USART and Asynchronous Communication
Serial frame format(
)very frame will have at least
$one start bit
$some data bits &*!+!,!- or .'
$one sto bit
#arity is otional
USART and Asynchronous Communication
/ow can two asynchronous devices communicate with each other0
A1R2
2+3h4
A1R5
2+3h4
There is no known hase relationshi between the two A1R boards
/ow can a receivin6 board 7know8 where the center of a bit frame is0
/ow can it know where the start bit is0
9hat do we need to know0

start bit
: 2 5 ;
USART and Asynchronous Communication

start bit
: 2 5 ;
$ <eed to know how fast the bits are comin6 &baud rate'
$ <eed to know where the start bit be6ins &its fallin6 ed6e'
$ Then we know when to samle
samle samle samle samle
USART and Asynchronous Communication

$ The USART uses a 2+% internal clock to samle the start bit.
The incomin6 data is continuously samled until a fallin6 ed6e is detected.
Once detected! the receiver waits + clocks to be6in samlin6.
One clock before the e%ected center of the start bit! ; samles are taken.
=f 5 or ; are detected as hi6h! the search for another fallin6 ed6e is started.
=f at least one samle is low! the start bit is validated. =f so! be6in samlin6
2+ clocks from center of start bit.
Revalidate a6ain with each data byte. Synchroni4ation haens on each
byte.
USART and Asynchronous Communication

$ The USART internal clock is set by the U"RR re6ister.
The internal clock is derived from the internal
C#U clock.
"oth transmitter and receiver use the same clock to
oerate from. The transmitter is synchronous to the
clock. =n comin6 data to the receiver is not.
The baud rates are not e%act ower of 5 multiles
of the C#U clock! thus baud rate inaccuracies
occur at some settin6s. This is why you see some
>funny> crystal oscillator fre?uencies.
USART and Asynchronous Communication

$ )rror detection
$#arity
$created by an @OR of the data bits
$arity can be even or odd
$#
even
= dn @OR dn$2 @OR dn$5 ........ @OR d:
$#
odd
= dn @OR dn$2 @OR dn$5 ........ @OR d: @OR 2
=f we have a data byte! :b::2:A22:2
and we want odd arity! the arity bit is set to a 7one8 to make
a total of . bits which is an odd number of 2Bs.... i.e.! odd arity
Thus! the new data byte with arity is(
:b::2:A22:2 lus the arity bit 2

arity bit
USART and Asynchronous Communication

$ )rror detection
$Crame error
$a frame error occurs when the receiver is e%ectin6 the sto bit
and does not find it.
$also known as a synchroni4ation failure
$the frame &byte' must be resent
$Data overrun error
$data was not removed in the receive buffer before another
byte came and overwrote it.
sto bit&s'
USART and Asynchronous Communication

$ USART )lectrical ath
9e use the RS$5;5 standard for communications to a #C.
RS$5;5 secifies an inverted E/$ ; 1olt interface.
F= $; volts is considered a 728 or 7mark8
G= E; volts is considered a 7:8 or 7sace8
$;1
E;1
USART and Asynchronous Communication

$ USART RS5;5 transceiver
$Char6e #um inverter used to
6enerate ne6ative volta6es.
USART and Asynchronous Communication

$ USART )lectrical ath
The me6a25- board uses the 3A@5;5
chi. =t has an internal char6e um to
6enerate the ne6ative and ositive volta6es
for the RS$5;5 interface.
The D". connector is standard for matin6
with a #C serial connector.
The ort D interface is shared with the
=R H)D =/O devices. They are inverted
also so that an 7sace8 si6nal does not
kee the =R H)D on &2::mA'.
to ort D
to ort )
USART and Asynchronous Communication

USART )lectrical ath
$UART can be used with =R H)D also.
USART and Asynchronous Communication

Some useful USART Software Routines(
//**************************************************************
// uart_init
//
//RXD0 is PORT E bit 0
//TXD0 is PORT E bit 1
//Jumpers J14 and J16 (mega128.1) OR
//Jumpers J7 and J9 (mega128.2)
//must be in place for the MAX232 chip to get data.
//
void uart_init(){
//rx and tx enable, 8 bit characters
UCSR0B |= (1<<RXEN0) | (1<<TXEN0);
//async operation, no parity, one stop bit, 8-bit characters
UCSR0C |= (1<<UCSZ01) | (1<<UCSZ00);
//set to 9600 baud
UBRR0H=0x00;
UBRR0L=0x67;
}
//**************************************************************
USART and Asynchronous Communication

Some useful USART Software Routines(
//**************************************************************
// uart_putc
//
//Takes a character and sends it to USART0
//
void uart_putc(char data){
while (!(UCSR0A&(1<<UDRE))); // Wait for previous transmission
UDR0 = data; // Send data byte
while (!(UCSR0A&(1<<UDRE))); // Wait for previous transmission
}
//**************************************************************
//**************************************************************
// uart_puts
//Takes a string and sends each charater to be sent to USART0
//void uart_puts(unsigned char *str) {
void uart_puts(char *str) {
int i = 0;
//Loop through string, send each char
while(str[i] != '\0'){uart_putc(str[i]); i++;}
}
//**************************************************************
USART and Asynchronous Communication

Some useful USART Software Routines(
//**************************************************************
// uart_getc
//Modified to not block indefinately in the case of a lost byte
//
char uart_getc(void) {
uint16_t timer = 0;
while (!(UCSR0A & (1<<RXC0))) {
timer++;
if(timer >= 16000) return 0;
} // Wait for byte to arrive
return UDR0;
}
//**************************************************************

You might also like