Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

8051 Serial Transmission

In a serial communication protocol there is no common clock between the two


communicating devices. The clock signal is derived from special character in the data stream.
A Universal Asynchronous Receiver-Transmitter (UART) circuitry control the receiving &
transmitting of data stream.

Data Framing
In asynchronous receiving & transmitting, data is framed in between START and STOP bits.
A START bit (usually a ZERO bit) is sent before sending data bits (LSB first)
and, ended with a STOP bit (usually a ONE)

Figure 1 – Data Framing

Configuring Serial I/O - SCON

Figure 2 SCON Register


 The serial port is configured using the SCON register shown in Figure 2.
 In modes 0 and 2 the baud rate is fixed based on the oscillator’s frequency. In modes
1 and 3 the baud rate is variable based on how often Timer 1 overflows.
 The high four bits (bits 4 through 7) are configuration bits. The low four bits (bits 0
through 3) are operational bits. They are used when actually sending and receiving
data--they are not used to configure the serial port.

NOTE
SM0, SM1 They determine the framing of data by specifying the number of bits per
character, and the start and stop bits
SM2 This enables the multiprocessing capability of the 8051
REN It is a bit-adressable register. When it is high, it allows 8051 to receive data
on RxD pin. If low, the receiver is disable
TI When 8051 finishes the transfer of 8-bit character, it raises TI flag to indicate
that it is ready to transfer another byte. TI bit is raised at the beginning of the
stop bit.
RI When 8051 receives data serially via RxD, it gets rid of the start and stop bits
and places the byte in SBUF register. It raises the RI flag bit to indicate that a
byte has been received and should be picked up before it is lost.

SBUF
SBUF is an 8-bit register used solely for serial communication
 For a byte data to be transferred via the TxD line, it must be placed in the SBUF
register
 The moment a byte is written into SBUF, it is framed with the start and stop bits and
transferred serially via the TxD line
 SBUF holds the byte of data when it is received by 8051 RxD line
 When the bits are received serially via RxD, the 8051 deframes it by eliminating the
stop and start bits, making a byte out of the data received, and then placing it in SBUF
Transmitting DATA Serially
 Transmission of data is initiated by writing to SBUF register.
 Data is transmitted when TI bit in SCON register is cleared (ZERO).
 TI bit is set by hardware when STOP bit appears on TXD terminal.
 TI bit must be cleared by software for next transmission.

Mode 1 – 8-bit UART


 In this mode, 10 bit data is transmitted serially consisting of 1 START bit, 8 data bits
and 1 STOP bit.
 The variable baud rate is set by Timer 1
NOTE
Baud rate is a measure of how fast data is transferred between two devices. It is measured in
bits per second (bps). Standard baud rates are: 110, 150, 300, 600, 1200, 2400, 4800, 9600,
19200 and 28800

Baud Rate Using Timer 1


8-bit auto-reload mode is used to get the overflow rate for the desired baud rate.

Calculating the Reload Value


To determine the value that must be placed in TH1 to generate a given baud rate, we may use
the following equation (assuming PCON.7 is clear).

TH1 = 256 - ((Crystal / 384) / Baud)


If PCON.7 is set then the baud rate is effectively doubled, thus the equation becomes:
TH1 = 256 - ((Crystal / 192) / Baud)
For example, if we have an 11.059Mhz crystal and we want to configure the serial port to
19,200 baud we try plugging it in the first equation:
TH1 = 256 - ((Crystal / 384) / Baud)
TH1 = 256 - ((11059000 / 384) / 19200)
TH1 = 256 - ((28,799) / 19200)
TH1 = 256 - 1.5 = 254.5

Since TH1 value is not a whole number, we need to set PCON.7 (SMOD).
When we do this we double the baud rate and utilize the second equation mentioned above.
Thus we have:
TH1 = 256 - ((Crystal / 192) / Baud)
TH1 = 256 - ((11059000 / 192) / 19200)
TH1 = 256 - ((57699) / 19200)
TH1 = 256 - 3 = 253

Eg 1 Write a C program for 8051 to transfer the letter “A” serially at 4800 baud
continuously. Use 8-bit data and 1 stop bit.

Solution:
#include <reg51.h>
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;
}
}
Eg 2 Write an 8051 C program to transfer the message “YES” serially at 9600 baud, 8-bit
data, 1 stop bit. Do this continuously.
Solution:
#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;
}

You might also like