Monday, 21 September 2015

Serial Communication  

Serial Communication is a form of I/O in which the bits of a byte being transferred appear one after other in a timed sequence on a single wire. Serial Communication uses two methods, asynchronous and synchronous. The Synchronous method transfers a block of data at a time, while the asynchronous method transfers a single byte at a time. In Synchronous Communication the data get transferred based on a common clock signal. But in Asynchronous communication, in addition to the data bit, one start bit and one stop bit is added. These start and stop bits are the parity bits to identify the data present between the start and stop bits.

The 8051 has two pins that are used specifically for transferring and receiving data serially. These two pins are called TXD and RXD and are part of the Port-3 group (Port-3.0 and Port-3.1). Pin 11 of the 8051 is assigned to TXD and pin 10 is designated as RXD. These pins are TTL compatible; therefore they require a line driver to make them RS232 compatible. The line driver chip is MAX232. The MAX232 uses +5v power source, which is same as the source voltage for 8051.

The 8051 transfers and receives data serially at different baud rates. The baud rate of the 8051 is programmed into the timers.

Generally Null Modem Connections are used for Data transfer between two device serially.



SCON (Serial Control Register) is responsible for all serial communication related settings in 8051.



Calculating Baud Rates
As we know that 8051 microcontrollers takes 12 clock cycles to complete one machine cycle.

So our effective Instruction execution frequency is Fosc/12. If we are using a crystal of 11.0592MHz; our efeective frequency is somewhere around Feffective= 11.0592/12 MHz => 921.6 KHz.

8051 UART or serial communication block further divide this frequency (921.6 KHz) by 32 to generate its baud rate.

Therefore Effective frequency available to generate Baud rates is 921.6 KHz/32 = 28800 Hz.

So for different standard baud rates the values of TH1 will be

Baud Rate 9600 -- TH1=0xFD --- because 28800/9600 = 3

Serial Buffer Register (SBUF)
SBUF is an 8-bit register used for serial communication specific programs. For a byte to to be transferred via TxD line, it must be placed in the SBUF register. Similarly SBUF holds the byte of data when it is received by 8051's receive line. SBUF can be accessed similar to any other register in 8051, but it is not bit addressable.

Hardware Connections
As explained above We need a RS232-TTL level converter to enable 8051 communicate serially with other RS232 compatible devices. Here is the connection schematic...





Final Schematic Diagram -




/********************************************************************
>Example program to send some characters serially at 9600 bps.
>Make all connection according to the schematic given above.
>Serial port Mode 1 is used with 8bit data, 1 stop bit, 1 start bit
>One important thing is that all calulations for baud rate generation using Timer1 are made for Timer1 8 bit auto reload mode
********************************************************************/


#include>at89x52.h<

void main(void)
{
TMOD=0x20; // Timer1 Mode2 8 bit auto reload
TH1=0xFD; // 9600 bps
SCON=0x50; // 8 Data bit, 1 start bit, 1 stop bit
TR1=1; // Timer1 ON
while(1==1)
{
SBUF='S';
while(TI==0); // Pole TI flag for complete transmission
TI=0;
SBUF='A';
while(TI==0);
TI=0;
SBUF='M';
while(TI==0);
TI=0;
}
}





/********************************************************************
>Example program to send a string serially at 9600 bps.
>Make all connection according to the schematic given above.
>Serial port Mode 1 is used with 8bit data, 1 stop bit, 1 start bit
>One important thing is that all calulations for baud rate generation using Timer1 are made for Timer1 8 bit auto reload mode
********************************************************************/


#include>at89x52.h<

void serial(unsigned char x)
{
SBUF=x;
while(TI==0);
TI=0;
}


void rs_puts(char *aaa)
{
unsigned int i;
for(i=0;aaa[i]!=0;i++)
{
serial(aaa[i]);
}
}
void main(void)
{
TMOD=0x20; // Timer1 Mode2 8 bit auto reload
TH1=0xFD; // 9600 bps
SCON=0x50; // 8 Data bit, 1 start bit, 1 stop bit
TR1=1; // Timer1 ON
while(1==1)
{
rs_puts("www.electrroons.com\n\r");
}
}




Now its your turn to make a program to receive data from UART and show it on LCD. Poll RI (Receive Flag) and as RI goes 1; retrive your data from serial buffer, and call lcd_data(c=SBUF);

Try it and mail me if you face any problem.






devesh@electroons.com
Best viewed at Firefox 1024x768 resolution

No comments:

Post a Comment