Friday, February 11, 2011

How to communicate using serial port?

Parallel Communication:

Usually 8 or more bits are sent simultaneously.

Serial Communication:

Bits of a character are sent one by one.


Why go serial??
Parallel communication is faster but it has certain drawbacks. For transmitting data in parallel we need lot many number of wires (i.e. no of wires = no of bits to be transmitted at once).

For transmitting data to long distances this approach proves expensive. Also the signal degradation with distance in parallel transmission is faster.
Serial transmission can use a minimum of 2 wires (Ground and data if it is half duplex communication) or 3 wires (Ground, Reception, Transmission for full duplex)
Serial communication can also provide for error detection and correction facilities which in parallel would again require more number of wires. The obvious drawback of serial communication is the speed which definitely is lesser compared to ideal parallel data transmission.


UART (Universal Asynchronous Receiver Transmitter)
UART is block in the muC which aids in serial transmission of data. Inside the CPU all data processing and manipulations are done in parallel. But for the reasons stated above communication with devices far away from the muC are better of done serially. The UART block converts the data given by the CPU and converts it into serial stream of data with start, stop and parity bits as required/configured.


We can use the UART in an interrupt driven mode as well as full software control mode. An interrupt driven UART program would generally prove efficient than the other.
The below is an example of such a program.
A very essential thing to do before using the program is reading the datasheet. Go through the UART functional descriptions and the special functions registers.


#include”REG52MOD.h”
#include”delay.h”
#include”LCD.h”


unsigned char RXED,TXED;
void UART_INIT(void) //UART Initialization
{
SBUF=0×00; //Empty Serial Buffer
SM0=0; //Set UART
SM1=1; //in mode 1
SM2=0;


REN=1; //Receive Enable
TMOD=0×2F; //Set baud
TH1=253; //rate as 9600bps
TR1=1;
ES=1; //Enable serial interrupts
EA=1; //Global interrupt enable
}


void TX(unsigned char dat) //Transmit Data
{


SBUF=dat;
delay(10);


}
void RXTX_int(void) interrupt 4 //Serial interrupt ISR
{
if(RI==1)
{
RI=0; //Reset receive data bit
RXED=SBUF;


LCD_WRITE(RXED); //Display received character on LCD
}


if(TI==1)
{
TI=0; //Reset transmit data bit
}
}


void main(void)
{


LCD_INIT(); //Initialize LCD
UART_INIT(); //Initialize UART
delay(1);
TX(’S’);
TX(’t’);
TX(’a’);
TX(’r’);
TX(’t’);
while(1){}


}
The program should be self explanatory. But some queries that might arise I guess would be:
Why have I set RI = 0, TI=0 in the ISR??
These are the flags set when a bit is received or transmitted completely. They are not automatically reset when the program enters the ISR. So we need to reset them through software.


Note: All flags need not be reset manually. Some have auto reset facilities.
But it is a good practice to reset any interrupt flag when you enter the ISR if that extra instruction doesn�t cause much inconvenience to the performance of the code.

Why is TH1=253??
This particular count value gives us a baud rate of 9600bps.


In serial communication mode 1, the baud rate is determined by the overflow frequency of Timer 1. There are various ways in which we can configure timer 1 to overflow at particular frequencies. But a simple method is using the timer 1 in 8-bit auto reload mode.
To determine the value that must be placed in TH1 to generate a given baud rate, we may use the following equation
TH1 = 256 – ((Crystal / 384) / Baud)
Will this setting remain same for all crystal values??
No. It won�t. The timer is coupled with the oscillator frequency. So for different crystal values the count value to be loaded will be different.


Note: The crystal value 11.0392Mhz is a peculiar value of frequency which can generate all standard baud rates like 2400, 4800, 9600 etc.
For interfacing the UART with your PC you need to configure the Hyper Terminal in windows.
You can find it in Start > Programs > Accessories > Communications > Hyper Terminal


Step 1
Enter a name for the connection


Step 2

Select Port as COM1 (Generally)


Step 3
Step 4

No comments:

Post a Comment