UART:        (MEGA163)

The minimum requirements to use the serial port are as follows:

            1.    Load the values for the Baud Rate into UBRR and UBRRHI.

            2.    Enable the Transmitter and / or Receiver in the UCSRB register.

These two steps will allow the programmer to include the Standard I/O header and to use the standard library functions such as printf, putchar, getchar, etc.    <stdio.h>

Alternatively, the programmer can monitor the RCX and TCX bits in the UCSRA register to determine when a character has been received or transmitted.  The UDR register contains the data to be transmitted or the data that has been received.


UART Baud Rate Registers - UBRR and UBRRHI

This is a 12-bit register which contains the UART Baud Rate.

Bit

15 14 13 12 11 10 9 8
        MSB     LSB UBRRHI

MSB             LSB

UBRR

Bit

7 6 5 4 3 2 1 0
Read/Write R R R R R/W R/W R/W R/W
  R/W R/W R/W R/W R/W R/W R/W R/W  
Initial Value 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0

The value for UBRR and UBRRHI can be selected from the tables or calculated with the equations located below.  Both the equations and the tables use the value of UBR.  UBR is equal to UBRRHI and UBRR where UBRRHI is the upper four-bits and UBRR is the lower eight-bits of the 12-bit number represented by UBR.

Equations to Calculate the BAUD Rate.

BAUD = Fclk / ( 16 * (UBR + 1) )

    or re-written

UBR = (Fclk / (BAUD * 16)) - 1

( Examples of Calculating Baud Rate Values )


UART Control and Status Register B - UCSRB

Bit

7 6 5 4 3 2 1 0  

 

RXCIE TXCIE UDRIE RXEN TXEN CHR9 RXB8 TXB8

UCSRB

Read/Write R/W R/W R/W R/W R/W R/W R W  
Initial Value 0 0 0 0 0 0 1 0  

Bit 4 - RXEN:  Receiver Enable

This bit enables the UART receiver when set (one).

Bit 3 - TXEN:  Transmitter Enable

This bit enables the UART transmitter when set (one).

( More on the UART Control and Status Register B )


UART Control and Status Register A - UCSRA

Bit

7 6 5 4 3 2 1 0  

 

RXC TXC UDRE FE OR - U2X MPCM

UCSRA

Read/Write R R/W R R R R R/W R/W  
Initial Value 0 0 0 0 0 0 0 0  

Bit 7 - RXC:  UART Receive Complete

        This bit is set when a received character is transferred from the Receiver Shift register to UDR.

Bit 6 - TXC:  UART Transmit Complete

        This bit is set when the entire character in the Transmit Shift register has been shifted out.

( More on the UART Control and Status Register A )


UART I/O Data Register - UDR

Bit

7 6 5 4 3 2 1 0  

 

MSB             LSB

UDR

Read/Write R/W R/W R/W R/W R/W R/W R/W R/W  
Initial Value 0 0 0 0 0 0 0 0  

The UDR register is actually two physically separate registers sharing the same I/O address.  When writing to the register, the UART Transmit Data register is written.  When reading from the register, the UART Receive Data register is read.


Usage:

UBRR = 25;                                 // set for 9600 BAUD with 4MHz Clock

UBRRHI = 0;

 

UCSRB = 0x18;                          // enable transmitter and receiver

if ( (UCSRA & 0x80) != 0 );      // Received a character...

ch = UDR;                                     // retrieve character from serial register

 


Example:

 

// setup the UART for serial communications
// by having the compiler calculate the values


void init_UART(void)
{
#define XTAL 6000000L
#define BAUD 9600

unsigned int UBR;                                                                 // 16 bit variable to hold serial port calculations

UBR = XTAL / 16 / BAUD - 1;                                              // calculate the load values
UBRR = (unsigned char)(UBR & 0xFF);                        // load the lower 8 bits
UBRRHI = (unsigned char)(UBR >> 8);                         // load the upper bits
UCSRB = 0x18;                                                                     // enable transmit and receive
}