32 ATMEL AVR MICROCONTROLLER PRIMER: PROGRAMMING AND INTERFACING
UCSRC. This allows the user to customize the data fea tures to the a pplication at hand. It
should be emphasize d that both the transmitter and receiver be configured with the same data
features for proper data transmission. The UCSRC contains the following bits:
USART mode select (UMSEL): 0, asynchronous operation; 1, synchronous operation
USART parity mode (UPM[1:0]): 00, no parity; 10, even parity; 11, odd parity
USART stop bit select (USBS): 0, one stop bit; 1, two stop bits
USART character s ize (data width) (UCSZ[2:0]): 000, 5 bits; 001, 6 bits; 010; 7 bits; 011,
8 bits; 111, 9 bits
2.3.2 System Operation and Programming
The basic activities of the USART system consist of initialization, transmission, and reception.
These activities are summarized in Figure 2.4. Both the transmitter and receiver must be initialized
with the same communication parameters f o r proper data transmissi on. The transmiss i on and
reception activities are similar except for the direction of data flow. In transmission, we monitor for
the UDRE flag to set, indicating the data register is empty. We then load the d a ta for transmission
into the UDR registe r. For rece ption, we monitor for the RXC bi t to set, indicating there are
unread data in the UDR register. We then re tri eve the data from the UDR register.
Set USART
communication parameters
(data bits, stop bit, parity)
Turn on transmitter
and/or receiver
Set USART for
asynchronous mode
Set Baud Rate
b) USART initialization
yes
no
Has UDRE
flag set?
Load UDR register with
data byte for transmission
a) USART transmission
yes
no
Has RXC
flag set?
Retrieve received data
from UDR register
c) USART reception
FIGURE 2.4: USART activities.
SERIAL COMMUNICATIONSUBSYSTEM 33
To program the USART, we implement the flow diagrams provided in Figure 2.4.Inthe
sample code provided, we assume the ATmega16 is operating at 10 MHz and we desire a baud rate
of 9600, asynchronous operation, no parity, o ne s top bit, and eight data bits.
To achieve 9600 baud with an operating frequency of 10 MHz requires that we set the
UBRR registe rs to 64, which i s 0x40.
//*************************************************************
//USART_init: initializes the USART system
//*************************************************************
void USART_init(void)
{
UCSRA = 0x00; //control register initialization
UCSRB = 0x08; //enable transmitter
UCSRC = 0x86; //async, no parity, 1 stop bit,
//8 data bits
//Baud Rate initialization
UBRRH = 0x00;
UBRRL = 0x40;
}
//*************************************************************
//USART_transmit: transmits single byte of data
//*************************************************************
void USART_transmit(unsigned char data)
{
while((UCSRA & 0x20)==0x00) //wait for UDRE flag
{
;
}
UDR = data; //load data to UDR for transmission
}

Get Atmel AVR Microcontroller Primer now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.