6 data transmission - the usart transmitter, 1 sending frames with 5 to 8 data bit – Rainbow Electronics ATmega128RFA1 User Manual
Page 345

345
8266A-MCU Wireless-12/09
ATmega128RFA1
C Code Example
(1)
#define FOSC 8000000// Clock Speed
#define BAUD 9600
#define (MYUBRR FOSC/16/BAUD-1)
void main( void )
{...
USART_Init ( MYUBRR );
...} // main
void USART_Init( unsigned int ubrr){
/* Set baud rate */
UBRRnH = (unsigned char)(ubrr>>8);
UBRRnL = (unsigned char) ubrr;
/* Enable receiver and transmitter */
UCSRnB = (1< /* Set frame format: 8data, 2stop bit */ UCSRnC = (1< } // USART_Init Note: "About Code Examples" on page 7 More advanced initialization routines can be made that include frame format as 23.6 Data Transmission – The USART Transmitter The USART transmitter is enabled by setting the Transmit Enable (TXEN) bit in the 23.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 The following code examples show a simple USART transmit function based on polling
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.
UCSRnB register. When the transmitter is enabled, the normal port operation of the
TxDn pin is overridden by the USART and gives the function 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 XCKn pin
will be overridden and used as transmission clock.
transmitted. The CPU can load the transmit buffer by writing to the UDRn 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 the shift register is loaded with new data, it will transfer one
complete frame at the rate given by the baud rate register, U2Xn bit or by XCKn
depending on mode of operation.
of the Data Register Empty Flag (UDREn). When using frames with less than eight bits,
the most significant bits written to the UDRn are ignored. The USART has to be
initialized before the function can be used. For the assembly code, the data to be sent
is assumed to be stored in register r16.