Sunday, January 5, 2014

Tutorial 5: Serial Port Communication with ATmega series

NAME :
Serial communication between PC and uC.

Application :
This can be used to input parameters/constants/dynamic values to micro-controller via serial port of PC.

Requirements and components:
We need few basic parts to start with serial communication (Hardware & software requirements)
  1. DB 9 connector (known as serial connector)
  2. MAX 232 IC
  3. Development board.
  4. Some wires and Pin connectors.
  5. PC with serial port.(COM port)
  6. Hyper-terminal or Putty (Serial supported software)
Verify the PC for COM port:

Before we begin the serial communication we need to verify that PC has COM port working on it.
Please follow the steps given below to verify the COM port :

  1. Right click on the my computer and select Manage
  2. This will open the Computer management screen, now click on the "Device Manager" left hand side and click in Ports(COM & LPT) on right hand side.
  3. You must see the COM1 listed under Ports(COM & LPT). Refer the screen shot below.

Fig 1 : COM1 port available under Ports(COM & LPT)

Connecting RS232 to microcontroller:

Do the connections as shown in the diagram below for connecting uC with MAX 232.


Prepare Serial cable:

We need  to prepare a serial cable to connect the PC with the MAX 232 PCB. Make a cable as shown in diagram :

Program for serial communication :

OK !!!
It't time to write the program in CodeVisionAVR.

Start the CodeVisionAVR and run the CodeWizardAVR.
(For more details on CodeVisionAVR pleas refer this tutorial)

Step 1 : Select ATmega16 and set the clock to correct value.


Step 2 : Select the USART tab in CodeWizard,
Check the "Receiver" and "Transmitter" check boxes.
Select "Baud Rate" to 9600
Select "Communication Parameters" to "8 Data, 1 Stop, No Parity"
Select "Mode" as "Asynchronous".


Step 3 : Generate, Save and Exit.
Now generate save and exit the CodeWizard.
Note : This will ask you to save 3 files. All three file must have same name CodeVision will select the file extension automatically.
Here is the example of 3 file in this pgrogram.
  1. Serial_Communication.c
  2. Serial_Communication.cwp
  3. Serial_Communication.prj
Step 4 : Write program statements:

CodeAWizard will create the large program as per our settings. 

Add "delay.h" header file just below "stdio.h" header file.
#include <delay.h>

Now scroll down and search for the line "// Place your code here"
Add following 2 line after "// Place your code here".

printf("Welcome to AVR\n\r");
delay_ms(1000);

1st line will send the "Welcome to  AVR" on terminal.
2nd line just adds delay between to printf cmds.

Step 5 : Built the program to generate the .HEX file.
Press "Ctrl + F9" to built the program, this will bring the following screen.
check for the errors and warnings, correct the error if there is any.


In this program there are no errors and warnings.
Click "OK".
This will create the .HEX file at ""C:\Program Files\cvavr2\bin\exe" (Default path)

Step 6 : Burn the program to microcontroller.
Use PonyProg to burn the .Hex file to uC.
(Refer this tutorial for more details on PonyProg)

Step 7 : Now connect the cable to PC serial port and to uC board.
Enjoy !!!

4 comments:

  1. Replies
    1. Hello Aviraje,

      I'll share code shortly.

      Thanks,
      Kaustubh Ghaisas.

      Delete
    2. Hello Aviraje,

      Firstly sorry for delayed reply.

      As I understand, you are trying to code with WinAVR or AVR studio.
      Please download 3 PDF files from following drop-box link.

      https://www.dropbox.com/sh/g9nyv0owfq2569m/AADwK3le5M57r3e5WZmzgzqNa?dl=0

      Kindly note that, I've LEDs connected to PORTB and UART connected to PC.
      Now, when you enter a digit on HyperTerminal corresponding digit will turn on LEDs
      e.g if you enter 9 on HyperTerminal means 0000 1001 (in BIN), this will turn on LEDs connected to PB0 and PB3.

      I hope this will help you.

      Regards,
      Kaustubh Ghaisas.

      Delete
  2. hi this is my code but not receiving anything.
    i am checking on proteus plz help me


    #define F_CPU 8000000UL

    #include
    #include
    #include
    #include
    #include
    #include "hex 2 ascii.h"

    #asm
    .equ __lcd_port=0x1b ;PORTC
    #endasm

    #define triger_echo 3
    #define RXEN 4
    #define TXEN 3
    #define UCSZ0 1
    #define UCSZ1 2
    #define URSEL 7
    #define UDRE 5
    #define RXC 7
    unsigned char c;
    unsigned char i,message[]="i love india\r\n";
    unsigned char j=0,message1[20];
    //unsigned int j=0;
    void uart_init()
    { unsigned int ubrr=51;
    //*****SET BAUD RATE *****************//

    UBRRH=(unsigned char) ubrr >> 8;
    UBRRL=(unsigned char) ubrr;


    /******TO ENABLE TX & RX ***********/

    UCSRB|=(1 << RXEN | 1 << TXEN);

    // *************8 bit data, 2 stop bit***********//

    UCSRC|= 1<< URSEL| 1<<UCSZ0|1<<UCSZ1;


    }



    void uart_transmit(unsigned char data)
    {
    // *********** wait for empty tx buffer ***********//
    while(!(UCSRA & (1 << UDRE)));


    //********PUT DATA INTO BUFFER**********//
    UDR=data;
    // UCSRA&=~(1<<UDRE);

    }


    unsigned char uart_receive()
    {
    //***wait for data to be read******//

    while(!(UCSRA & (1 << RXC)));


    //*****get and return data from buffer******//

    return UDR;
    }

    unsigned char UART_rx()
    {
    while(!(UCSRA & (1<<RXC)));
    return UDR;
    }

    void main()
    {

    DDRA=0XFF;
    DDRD=0X00;
    PORTD=0XFF;
    PORTA=0X00;

    lcd_init(16);
    uart_init();

    // go on the second LCD line
    lcd_gotoxy(0,0);

    // display the message
    lcd_putsf("Radar");

    lcd_gotoxy(0,1);

    lcd_putsf("Distance");

    delay_ms(1000);
    // stop here
    lcd_clear();

    for(i=0;message[i];i++)
    {
    uart_transmit(message[i]);
    }


    message1[j]=uart_receive();

    lcd_putchar(message1[j]);
    _lcd_write_data(message1[j]);
    delay_ms(1000);
    uart_transmit(message1[j]);
    j++;



    }








    ReplyDelete