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 !!!