Monday, 23 December 2013

SERIAL COMMUNICATION 8051

SERIAL COMMUNICATION


              Several devices collect data from sensors and need to send it to another unit, like a computer, for further processing. Data transfer/communication is generally done in two ways: parallel and serial. In the parallel mode, data transfer is fast and uses more number of lines. This mode is good for short range data transfer.
Serial communication on the other hand, uses only one or two data lines to transfer data and is generally used for long distance communication. In serial communication the data is sent as one bit at a time. This article describes the interfacing of 8051 microcontroller (AT89C51) with a computer via serial port, RS232. Serial communication is commonly used in applications such as industrial automation systems, scientific analysis and certain consumer products.
               The Universal Asynchronous Receiver Transmitter (UART) is a popular and widely-used device for data communication in the field of telecommunication. There are different versions of ARTs in the industry. Some of them contain FIFOs for the receiver/transmitter data buffering and some of them have the 9 Data bits mode (Start bit + 9Data bits + Parity + Stop bits). This application note describes a fully configurable UART optimized for and implemented in a variety of Lattice devices, which have superior performance and architecture compared to existing semiconductor ASSPs (application-specific standard products).

This UART reference design contains a receiver and a transmitter.  The receiver performs serial-to-parallel conversion on the asynchronous data frame received from the serial data input SIN. The transmitter performs parallel-to-serial conversion on the 8-bit data received from the CPU. In order to synchronize the asynchronous serial data and to insure the data integrity, Start, Parity and Stop bits are added to the serial data.

The UART, or Universal Asynchronous Receiver / Transmitter, is a feature of our microcontroller useful for communicating serial data (text, numbers, etc.) to your PC. The device changes incoming parallel information (within the microcontroller/PC) to serial data which can be sent on a communication line. With the UART, you can add an LCD, boot loading, Bluetooth wireless, make a data logger, debug code, test sensors, and much more.


The first chip on UART was designed in around 1971. It is one of the simplest forms of data transmission from a controller/PC. It requires a minimum of two pins (transmitter and receiver pin) and a common ground line for data communication. The UART chip is generally found inbuilt in most of the microcontrollers. 

In order to communicate the controller with PC a level converter IC like MAX232/233 is needed. Moreover to establish synchronization between two entities, both the devices must transmit or receive the data at same baud rate (the baud rate must be previously known at both the ends). Because of its speed limitation, UART is not used for high speed devices. 

The microcontroller AT89C51 has an inbuilt UART for carrying out serial communication. The serial communication is done in the asynchronous mode. A serial port, like other PC ports, is a physical interface to establish data transfer between computer and an external hardware or device. This transfer, through serial port, takes place bit by bit.

IBM introduced the DB-9 RS-232 version of serial I/O standard, which is most widely used in PCs and several devices. In RS232, high and low bits are represented by flowing voltage ranges: 

To learn more about UART study my previous posts about UART and MAX232. The 8051 microcontroller is having an inbuild UART. so we only need a MAX 232 IC to communicate with the microcontroller and UART. So by using the MAX232 IC it is easy to made a communicaiton between the microcontroller and the Uart.  
The microcontroller AT89C51 has an inbuilt UART for carrying out serial communication. The serial communication is done in the asynchronous mode. A serial port, like other PC ports, is a physical interface to establish data transfer between computer and an external hardware or device. This transfer, through serial port, takes place bit by bit.







CODING FOR 8051


#include <reg52.h>
//DEFINE CONSTANT
#define Baud_rate0xFD  // BAUD RATE 9600              
//DEFINE PROTOTYPES
voidSerialInitialize(void);
voidSendByteSerially(unsigned char);
voidcct_init(void);
sbit Appliance1 = P1^0;
sbit Appliance2 = P1^1;
sbit Appliance3 = P1^2;
sbit Appliance4 = P1^3;
sbit Appliance5 = P1^4;
sbit Appliance6 = P1^5;
sbit Appliance7 = P1^6;
sbit Appliance8 = P1^7;

void main()
{    
cct_init();    
SerialInitialize();  
    EA = 1;
    ES = 1;
    while(1) {;}
}
voidcct_init(void)   //initialize cct
{
    P0 = 0x00; //not used
    P1 = 0x00; //Used for Appliances
    P2 = 0x00; //not used
    P3 = 0x03; //used for serial
}

voidSerialInitialize(void)                  // INITIALIZE SERIAL PORT
{
 TMOD = 0x20;                          // Timer 1 IN MODE 2 -AUTO    RELOAD TO GENERATE BAUD RATE
    SCON = 0x50;                         // SERIAL MODE 1, 8-DATA BIT 1-START BIT, 1-STOP BIT, REN ENABLED
    TH1 = Baud_rate;                     // LOAD BAUDRATE TO TIMER REGISTER
    TR1 = 1;                             // START TIMER
}

voidSendByteSerially(unsigned char serialdata)
{
    SBUF = serialdata;                    // LOAD DATA TO SERIAL BUFFER REGISTER
    while(TI == 0);               // WAIT UNTIL TRANSMISSION TO COMPLETE
    TI = 0;               // CLEAR TRANSMISSION INTERRUPT FLAG
}

voidserial_ISR (void) interrupt 4
{
    //receive character
    charchr;
    if(RI==1)
    {
       chr = SBUF;
       RI = 0;
    }
    P0 = ~P0;    //Show the data has been updated

switch(chr)
    {
case '1':  Appliance1 = 1; SendByteSerially('k');  break;
case '2':  Appliance2 = 1; SendByteSerially('k');  break;
case '3':  Appliance3 = 1; SendByteSerially('k');  break;    
case '4':  Appliance4 = 1; SendByteSerially('k');  break;    
case '5':  Appliance5 = 1; SendByteSerially('k');  break;    
case '6':  Appliance6 = 1; SendByteSerially('k');  break;    
case '7':  Appliance7 = 1; SendByteSerially('k');  break;    
case '8':  Appliance8 = 1; SendByteSerially('k');  break;

case 'a':  Appliance1 = 0; SendByteSerially('k');  break;
case 'b':  Appliance2 = 0; SendByteSerially('k');  break;
case 'c':  Appliance3 = 0; SendByteSerially('k');  break;
case 'd':  Appliance4 = 0; SendByteSerially('k');  break;
case 'e':  Appliance5 = 0; SendByteSerially('k');  break;
case 'f':  Appliance6 = 0; SendByteSerially('k');  break;
case 'g':  Appliance7 = 0; SendByteSerially('k');  break;
case 'h':  Appliance8 = 0; SendByteSerially('k');  break;

default: ; break;     //do nothing
    }

    RI = 0;
}   

By using the code above we can communicate with the electrical appliances by giving the voltage and also grounding the voltage. So it is possible to communicate with the electrical devices or home applicances and we can made a home automation project by using the code above.


No comments:

Post a Comment

share your thoughts