Quantcast
Viewing all articles
Browse latest Browse all 215

AVR Serial Communication (UART) Programming tutorial

Image may be NSFW.
Clik here to view.
UART-serial-communication-tutorial-AVR
Serial Communication tutorial
This tutorial focuses to teach you how to program Serial Communication (UART) in AVR Microcontrollers. UART plays an important role in almost every embedded applications which we see in our day to life and hence it was considered to be very important concept in every Microcontroller.

The above design demonstrates the usage of UART to send and receive data via hyperterminal as well display the received data in 1 16x2 LCD. As we all know Microcontroller works in TTL logic which is not compatible with the PC so we have to employ a level converter IC MAX232, read more about the working of IC MAX232.

REGISTERS USED:

In AVR there are five registers which are meant to use for Serial Communication such as UDR, UBBR , UCSRA, UCSRB, UCSRC. Lets see the functions of these registers briefly.

UDR:

Image may be NSFW.
Clik here to view.
UDR-register-display
UDR Register
UDR or USART Data Register is meant for writing and receiving the data through the UART. In this there are two shift registers referred to as Transmit Shift register and Receive Shift register with each having a separate buffer register. When the data is written to UDR it will be transferred to Transmit Data buffer register and when we read the contents of the Receive Data buffer register is returned.

UBBR:

In AVR the baud rate of the UART is programmable and it is achieved by means of this UBBR register. It is 16 bit register classified into lower UBBRL and higher UBBRH out of which 12 bit is usable The formula governing the relation between the value of UBBR and Oscillator is 
Baud Rate = (Oscillator Frequency / (16( UBBR Value +1))

So for a 8MHz oscillator frequency and 9600 baud rate the value need to be loaded in the UBBR will be 

UBBR = (8MHZ /16(9600))-1
         =(500KHz/ 9600) - 1
                 = 51 ( equivalent hex 33)

UCSRA:

Image may be NSFW.
Clik here to view.
UCSRA-regsiter-UART-communication
UCSRA Register

UCSRB:

Image may be NSFW.
Clik here to view.
UCSRB-register-UART-AVR
UCSRB Register

UCSRC Register:

Image may be NSFW.
Clik here to view.
UCSRC-register-UART-AVR
UCSRC Register

STEPS TO PROGRAM UART:

  1. Load the hex value in the UBRR Register for the Baud rate you are about to use.
  2. Set the bits in the registers UCSRA, UCSRB & UCSRC based on your usage requirement.
  3. For Transmission Place the data in the UDR register and check for the appropriate flag to set in the UCSRA regsiter
  4. Clear the Flag for further transmission.
  5. For receiving the data, wait for the Receive flag to set in the UCSRA register and then read the UDR register to obtain the received data for processing or display.
  6. Clear the Flag for further data reception.

CODE:

The below code is built using AVR studio software. This code demonstrates both transmission and receiving the data. Initially controller send data "BGN" to the hyperterminal and then prepares itself to receive data which is made to print in a 16x2 LCD.

  1. #include<avr/io.h>
  2. #define F_CPU 8000000UL
  3. #include<util/delay.h>
  4. void display(char a,int b);     //LCD display routine
  5. void check();
  6. char c;
  7. int i,cursor;
  8. int main(void)
  9. {
  10.   DDRA=0xff;                      //Setting Directions
  11.   DDRB=0x03;
  12.   display(0x38,0);
  13.   display(0x0f,0);
  14.   display(0x80,0);
  15.   UBRRL=0x33;                 //Hex value for Baud rate 9600
  16.   UCSRB=0x18;                //Initialization of UCSRB reg
  17.   UCSRC=0x06;                //Initialization of UCSRC reg
  18.   while(!(UCSRA&(1<<5)));    //Flag Check
  19.   UDR='B';                            //Data write in UDR
  20.   UCSRA&=~(1<<5);             //Clearing Flag
  21.   while(!(UCSRA&(1<<5)));
  22.   UDR='G';
  23.   UCSRA&=~(1<<5);
  24.   while(!(UCSRA&(1<<5)));
  25.   UDR='N';
  26.   UCSRA&=~(1<<5);

  27.    while(1)
  28.    {
  29.      while(!(UCSRA&(1<<7)));   //Flag check for receive
  30.      c=UDR;                             //Data Read
  31.      display(c,1);                      //Displaying received data in LCD
  32.      UCSRA&=~(1<<7); 
  33.      check();
  34.    }
  35.  }

  36. void display(char a,int b)       //LCD routine
  37.  {
  38.   PORTA=a;
  39.   if(b==0)
  40.    {
  41.      PORTB&=~(1<<0);
  42.    }
  43.   else 
  44.    {
  45.      PORTB|=(1<<0);
  46.      cursor++;
  47.     }
  48.   PORTB|=(1<<1);
  49.   _delay_ms(10);
  50.   PORTB&=~(1<<1);
  51.   _delay_ms(10);
  52.  }

  53. void check()                   //Routine to print data continuously 
  54.  {
  55.    if(cursor==16)
  56.    {
  57.     display(0xc0,0);       //Jumps second row after reaching end of first row
  58.    }
  59.    else if(cursor==32)
  60.    {
  61.      display(0x80,0);    //Jumps first row after reaching end of second row
  62.      cursor=1;
  63.    }
  64.  }

Like this tutorial? Share this with others through social sites and do not forget to follow us there for more of these. Post your comments and queries below, we will get back to you as soon as possible.

Image may be NSFW.
Clik here to view.

Viewing all articles
Browse latest Browse all 215

Trending Articles