Serial communication is a transfer of data from one device to other bit by bit. I know It was difficult to understand. Let’s just keep it very simple. The word ‘communication’ is present here which is telling us someone is going to communicate with other through some means. Here, ‘someone’ may be a computer,a microcontroller, an Arduino or may be your cell phone as well. So, basically serial communication is a communication medium between two electronics devices. There are lot of other ‘communication medium’ as well like parallel communication, i2c, SPI etc. The wireless examples are Bluetooth, WIFI, RF, GSMetc. The main advantage of using ‘serial communication’ is, it uses only two wires for sending and receiving data but the speed of this communication is slow as compared to other techniques.
Serial Communication of Atmel AVR Microcontrollers:
Now, how to enable the serial communication of Atmel AVR Microcontrollers? USART (Universal Synchronous and Asynchronous Receiver and Transmitter) or UART of Atmel AVR is a device (or registers) to do serial communication. So, basically if you can handle UART or USART registers of Atmel AVR Microcontroller then you can communicate your microcontroller with any other electronics device.
What is BAUD Rate?
First of all you should know about the concept of Baudrate. It is basically a speed of USART that is bits per second transmitting or receiving through serial communication. Let’s understand it in a easy way. For example, if you only know French language and the person you want to talk only speaks German. Then you both persons will not able to communicate properly. His words will be rubbish for you and your words will be rubbish for him. To communicate properly both persons will come to common language that might be English. In the same way during serial communication Baud Rate is just like a language. Both devices should have ‘same Baud rate’ while communicating.
How to set Baud Rate in Atmel AVR Microcontrollers?
There are 6 registers of UART/USART is present in the Atmel AVR microcontrollers for serial communication. In which 2 registers are to set the Baud Rate and the remaining 4 registers handle other stuff of UART. Let’s start with the Baud Rate registers.
UBRRH and UBRRL are two registers which are used to set the Baud Rate in Atmel AVR Microcontrollers. There is a formula to get the UBRR register value. Look in the table below:
Just look at the Asynchronous normal mode formulas.
BAUD = Baud rate (in bits per second, bps)
fosc = System Oscillator clock frequency
UBRR = Contents of the UBRRH and UBRRL Registers, (0 – 4095)
For example if we have 12Mhz system frequency of microcontroller and want to set 9600 Baud Rate Value then,
Then according to above given formula:
UBRR = (12000000/(16×9600)) – 1
UBRR = 77.125
Now I got the value 77.125 which is quite near to 77. So I will select 77 UBRR value and in hexa you may be write as:
Character size settings:
When you are talking with someone you have to make pauses during conversation so that other person can easily understand you. Like a full stop in a writing. If I don’t use full stops then you will not be able to understand what I am saying right now. So, in the same way we have to set character size of communication. Both devices which are communicating should have same character size as well. Normally the characters we use in our coding have size of 8 bits. So, in the following example I will set 8 bit character size.
UCSRC register is used for setting the character size. Let’s have a inner look of the register.
UCSRC=(1<<URSEL) | (0<<UMSEL) | (0<<UPM1) | (0<<UPM0) | (0<<USBS) | (1<<UCSZ1) | (1<<UCSZ0) | (0<<UCPOL);
In every pair of bracket, there is a bit of UCSRC register. For character size setting,we just have to set ‘UCSZ0’ and ‘UCSZ1’ bits. Look on the table below:
For 8 bit, I gave ‘1’ to both bits. There is a URSEL bit present here which selects between USCRC Register and UBRR Register. ‘0’ to select UBRR register and ‘1’ to select UCSRC register. It is a good way to set UBRR register value first and after that give ‘1’ to URSEL register for setting the character size. In simple serial communication, the remaining bits of UCSRC have no use.
UCSRB=(0<<RXCIE) | (0<<TXCIE) | (0<<UDRIE) | (1<<RXEN) | (1<<TXEN) | (0<<UCSZ2) | (0<<RXB8) | (0<<TXB8);
UCSRB (USART Control and Status Register B) register is used for enabling the serial communication i.e. Tx and Rx pin will ready to send or receive the data at Tx and Rx pins of microcontroller. RXEN bit will enable the receiving and TXEN bit will enable the transmitting of data. If these bits are enable, Tx & Rx pins of Atmel AVR Microcontrollers will no longer work as simple input or output pin.
I did not include the UCSRA register because in normal serial communication receiving and transmitting, it has no use. To study more about it read the datasheet of Atmel AVR Microcontrollers. The last register of USART device is UDR (USART Data Register) . It is basically data register for the holding of data during communication.
Now, I am going to illustrate an example to show the use of USART device. This code is written on code vision for ATmega16.
Connect ATMega16 with virtual terminal as shown below:
Virtual terminal work same as hyper terminal or putty serial communication softwares in real.
What is Virtual Terminal?
A terminal is a software which handles the serial communication of computer. As UART controls the serial communication in microcontrollers. Same as, a terminal controls the serial communication in computer. If you want to communicate your microcontroller with a computer then you need some terminal software. Software like Hyper terminal, Putty or may be Serial Monitor of Arduino. In the above example that hyper terminal is just like a terminal software present in a computer. You may say that in above example you are learning how to communicate your microcontroller with a computer. If you want to learn how to communicate your computer in real systems then click the link below:
Now come back to simultion example.The settings of Micro controller must be like this:
Copy the following code in your compiler, run it and check the results in the Proteus software. Every time you write some character on virtual terminal it must print that character again three times. Once this code will run properly, change something in code and check the results.
//Code for Codevision
#define F_CPU 12000000UL
unsigned char data;
//Function for character receiving on Rx pin
unsigned char uart_receive ()
while((UCSRA&(1<<RXC)) == 0);
// wait untill 8-bit of a character receive
//Functions transmit data
void uart_transmit (unsigned char data)
while (!( UCSRA & (1<<UDRE)));
// wait for the register to free from data
UDR = data;
// load data in the register
UBRRL=0x4d; // To set the Buad rate to 9600.. get value through above formula
UCSRB=(1<<RXCIE) | (1<<RXEN) | (1<<TXEN);
UCSRC=(1<<URSEL) | (1<<UCSZ1) | (1<<UCSZ0) ;
data=uart_receive(); // MC will wait here infinitly to get a 8-bit character
//the functions below will print the every character three times
To learn more about the micro-controllers stay tuned with us. If you need any help feel free to comment below 🙂