beautypg.com

2 sending frames with 9 data bit, Atmega128rfa1 – Rainbow Electronics ATmega128RFA1 User Manual

Page 346

background image

346



8266A-MCU Wireless-12/09

ATmega128RFA1

Assembly Code Example

(1)

USART_Transmit:

; Wait for empty transmit buffer

sbis UCSRnA,UDREn rjmp USART_Transmit

; Put data (r16) into buffer, sends the data

out UDRn,r16

ret

C Code Example

(1)

void USART_Transmit( unsigned char data )

{

/* Wait for empty transmit buffer */

while ( !( UCSRnA & (1<

/* Put data into buffer, sends the data */

UDRn = data;

}

Note:

1. See

"About Code Examples" on page 7

The function simply waits for the transmit buffer to be empty by checking the UDREn
flag, before loading it with new data to be transmitted. If the data register empty
interrupt is utilized, the interrupt routine writes the data into the buffer.

23.6.2 Sending Frames with 9 Data Bit

If 9 bit characters are used (UCSZn2:0 = 7), the ninth bit must be written to the TXB8 bit
in UCSRnB before the low byte of the character is written to UDRn. The following code
examples show a transmit function that handles 9 bit characters. For the assembly
code, the data to be sent is assumed to be stored in registers r17:r16.

Assembly Code Example

(1)(2)

USART_Transmit:

; Wait for empty transmit buffer

sbis UCSRnA,UDREn

rjmp USART_Transmit

; Copy 9th bit from r17 to TXB8

cbi UCSRnB,TXB8

sbrc r17,0

sbi UCSRnB,TXB8

; Put LSB data (r16) into buffer, sends the data

out UDRn,r16

ret