UART of AVR
In the previous article, we are discussing Serial communication and it’s protocols. We are going to discuss the most basic one-the UART and USART of AVR and then we will move on to SPI and I2C communication.
USART and UART of AVR
If you like a greater insight between these two and differences between each of them, please visit here as I am not going to discuss much in this article. I would rather discuss the features of it and how to use it. Also, we are going to do it a bit different way. I am going to send the data from microcontroller to computer via UART and Asynchronous mode which is the very basic and yet, very useful and then, in the subsequent articles, we are going to send the data in different serial modes as well.
Please do note that I have an Arduino working as a USB to TTL converter to receive the data to the computer. If you guys don’t have it, you can easily buy it from this link. This is an affiliate link so you will be favoring us big time if you buy it from this link.
The UART of Atmega16/32
The Atmega-16/32 have UART with and it can be configured with the baud-rate of up-to 115200. It is very easy to send the data via UART. We just need to configure few registers that are there and then, we will be able to send the data. Here I am sending the data from the terminal of the computer to the controller and then receiving the same data from the controller to the computer.
Schematic Diagram
I have connected Rx of a microcontroller to Tx of Arduino and Tx of an Arduino to Rx of a controller. Also, please make sure that both of them has a common ground.
It’s looking like a dreadful but a very simple circuit. Just connect Rx of the controller to Tx of Arduino and Rx of Arduino to Tx of the controller. Make sure that they both have a common ground. If you are connecting it with the same computer, it will have a common ground. The third component is the USBasp programmer, used to program the controller.
The Registers
The registers we need to configure are:
UBRR(USART Baud Rate Register)
UCSRB
This register is used to enable transmission and reception in the controller. It also used to control the data length. You just have to enable the transmission and reception of the signal using this line-
UCSRC-USART Control and Status Register C – UCSRC
This register is used to specify the data length. Here we are selecting the 8 bt which can be done by setting UCSZ0 and UCSZ1 to high.
Now that we know everything setup, let’s program it.
The Program
The program is straightforward. I am receiving the character from the terminal and sending it back to the terminal. The receiving function receives the data. UDR register temporary holds the character. The transmission function has a character parameter which is transferring to the terminal. The while loop is the implementation of polling method. For the difference between polling and interrupt, please visit here.
#include <avr/io.h>
#include <util/delay.h>
#include <stdlib.h>
void USART_emit(char Data){
while((UCSRA &(1<<UDRE)) == 0);
UDR = Data;
}
int USART_Receive(){
while((UCSRA &(1<<RXC)) == 0);
return UDR;
}
void main (void)
{
unsigned char a;
DDRA=0xff;
UBRRL=51;
UBRRH=51<<8;
UCSRB|=(1<<TXEN)|(1<<RXEN)|(1<<RXCIE)|(1<<TXCIE); //setting the proper values
UCSRC|=(1<<URSEL)|(1<<UCSZ0)|(1<<UCSZ1); //data length is 8 bit.
while(1)
{
a=USART_Receive(); //saving the received data in a variable
USART_emit(a); //Transmtting the recieved data back to the port
a=0;
}
}
Burn the above program in your controller.
Please do note that I have implemented serial communication in the polling method approach. In subsequent posts, we are going to implement the interrupt method. To know the difference between polling and method, click here.
Execution of the Program
The program runs fine. It’s echoing back the data that you send from the computer. I have used a terminal software to do this. You can use Arduino’s inbuilt terminal software or you can use any other terminal software as well. Please make sure that you have connected to the right com port. When you connect your Arduino to the computer, there will be a creation of COM PORT. You an check that iin the device manager of your computer as well/. Select the same comport in the terminal software.
Me and my neighbor were just preparing to do a little research about this. We got a grab a book from our local library but I think I learned more clear from this post. I’m very glad to see such fantastic info being shared freely out there.