Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
746 views21 pages

03 - 8051 Serial Port Codes

When the SMOD bit in the PCON register is set to 1, it doubles the baud rate that can be achieved with a given crystal frequency. Specifically: - With SMOD=0, the baud rate formula is: Baud Rate = Crystal Frequency / (12 x Timer Value) - With SMOD=1, the baud rate formula becomes: Baud Rate = Crystal Frequency / (6 x Timer Value) So by setting SMOD to 1, you are effectively dividing the crystal frequency by 6 instead of 12 to calculate the baud rate. This has the effect of doubling the maximum baud rate that can be achieved for a given crystal frequency and timer value. For example, with

Uploaded by

atiqakbar1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
746 views21 pages

03 - 8051 Serial Port Codes

When the SMOD bit in the PCON register is set to 1, it doubles the baud rate that can be achieved with a given crystal frequency. Specifically: - With SMOD=0, the baud rate formula is: Baud Rate = Crystal Frequency / (12 x Timer Value) - With SMOD=1, the baud rate formula becomes: Baud Rate = Crystal Frequency / (6 x Timer Value) So by setting SMOD to 1, you are effectively dividing the crystal frequency by 6 instead of 12 to calculate the baud rate. This has the effect of doubling the maximum baud rate that can be achieved for a given crystal frequency and timer value. For example, with

Uploaded by

atiqakbar1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

8051

UART
Serial Port
Why Serial Communication?
- Parallel I/O uses many signal pins
- Cost of cable
- Many applications do not need high data rate
Basics of Serial Communication
• Serial data communication uses two methods:
• Synchronous method transfers a block of data at a time
• Asynchronous method transfers a single byte at a time
• It is possible to write software to use either of these methods
• The programs can be tedious and long
• There are special IC chips made by many manufacturers for serial
communications:
• UART (universal asynchronous receiver transmitter)
• USART (universal synchronous asynchronous receiver transmitter)
Half- and Full-Duplex Transmission
• If data can be transmitted and received, it is a duplex transmission
• If data transmitted one way a time, it is referred to as half duplex
• If data can go both ways at a time, it is full duplex
• This is contrast to simplex transmission
UART Protocol
• A protocol is a set of rules agreed by both the sender and receiver on:
• How the data is packed
• How many bits constitute a character
• When the data begins and ends
• Asynchronous serial data communication is widely used for character-
oriented transmissions
• Each character is placed in between start and stop bits, this is called framing
• The start bit is always one bit
• The stop bit can be one or two bits
• The start bit is always a 0 (low)
• The stop bit(s) is 1 (high)
Registers Used in 8051 UART
• SBUF (8-bit)  Hold data to be transmitted or Save received data
• SCON (8-bit)  Used for serial port control settings
SBUF Register
• 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, and then placing the byte in SBUF
SCON Register

• SCON is bit-adressable register used to program the start bit, stop


bit, and data bits of data framing, among other things
SCON Register (cont.)
• 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
SCON Register (cont.)
• REN (receive enable)
• When it is high, it allows 8051 to receive data on RxD pin
• If low, the receiver is disable
• TI (transmit interrupt)
• 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 (receive interrupt)
• 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
• RI is raised halfway through the stop bit
Data Transfer Rate - BAUD RATE
• The rate of data transfer in serial data communication is stated in bps
(bits per second)
• Another widely used terminology for bps is baud rate
• It is modem terminology and is defined as the number of signal changes per
second
• In 8051 Timer-1 in 8-bit Auto reload mode-2 is used to generate
baud rate
How Timer value for Baud Rate is Generated??
• Formula is
TH1 = -[(Xtal÷12)÷32 ]/ Baudrate]

e.g for Xtal=11.0592Mhz, and Baud Rate= 4800bps


TH1 = -[(11.0592*10^6 ÷ 12) ÷32 ]/ 4800] = -6
coding steps to transmit data

Load TH1
Load TMOD for Load SBUF with data to be
according to
Timer-1 in Load SCON Start transmitted
required baud
Mode 2 timer
rate

Goto next step Wait for TI


according your Clr TI flag to become high
requirement (wait for completion
of data transmission)
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;
}
}
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 void SerTx(unsigned char x)
{
TH1=0xFD; //9600 baud rate
SBUF=x; //place value in buffer
SCON=0x50; while (TI==0); //wait until transmitted
TR1=1; //start timer TI=0;
}
while (1)
{
SerTx(‘Y’);
SerTx(‘E’);
SerTx(‘S’);
}
}
coding steps to transmit data

Wait for RI
Load TH1 to become high
Load TMOD for
according to (wait for completion of data
Timer-1 in Load SCON Start
required baud reception)
Mode 2 timer
rate

Goto next step Take out received


according your Clr RI flag data from SBUF reg
requirement
Program the 8051 in C to receive bytes of data serially and put them in P1. Set the baud rate at 4800, 8-bit data, and 1
stop bit.
Solution:
#include <reg51.h>
void main(void)
{
unsigned char mybyte;
TMOD=0x20; //use Timer 1, mode 2
TH1=0xFA; //4800 baud rate
SCON=0x50;
TR1=1; //start timer
while (1) //repeat forever
{
while (RI==0); //wait to receive
mybyte=SBUF; //save value
P1=mybyte; //write value to port
RI=0;
}
}
Write an 8051 C program to send two different strings to the serial port. Assuming that SW is connected to pin P2.0, monitor its
status and make a decision as follows:
SW = 0: send your first name
SW = 1: send your last name , Assume XTAL = 11.0592 MHz, baud rate of 9600, 8-bit data, 1 stop bit.
How To Increase Baud Rate??
• There are two ways to increase the baud rate of data transfer
1. To use a higher frequency crystal
2. To change a bit in the PCON register
• PCON register is an 8-bit register
• When 8051 is powered up, SMOD is zero by default, We can set it to high by software and
thereby double the baud rate
Baud rate when SMOD=1 in
PCON register?

You might also like