151
ATmega8535(L)
2502K–AVR–10/06
The following simple USART initialization code examples show one assembly and one
C function that are equal in functionality. The examples assume asynchronous opera-
tion using polling (no interrupts enabled) and a fixed frame format. The baud rate is
given as a function parameter. For the assembly code, the baud rate parameter is
assumed to be stored in the r17:r16 registers. When the function writes to the UCSRC
Register, the URSEL bit (MSB) must be set due to the sharing of I/O location by UBRRH
and UCSRC.
Note:
1. See “About Code Examples” on page 7.
More advanced initialization routines can be made that include frame format as parame-
ters, disable interrupts and so on. However, many applications use a fixed setting of the
baud and control registers, and for these types of applications the initialization code can
be placed directly in the main routine or be combined with initialization code for other I/O
modules.
Assembly Code Example
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
C Code Example
#define FOSC 1843200// Clock Speed
#define BAUD 9600
#define MYUBRR FOSC/16/BAUD-1
void
main(
void
)
{
...
USART_INIT ( MYUBRR );
...
}
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<<USBS)|(3<<UCSZ0);
}
Содержание ATmega8535
Страница 314: ...314 ATmega8535 L 2502K AVR 10 06 ...
Страница 320: ...vi ATmega8535 L 2502K AVR 10 06 ...