Skinny Satan

A cool Nerd To Hangout With

Microcontrollers 101: UART/USART

As the title says, we are covering USART this time. USART stands for Universal Synchronous Asynchronous Reception Transmission. It is a kind of A serial tx/rx protocol. Serial transmission protocol is a necessity to establish a communication between two units which can be from a computer to any other computer. This protocol is in two modes.
1.Synchronous mode
2.Asynchronous mode.

serialdata

In synchronous mode we synchronize the clock between both the systems by the use of clock signal, but in asynchronous operation it is not necessary as we match the baud rate only. But Asynchronous mode is more Prune to the errors thus cant be used for higher data rates, but still as it reduces the hardware and is simpler UART i.e. Asynchronous mode is used over USART i.e. the synchronous mode.

We will be covering this tutorial again on our AVR platform.
Lets consider a scenario where we are supposed to establish a connection between to micro controllers.To communicate, we need to consider three pins,
1.Receiver pin (RXD)
2.Transmitter pin (TXD)
3.Serial Clock (XcK)

Let us first see which Register are supposed to be set to Set the Baud rate.
UBRR is a 12 bit Register which is in two parts UBRRL and UBRRH. And then there is this U2X pin UCSRA register that is Control and status register A.

Here are the formulae to obtain the required baud rate.

Baudrate

The data is set through a set of frames, here is a framing formats of the data.
• 1 start bit
• 5, 6, 7, 8, or 9 data bits
• no, even or odd parity bit
• 1 or 2 stop bits

Here is a list of registers, you need to check before doing UART.

Uart_Registers

Now, UCSRA is register is only about flags (except U2X), so we will see how we can use these flags later. UCSRB is the register to enable transmitter,receiver, and their respective Interrupts. And UCSRC is for actually initiating the UART protocol.

Lets Initialize UART now.

void uart_init()
{
UCRSA |= (1<<U2X); //Doubling the Data rate.
UCSRB |= (1<<RXEN) | (1<<TXEN);//enable both way communication
UCSRC |= (1<<UCSZ1) | (1<<UCSZ0); //8bit uart no parity 1 stop bit
UBRRL = 12; // 9600 baud rate
}

that’s all you need to actually initialize the UART.

Now here are the below functions to Transmit and receive the characters.

void uart_tx(char data)
{
	while(!(UCSRA & (1<<UDRE)));//Wait till buffer is emptied

	UDR=data; //Transmit
}
char uart_rx()
{
	while(!(UCSRA & (1<<RXC))): // Wait till receiving is done.

	return UDR;	//receive.

}

As you can see above for transmitting and receiving There us this same UDR buffer register. One for transmission and another for reception.
To transmit the Data First we wait till the buffer is emptied by checking the UDRE bit in UCSRA register and as soon as it is emptied add the data in buffer to transmit it.
For reception we wait till the Data is fully received in the buffer by checking the RXC flag in UCSRA register and as it is done, UDR is read for received data.

So now you know how to initialize, transmit and receive. Use these to test and try implementing your own framing format and protocols using these, Use it in your projects, in wireless Link establishments. Possibilities are infinite. Use Wisely.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Information

This entry was posted on April 26, 2014 by in Tutorial and tagged , , , .
%d bloggers like this: