(Code)
Line 49: Line 49:
 
UART_SetBaudRate(v_baudRate_u32);
 
UART_SetBaudRate(v_baudRate_u32);
 
}
 
}
 +
</syntaxhighlight>
 +
 +
The UART_SetBaudRate() function calcuates the values to the set for appropriate registers as below.
 +
<syntaxhighlight>
 +
void UART_SetBaudRate(uint32_t v_baudRate_u32)
 +
{
 +
uint16_t RegValue;
 +
 +
if((v_baudRate_u32 >= C_MinBaudRate_U32) && (v_baudRate_u32<=C_MaxBaudRate_U32))
 +
{
 +
/* Check if the requested baudate is within range,
 +
    If yes then calculate the value to be loaded into baud rate generator. */
 +
RegValue = M_GetBaudRateGeneratorValue(v_baudRate_u32);
 +
}
 +
else
 +
{
 +
/* Invalid baudrate requested, hence set it to default baudrate of 9600 */
 +
RegValue = M_GetBaudRateGeneratorValue(9600);
 +
}
 +
 +
UBRRL = util_ExtractByte0to8(RegValue);
 +
UBRRH = util_ExtractByte8to16(RegValue);
 +
}
 +
 
</syntaxhighlight>
 
</syntaxhighlight>

Revision as of 12:55, 25 March 2016

An embedded project/product without a Universal Asynchrounous Receiver Transmiter (UART) interface is unimaginable. Even if the MCU is not talking to another serial device, you'll need it at-least during the development work to speak to your computer. UART also come in handy for firmware upgrade and enabling/disabling product features during it's lifetime. This probably one of the first interfaces you would want to include in your project.
So let's get started! As with the series, we will be using a Atmega32 for this tutorial, other AVRs should work the same way. Do do not forget to look at the datasheet once before trying out with other controllers.

Basics

I know you're getting your head around the acronym UART still, but do not worry it is simple. Don't know where the word Universal came from but, Asynchronous makes lot of sense. Figure below shows the typical connection of a serial device with the MCU. There are basically Transmit(Tx) and Receive(Rx) lines and a common ground.

UART Block.png

Now imagine we want to send some data as shown below:

Data tx.png

The duration of a '1' or '0' is unknown to the receiving device. Hence it asynchronous. This is solved by agreeing upon a common data rate by both the devices, known as baud Rate.

You'll find typical baud rates to be 4800, 9600, 19200, 115200 etc. Atmega32 allows you to select the baud rate you wish. You need to look at the baud rate of the interfacing device to know it's default value. Typically most devices are set to 9600. Do not take my word for it, do check the datasheet.

Let us go ahead and configure the Atmega32 Hardware UART to communicate with the computer and send/receive data strings. 0 UART main.gif

Hook up

Atmega32 UART Configuration

Code

#include "uart.h"
int main() 
{
    UART_Init(9600);
 
    while(1)
    {
        UART_Printf("Welcome to AVR Serial Programming by ExploreEmbedded\n\r");
    }
    return (0);
}

Well, this looks cool isn't it? Just a one include uart.h and one UART_init(9600) function. Well it's dig a little deeper in the init function to see how it is set up.

void UART_Init(uint32_t v_baudRate_u32)
{
	UCSRB= (1<<RXEN) | (1<<TXEN);                  // Enable Receiver and Transmitter
	UCSRC= (1<<URSEL) | (1<<UCSZ1) | (1<<UCSZ0);   // Asynchronous mode 8-bit data and 1-stop bit
	UCSRA= 0x00;                                   // Clear the UASRT status register
	UART_SetBaudRate(v_baudRate_u32);
}

The UART_SetBaudRate() function calcuates the values to the set for appropriate registers as below.

void UART_SetBaudRate(uint32_t v_baudRate_u32)
{
	uint16_t RegValue;
 
	if((v_baudRate_u32 >= C_MinBaudRate_U32) && (v_baudRate_u32<=C_MaxBaudRate_U32))
	{
		/* Check if the requested baudate is within range,
	     If yes then calculate the value to be loaded into baud rate generator. */
		RegValue = M_GetBaudRateGeneratorValue(v_baudRate_u32);
	}
	else
	{
		/*	 Invalid baudrate requested, hence set it to default baudrate of 9600 */
		RegValue = M_GetBaudRateGeneratorValue(9600);
	}
 
	UBRRL = util_ExtractByte0to8(RegValue);
	UBRRH = util_ExtractByte8to16(RegValue);
}