0% found this document useful (0 votes)
13 views3 pages

Uart

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views3 pages

Uart

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

DESIGN NOTE

KEYWORDS:
HARDWARE UART #004
This document is originally distrubuted by AVRfreaks.net, and may be distributed, reproduced, and modified
without restrictions. Updates and additional design notes can be found at: www.avrfreaks.net

Common Use of the AVR Hardware UART

Introduction This document provides a short introduction to the use of the hardware UART present in
most AVR devices. There are several existing application notes regarding the UART
(AVR304, AVR305 and AVR306). Issues outside the boundaries of this document might
be resolved in one of these.

Overview The following registers affect the AVR hardware UART:

• UDR – UART Data Register


Actually two physically separated registers sharing the same I/O address. Transmitted
and received data are written to, and read from this register.

• USR – UART Status Register


This register contains status information bits, the most commonly used being Receive
Complete, Transmit Complete, and Data Register Empty.

• UCR – UART Control Register


In this register, the transmission interrupts are enabled/disabled, as well as the transmit-
ter/receiver themselves. Specifics about the data word are also set here.

• UBRR – UART Baud Rate Register


In this register, the transmission BAUD rate is set. The datasheet on every AVR part
contains tables of the most common baudrate settings, as well as general equations to
set the correct value in the UBR register if the tables do not cover the parameters.
The operation of the UART is not very complex. In the following, two examples will be
presented: Polled and Interrupt controlled UART. The latter can be expanded further as
suggested in the application note AVR306.

www.avrfreaks.net 1
Design Note #004 – Date: 02/02
Code examples The following code should be self-explanatory if the comments are read.

Polled UART
C Code Assembly
//include definitions for the AT90S8515 ;include definitions for the AT90S8515
#define ENABLE_BIT_DEFINITIONS .include "8515def.inc"
#include <io8515.h>
;definitions
//initialize UART .def temp = r16 ;temporary data
void InitUART(unsigned char baudrate)
{ lditemp,low(RAMEND)
UBRR = baudrate; outSPL,temp
//enable receiver and transmitter lditemp,high(RAMEND)
UCR |= (1<<RXEN)|(1<<TXEN); outSPH,temp;init Stack Pointer
} rjmpstart;reset handler

//receive a byte ;initialize UART


unsigned char ReceiveByte(void) initialize: ;baudrate in temp
{ out UBRR,temp
//polls on receive complete ;enable receiver and transmitter
while(!(USR & (1<<RXC))) ldi temp,(1<<RXEN)|(1<<TXEN)
; //wait out UCR,temp
return UDR; //return data ret
}
;receive a byte
//transmit a byte receive:
void TransmitByte(unsigned char data) sbis USR,RXC ;receive complete?
{ rjmp receive
//polls on data register empty in temp,UDR ;return data in temp
while(!(USR & (1<<UDRE))) ret
; //wait
UDR = data; //transmit data ;transmit a byte
} transmit:
sbis USR,UDRE ;ready to send?
//sample program: echo a character rjmp transmit
void main(void) out UDR,temp
{ ret
//set the baudrate to [email protected]
InitUART(11); ;sample program: echo a charachter
while(1) //eternal loop start:
{ ldi temp,11
TransmitByte(ReceiveByte()); rcall initialize ;[email protected]
}
} loop:
rcall receive
rcall transmit
rjmp loop

2 www.avrfreaks.net Design Note #004 – Date: 02/02


Interrupt driven UART
C Code Assembly
//include bit definitions for the AT90S8515 ;include bit definitions for the AT90S8515
#define ENABLE_BIT_DEFINITIONS .include "8515def.inc"
#include <io8515.h>
#include <ina90.h> .def temp = r16 ;temporary data

//declarations .org $0000


void TransmitByte(unsigned char data); lditemp,low(RAMEND)
outSPL,temp
//receive complete interrupt lditemp,high(RAMEND)
interrupt [UART_RX_vect] void outSPH,temp ;init Stack Pointer
UART_RX_interrupt(void) rjmp start ;reset handler
{
unsigned char data; .org URXCaddr ;definition in the
data = UDR; //receive data rjmp UART_RX_interrupt ;8515 include file
TransmitByte(data); //bounce data back
} ;receive complete interrupt
UART_RX_interrupt:
//initialize UART in temp ,UDR
void InitUART(unsigned char baudrate) rcall transmit
{ reti
UBRR = baudrate;
/*enable receive complete interrupt, ;initialize UART
receiver and transmitter*/ initialize:
UCR |= (1<<RXEN)|(1<<TXEN)|(1<<RXCIE); out UBRR, temp ;init baudrate
}
;enable receiver, transmitter and TXCint
//transmit a byte ldi temp, (1<<RXEN)|(1<<TXEN)|(1<<RXCIE)
void TransmitByte(unsigned char data) out UCR, temp
{ sei ;global interrupt enable
UDR = data; ret
}
;transmit a byte
void main(void) transmit:
{ sbis USR,UDRE ;ready to send?
InitUART(11); //[email protected] rjmp transmit
out UDR, temp
while(1) ret
; //eternal loop
} start:
ldi temp, 11 ;[email protected]
rcall initialize

forever:
rjmp forever ;eternal loop

Design Note #004 – Date: 02/02 www.avrfreaks.net 3

You might also like