179
AT90PWM216/316 [DATASHEET]
7710H–AVR–07/2013
Note:
1. The example code assumes that the part specific header file is included.
For I/O Registers located in extended I/O map, “IN”, “OUT”, “SBIS”, “SBIC”, “CBI”, and “SBI” instructions must be
replaced with instructions that allow access to extended I/O. Typically “LDS” and “STS” combined with “SBRS”,
“SBRC”, “SBR”, and “CBR”.
More advanced initialization routines can be made that include frame format as parameters, 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.
17.6
Data Transmission – USART Transmitter
The USART Transmitter is enabled by setting the Transmit Enable (TXEN) bit in the UCSRB Register. When the
Transmitter is enabled, the normal port operation of the TxDn pin is overridden by the USART and given the func-
tion as the Transmitter’s serial output. The baud rate, mode of operation and frame format must be set up once
before doing any transmissions. If synchronous operation is used, the clock on the XCK pin will be overridden and
used as transmission clock.
17.6.1
Sending Frames with 5 to 8 Data Bit
A data transmission is initiated by loading the transmit buffer with the data to be transmitted. The CPU can load the
transmit buffer by writing to the UDR I/O location. The buffered data in the transmit buffer will be moved to the Shift
Register when the Shift Register is ready to send a new frame. The Shift Register is loaded with new data if it is in
idle state (no ongoing transmission) or immediately after the last stop bit of the previous frame is transmitted. When
Assembly Code Example(1)
USART_Init:
; Set baud rate
sts
UBRRH, r17
sts
UBRRL, r16
; Set frame format: 8data, no parity & 2 stop bits
ldi
r16, (0<<UMSEL)|(0<<UPM0)|(1<<USBS)|(3<<UCSZ0)
sts
UCSRC,r16
; Enable receiver and transmitter
ldi
r16, (1<<RXEN0)|(1<<TXEN0)
sts
UCSRB,r16
ret
C Code Example
(1)
void
USART_Init( unsigned int baud )
{
/* Set baud rate */
UBRRH = (unsigned char)(baud>>8);
UBRRL = (unsigned char)baud;
/* Set frame format: 8data, no parity & 2 stop bits */
UCSRC = (0<<UMSEL)|(0<<UPM0)|(1<<USBS)|(3<<UCSZ0);
/* Enable receiver and transmitter */
UCSRB = (1<<RXEN0)|(1<<TXEN0);
}