1 of 14

SERIAL PORT (RS232) INTERFACING TO 8051

2 of 14

  • Data transferred by computer occurs in two ways. They are parallel and serial communication.
  • Parallel Communication has 8 or more lines. It is used for transfer of data to a device that is only a short distance.
  • while serial communication is used to transfer data over a long distance.
  • The serial communication uses two techniques for communication. They are
  • Synchronous- transfer block of data(characters) at a time Asynchronous- transfer single byte at a time
  • This communication can be done by the following hardware: USART (Universal Synchronous-Asynchronous Receiver-Transmitter)
  • UART (Universal Asynchronous Receiver Transmitter)

3 of 14

RS232 standards

  • It is the most widely used serial I/O interfacing standard for serial communication.
  • The input and output voltage levels are not TTL compatible.
  • The bit-value of 1 is represented as -3 to -25 V and the value0 is represented as +3 to +25 V, while -3 to +3 is undefined.
  • To connect RS232 to a microcontroller system, it must use voltage converters such as MAX232 to convert the TTL logic levels to the RS232 voltage levels, and vice versa.
  • MAX232 IC chips are commonly referred to as line drivers.

4 of 14

8051 connections

  • To RS232 8051 has two pins used for transferring and receiving data serially.
  • They are TxD and RxD pins. TxD and RxD are part of the port 3 group (P3.0 and P3.1), while pin 11 (P3.1) of 8051 is assigned to TxD.
  • The pin 10 (P3.0) of 8051 is assigned to RxD. These pins are TTL compatible.
  • They require a line driver to make them RS232 compatible.
  • The driver used for this is MAX232 chip.

5 of 14

Serial Communication Registers

  • SBUF Register: It is an 8-bit register used for serial communication. To transmit a byte of data via the TxD line, it must be placed in the SBUF register.
  • SCON (serial control) register: It is an 8-bit register used to program start bit, stop bit, and data bits of data for framing, among other things. In this SM0 and SM1 are used to determine the mode.

6 of 14

7 of 14

BAUD RATE

  • Relationship between the crystal frequency and the baud rate in the 8051 is as follows:
  • 8051 divides the crystal frequency by 12 to get the machine cycle frequency.
  • Load FD/FA/F4/E8 in TH1 to get 9600/4800/2400/1200 baud rate.

8 of 14

Steps to send data serially in serial port communication

  • 1. Set baud rate by loading TMOD register with the value 20H; this indicates timer 1 in mode 2 (8-bit auto-reload) to set baud rate.
  • 2. The TH1 is loaded with proper values to set baud rate for serial data transfer.
  • 3. The SCON register is loaded with the value 50H, indicating serial mode 1, where an 8- bit data is framed with start and stop bits.
  • 4. TR1 is set to 1 to start timer 1.
  • 5. Transmit Interrupt(TI) is cleared by CLR TI instruction.
  • 6. The character byte to be transferred serially is written into SBUF register.
  • 7. The TI flag bit is monitored to see if the character has been transferred completely. 8. To transfer the next byte, go to step 5.
  • Check the TI flag bit, we know whether or not 8051 is ready to transfer another byte.
  • TI flag bit is raised by the 8051 after transfer of data. ● TI flag is cleared by the programmer by instructions like “CLR TI”.
  • Writing a byte into SBUF before the TI flag bit is raised, may lead to loss of a portion of the byte being transferred.

9 of 14

Steps to receive data serially in serial port communication

  • 1. Set baud rate by loading TMOD register with the value 20H; this indicates setting of timer 1 in mode 2 (8-bit auto-reload) to set baud rate.
  • 2. The TH1 is loaded with proper values to set baud rate.
  • 3. The SCON register is loaded with the value 50H, indicating serial mode 1, where an 8- bit data is framed with start and stop bits.
  • 4. TR1 is set to 1 to start timer 1.
  • 5. Receive Interrupt(RI) is cleared by CLR RI instruction.
  • 6. The RI flag bit is monitored to see if an entire character has been received yet.
  • 7. When RI is raised, SBUF has the byte, its contents are moved into a safe place. 8. To receive next character, go to step 5.
  • It receives the start bit, next bit is the first bit of the character about to be received.
  • When the last bit is received, a byte is formed and placed in the SBUF register.
  • When the stop bit is received, it makes RI = 1 indicating that the entire character byte has been received and can be written before being overwritten.
  • When RI=1, received byte is in the SBUF register, copy SBUF contents to a safe place.
  • After the SBUF contents are copied, the RI flag bit must be cleared to 0.

10 of 14

PROGRAMMING FOR SERILA PORT-8051

  • #include void main(void)
  • {

TMOD=0x20; //use Timer 1, mode 2 TH1=0xFA; //4800 baud rate SCON=0x50; TR1=1; while (1)

{

SBUF=‘A’; //place value in buffer while (TI==0); TI=0;

}

}

11 of 14

Program to send data serially

#include<reg51.h>

void SerTx(unsigned char);

void main(void)

{

TMOD=0x20; //use Timer 1, mode 2

TH1=0xFD; //9600 baud rate

SCON=0x50;

TR1=1; //start timer

while (1) {

SerTx(‘Y’);

SerTx(‘E’);

SerTx(‘S’);

}

}

void SerTx(unsigned char x){

SBUF=x; //place value in buffer

while (TI==0); //wait until transmitted

TI=0;

}

12 of 14

BLOCK DIAGRAM

13 of 14

CIRCUIT DIAGRAM

14 of 14

THANK YOU