interfacing AVR with SERIAL PORT OF COMPUTER This tutorial is taken from the book
MY EXPERIENCE IN PROGRAMMING AVR MICROCONTROLLER
IN C USING WINAVR/AVRGCC.
you can download it free from the files
section(FILE-AVRBOOK.RAR) of yahoo group-booksbybibinhttp://groups.yahoo.com/group/booksbybibin/
comments about this book on
www.booksbybibin.blogspot.comSERIAL PORT OF COMPUTERBIOS SETTINGS FOR SERIAL PORT
First set the bios then only do any other operations like programming the
microcontroller and serial port. See the Port numbers correctly. Then only use the
COM1, COM2 or lpt1,lpt2 repectively. This is the most important step. AVRDUDE
programmer sometimes won't work for some parallel port. It does not worked for
INTEL965GV motherboard. So see my previous book on parallel port before doing
these steps.
SERIAL PORT INTERFACING WITH COMPUTER
For interfacing AVR with serial port of cable you
require a serial port cable and max232 or HIN232(level converter). RS-232 of the
computer has LOGIC HIGH in the range of -3 to -25V( mostly around -8.5V) and
LOGIC LOW around 3 to 25V (mostly 8.5V). We had to convert it to LOGIC
HIGH(5V) and LOGIC LOW(0V) of microcontroller, so we use max232 or you can use
any other level converters. Next is to get a serial port cable,if you are not getting a
serial port cable, then buy a serial mouse costs around Rs.70 and take the cable from it.
But mouse serial port cable has only 4 pins Rx,Tx,DTS,GND, so it can't be used for
programmer's of some microcontrollers. But in our case we require only 3
pins(Rx,Tx,GND). So it is better to go for a serial cable, I used one that of mouse.
Figure shows the connections with serial port of computer. You can use a 1uF
capacitor instead of 10uF.
HYPER TERMINAL
Hyper Terminal is used to see the serial port data. It
will show only the received data. All the data is show in ASCII. Go to
Start_Programs_Accessories_Communicatin_Hyper Terminal. If you are using serial
port for your internet you have to disconnect it for acessing serial port. Run Hyper
Terminal then some screen come for asking area codes, put some garbage value and
press OK and in the next page press OK, then you will come to this page.
If you use some USB to RS232 converter, then I can't say that Hyper Terminal will
work or not. It does not worked for me.
See in the device manager for the port settings.
Device Manage COM Properties
device manager
Testing your computer Serial Port:
Short circuit Rx and Tx of your computer and type letters keeping hyperterminal
working. If you can see what you are typing(i.e, if you type 'a' then it is displayed as 'a'
on the hyperterminal) then you can infer that your serial port will work properly.
Don't use ground and other pins,leave all other pins open and just short circuit pin2
and pin3(Rx and Tx).
serial5
Now connect serial port to max232, then short circuit the Rx and Tx of max232 ie pin
11 and pin 12 and type something in hyperterminal, see what you are getting. If you
are getting the alphabets you typed then your connections are proper. Max232 is
working fine. By default the TTL output is 5V. See the RS232 output corresponding to
5V input, it should be mostly in the range -8V to -10V. See these conditions properly
otherwise your microcontroller will get burned because voltage greater than 5V is
damagerous to the microcontroller.
FOLDER:serial_computer(see avrbook.rar from yahoo group-booksbybibin)
/*this program sends back the next ascii value u typed*/
/*----------------------------------------------------------------
-----------------HEADER FILES-------------------------------------
-----------------------------------------------------------------*/
#include <avr/io.h>
/*----------------------------------------------------------------
-----------------FUNCTIONS---------------------------------------
-----------------------------------------------------------------*/
void InitUART( unsigned char baudrate );
unsigned char ReceiveByte( void );
void TransmitByte( unsigned char data );
/*----------------------------------------------------------------
-----------------MAIN FUNCTION------------------------------------
-----------------------------------------------------------------*/
void main()
{
InitUART( 25 ); /* Set the baudrate to 2400 bps using a 1MHz crystal */
for(;;) /* Forever */
{
TransmitByte(ReceiveByte()+1); /* Echo the received character */
}
}
/*----------------------------------------------------------------
------------FUNCTIONS TO Initialize UART--------------------------
-----------------------------------------------------------------*/
void InitUART( unsigned char baudrate )
{
UBRRL = baudrate; /* Set the baud rate */
UCSRB = (UCSRB | _BV(RXEN) | _BV(TXEN) ); /* Enable UART receiver and transmitter */
}
/*----------------------------------------------------------------
------------FUNCTIONS TO READ UART-------------------------------
-----------------------------------------------------------------*/
unsigned char ReceiveByte( void )
{
while ( !(UCSRA & (_BV(RXC))) ); /* Wait for incomming data */
return UDR;/* Return the data */
}
/*----------------------------------------------------------------
------------FUNCTIONS TO WRITE UART-------------------------------
-----------------------------------------------------------------*/
void TransmitByte( unsigned char data )
{
while ( !(UCSRA & (_BV(UDRE))) ); /* Wait for empty transmit buffer */
UDR = data; /* Start transmittion */
}
Here this code will send back next ASCII you type. See the error in baud rates for
different clock frequencies. Here I am using 2400 8 N 1 with 1Mhz internal crystal. If
you want more speed then go for internal 8Mhz crystal. But still error is around .2%,
so if you want 0% error in baud rate go for crystal 3.6864Mhz or crystals given in the
datasheet which is having 0% error in baud rate
TransmitByte(ReceiveByte()+1);
You can use terminal program inbuilt in WinXP or Bray's terminal for serial port
interfacing with computer. Bray's terminal is a free software with more options than
hyperterminal. Hyperterminal won't show the transmitted data, it shows only the
received data.
SERIAL PORT TROUBLESHOOTING AND ERRORS:
When synchronisation is lost you will get xxxxxxxxxxxxxxxxx displayed on
hyperterminal. So be thorough about the micrcontroller clock and baud rate you set.
This is the most important error occurs with serial port. If you use a C program then
you will get some different values other than the values you sent. Remember only one
program can acess serial port at a time. Sometimes Turbo C is having problem in
acessing serial ports. I am attaching a C program with it.
Forumer™ is Voted #1 Free Forum Hosting provider
Build your own community today with the largest message board hosting company.