
137
ATmega8515(L)
2512A–AVR–04/02
If used, the paritybit islocatedbetween the last data bit andfirst stopbit of a serial
frame.
USART Initialization The USART has to beinitializedbeforeanycommunication can take place. Theinitial-
ization process normally consists ofsetting the baudrate,setting frame format and
enabling thetransmitter or theReceiverdepending on theusage. For interrupt driven
USART operation, theglobal interrupt flag should be cleared(and interrupts globally dis-
abled) when doing theinitialization.
Before doing a re-initialization withchangedbaudrate orframe format,be surethat
thereare no ongoing transmissionsduring the period the registers are changed.The
TXC flag can beused to check that theTransmitterhascompleted all transfers, and the
RXC flag can beused to check that therearenounreaddatainthe receive buffer. Note
that theTXC flag must be clearedbeforeeach transmission (before UDRiswritten) if it
is usedfor thispurpose.
The following simple USART initialization codeexamplesshow one assembly and one
Cfunction that areequal in functionality.Theexamples assume asynchronous opera-
tion using polling (no interrupts enabled) and a fixedframe format. The baudrate is
given as a function parameter. For theassembly code, the baudrate parameter is
assumed to be stored in the r17:r16registers. When the function writes to the UCSRC
Register, the URSEL bit (MSB) must be set due to the sharing ofI/Olocation by UBRRH
and UCSRC.
Note: 1. Theexample codeassumes that the part specificheaderfileis included.
Moreadvanced initialization routinescan bemadethat include frame format asparame-
ters, disableinterrupts andso on. However, many applications useafixedsetting of the
baud andcontrolregisters, andfor thesetypes of applications theinitialization code can
Assembly Code Example
(1)
USART_Init:
;
Set baud rate
out UBRRH, r17
out UBRRL, r16
;
Enable receiver and transmitter
ldi r16, (1<<RXEN)|(1<<TXEN)
out UCSRB,r16
;
Set frame format: 8data, 2stop bit
ldi r16, (1<<URSEL)|(1<<USBS)|(3<<UCSZ0)
out UCSRC,r16
ret
CCode Example
(1)
void USART_Init( unsigned int baud )
{
/*
Set baud rate
*/
UBRRH = (unsigned char)(baud>>8);
UBRRL = (unsigned char)baud;
/*
Enable receiver and transmitter
*/
UCSRB = (1<<RXEN)|(1<<TXEN);
/*
Set frame format: 8data, 2stop bit
*/
UCSRC = (1<<URSEL)|(1<<USBS)|(3<<UCSZ0);
}
Kommentare zu diesen Handbüchern