8051 - Serial Communication

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 44

8051- serial communication

8051- serial communication


• One of the 8051’s powerful features is its integrated
UART, also known as a serial port.
• The fact that the 8051 has an integrated serial port
means that one can easily read and write values to the
serial port.
• If it were not for the integrated serial port, writing a byte
to a serial line would be a rather tedious process
requiring turning on and off one of the I/O lines in rapid
succession to properly "clock out" each individual bit,
including start bits, stop bits, and parity bits.
1.serial communication
Fundamentals of serial communication
Serial communication means data transfer is bit by bit.
It has 3 modes of operation
a) simplex

transmitter receiver

data transmission is only in one direction

b) half duplex

transmitter receiver

receiver transmitter
In half duplex data transmission is in both directions, not at single
time.

c) Full duplex

transmitter receiver

receiver transmitter

In Full duplex method data transmission is in both ways. i.e.


Simultaneously
For serial communication to work, the byte of data must be converted
to serial bits using a parallel –in –serial out shift register ,then it can
be transmitted over a single data line.

Similarly at receiving end, there must be a serial- in-parallel_out


register, to receive serial data and pack them into a byte .

If the data has to be transferred on the telephone line ,a MODEM


must be connected to convert 0&1’s to audio tones.

When the distance is short ,the digital signal can be transferred as it is


on a simple wire and requires no modulation. This is how a IBM PC
key board transfer the data to the mother board .
•Serial data transfer is two ways .
1. Asynchronous data transfer – transmits byte by byte

2. Synchronous data transfer --transfers a block of data at a time.


It is possible to write software to use either of these methods but they
are complicated.

A special IC is commonly used for this purpose.


UART (universal asynchronous receiver transmitter)
USART (universal synchronous and receiver transmitter)

8051 has a built it in UART : Asynchronous data transfer

In synchronous format, a receiver and a transmitter are synchronized.


A block of characters are transmitted along with synchronization
information

The transmission rate is greater than 20k bits per second


transmitter receiver

sync sync d d

BYSYNC,HDLC,SDLC protocols will come under this class.

Asynchronous data transfer

transmitter receiver
start stop stop
data

space mark

Framing =start bit +data + parity bit +1 or 2 stop bits. Normally (when
there is no data), high (logic 1)is transferred from source to
destination.
Rate of transmission
The rate at which the bits are transmitted in bps.
Baud: The number of signal changes per one second.
As far as conductor wire is concerned, the baud rate and bps are
same and hence they are interchangeable
Error checks in data communication:
Parity check can only one bit of error can be detected. Check sum is
another variety to check error. Check sum is a sum of all data byte
string (without carry).

Data communication over telephone

Serial to parallel
Microcontroller converter Modem
RS232C cable

1488 1489

2 DCE
DTE
1489 3 1488 Modem

7
Data Terminal Equipment : Generally computer or microprocessor
that are sending and receiving data.

Data communication equipment modems and other equipment that are


used to send data for long distance.

Digital data communication uses modem and & standard phone lines.
Max 232:

It is a single chip ,which converts TTL levels to Rs232c to


voltage levels and vice versa.
VCC

C3
1 2
6
C1 3 C4
4
C2 GND
5
11 T1 in T1 out 14

12 R1 out R1 in 13

10 T2 in T2 out 7

9 R2 out R2 in 8

gnd
TTL side 15 RS232 side
8051
MAX232

P3.1 11 11
TxD 5
14 2 gnd

13 3
P3.0 10 12
RxD
DB--9
VCC

13 11

14 15
12 16
10
17
MAX233
2 T1 in T1 out 5

3 R1 out R1 in 4

1 T2 in T2 out 18

20 R2 out R2 in 19

gnd
TTL side RS233 side
8051 – serial communication
• For implementing serial communication using 8051 microcontroller
one has to configure the serial ports operation mode and baud rate.
• Once configured, all we have to do is write to an SFR to write a
value to the serial port or read the same SFR to read a value from
the serial port.
• The 8051 will automatically let us know when it has finished sending
the character we wrote and will also let us know whenever it has
received a byte so that we can process it. We do not have to worry
about transmission at the bit level--which saves us quite a bit of
coding and processing time.
• There are 2 SFRs dedicated for serial communication.
1. SBUF and 2. SCON
• SBUF is used for buffering the data and SCON is used to configure
and control the operation of serial port of 8051.
Setting the Serial Port Mode
• Framing:
An 8-bit message needs to be “framed” so that the receiver can
detect correctly its beginning and end.

• Standard framing:
– Start bit – always 0, Stop bit – always 1.
– Optional parity bit
– Stop bit can be one or two bits
Serial Communication
• The message now becomes:
– Start bit (10), LSB, …, MSB, <parity bit>, Stop bit (01),
<2nd stop bit (1)>

Sta <P Sto <S


0 1 2 3 4 5 6 7
rt > p t>

• MCS-51 has a full-duplex serial port that can be used as a normal serial
interface (non-framed) or as an internal UART (framed).
– This serial port controls the RxD and TxD dual functions for pins P3.0
and P3.1.
• The MCS-51 serial port is controlled using the SCON SFR (98H).
• The MCS-51 serial port communicates with the rest of the chip using the
SBUF SFR (99H).
Serial Communication
• SBUF is actually two separate registers at the same address.
– Write-only transmit register.
– Read-only receive register.
• Cannot read back what was sent for transmission.
• The byte to be transmitted on the serial port is “written” into SBUF.
– Serial transmission starts immediately.
• The byte received from the serial port will be stored in SBUF once the last
bit is received.
– This is called “double buffering”.
• Received data is buffered in the serial port itself until the full byte
is received. This allows a little more time to deal with the previous
data before its over-written with the new one.
• HOWEVER, the previous data must be read before the new byte
completes. Otherwise, the old data will be lost.
Serial Communication
The SCON Register
MSB LSB
SM0 SM1 SM2 REN TB8 RB8 TI RI

Bit Name Description


SCON.7 SM0 Serial Port Mode bit 0
SCON.6 SM1 Serial Port Mode bit 1
SCON.5 SM2 Multiprocessor Communication Enable
SCON.4 REN Receive Enable (Set to enable reception. CLR to disable reception.)
SCON.3 TB8 Bit 8 of message to transmit (Used to transmit optional parity bit)
SCON.2 RB8 Bit 8 of received message (Receives optional parity bit)
SCON.1 TI Transmit Interrupt Flag (Set when Byte in SBUF is completely
transmitted.)
SCON.0 RI Receive Interrupt Flag (Set when a valid byte is received into
SBUF)
Bit Name Bit Explanation of Function
Address
7 SM0 9Fh Serial port mode bit 0
6 SM1 9Eh Serial port mode bit 1.
5 SM2 9Dh Multiprocessor Communications Enable (explained later)
4 REN 9Ch Receiver Enable. This bit must be set in order to receive
characters.
3 TB8 9Bh Transmit bit 8. The 9th bit to transmit in mode 2 and 3.
2 RB8 9Ah Receive bit 8. The 9th bit received in mode 2 and 3.
1 TI 99h Transmit Flag. Set when a byte has been completely
transmitted.
0 RI 98h Receive Flag. Set when a byte has been completely
received.
SM0 SM1 Serial Explanation Baud Rate
Mode
0 0 0 8-bit Shift Register Oscillator / 12
0 1 1 8-bit UART Set by Timer 1 (*)
1 0 2 9-bit UART Oscillator / 64 (*)
1 1 3 9-bit UART Set by Timer 1 (*)
SCON
• Bits SM0 and SM1 let us set the serial mode to a value between 0
and 3, selecting the Serial Mode selects the mode of operation (8-
bit/9-bit, UART or Shift Register) and also determines how the baud
rate will be calculated. In modes 0 and 2 the baud rate is fixed
based on the oscillators frequency. In modes 1 and 3 the baud rate
is variable based on how often Timer 1 overflows. Well talk more
about the various Serial Modes in a moment.

• The next bit, SM2, is a flag for "Multiprocessor communication."


Generally, whenever a byte has been received the 8051 will set the
"RI" (Receive Interrupt) flag. This lets the program know that a byte
has been received and that it needs to be processed. However,
when SM2 is set the "RI" flag will only be triggered if the 9th bit
received was a "1". That is to say, if SM2 is set and a byte is
received whose 9th bit is clear, the RI flag will never be set. This
can be useful in certain advanced serial applications. For now it is
safe to say that you will almost always want to clear this bit so that
the flag is set upon reception of any character.
•The next bit, REN, is "Receiver Enable." This bit is very
straightforward: If you want to receive data via the serial port, set this
bit. You will almost always want to set this bit.

• The last 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.

• The TB8 bit is used in modes 2 and 3. In modes 2 and 3, a total of


nine data bits are transmitted. The first 8 data bits are the 8 bits of the
main value, and the ninth bit is taken from TB8. If TB8 is set and a
value is written to the serial port, the data bits will be written to the
serial line followed by a "set" ninth bit. If TB8 is clear the ninth bit will be
"clear.“

• The RB8 also operates in modes 2 and 3 and functions essentially


the same way as TB8, but on the reception side. When a byte is
received in modes 2 or 3, a total of nine bits are received. In this case,
the first eight bits received are the data of the serial byte received and
the value of the ninth bit received will be placed in RB8.
• TI means "Transmit Interrupt." When a program writes a value to the
serial port, a certain amount of time will pass before the individual bits
of the byte are "clocked out" the serial port.

• If the program were to write another byte to the serial port before
the first byte was completely output, the data being sent would be
garbled.
• Thus, the 8051 lets the program know that it has "clocked out"
the last byte by setting the TI bit. When the TI bit is set, the
program may assume that the serial port is "free" and ready to
send the next byte.

• The RI bit means "Receive Interrupt." It functions similarly to the "TI"


bit, but it indicates that a byte has been received. That is to say,
whenever the 8051 has received a complete byte it will trigger the RI
bit to let the program know that it needs to read the value quickly,
before another byte is read.
Serial Communication
• Modes of the Serial Port
• Mode 0 – SM0 = SM1 = 0
– Half Duplex Synchronous Operation.
• Data is sent and received (not simultaneously) using the RxD pin.
• The TxD pin carries “the shift clock” during both receiving and
transmitting. (Reference clock for synchronization).
• Data is sent in 8-bit un-framed packets.
– LSB first.
• Data rate is set to 1/12 clock frequency.
– Machine Cycle Frequency.
– Same as the shift clock.
Serial Communication
Mode 0 – Transmission
•Transmission starts as soon as byte is written into SBUF.
•During transmission, each bit stays valid on the RxD pin for one
complete machine cycle.
•The shift clock goes low in the middle of the cycle and returns
high right before the end.
•The TI flag is set when the 8th bit is done transmitting.

Machine Cycle

Clock

Data out

Shift Clock
Mode 0 – Reception

• Reception is initiated as soon as REN bit is set to 1 and the receive interrupt
(RI) bit is cleared.
– Usually, REN is set at the beginning of the program to initialize the
serial port, then RI is cleared to start a data input operation.
• As soon as RI is cleared, the shift clock will be produced on the TxD pin.
– At the beginning of the following machine cycle, data will be clocked in
from the RxD line.
• The clocking occurs on the rising edge of the TxD line.
– After the 8th clocking cycle, the data is copied to SBUF and the RI bit is
set.
Mode 1
• In mode 1, the 8051 serial port operates an 8-bit UART with variable baud
rate.
– The essential operation of a UART is parallel-to-serial conversion of
output data and serial-to-parallel conversion of input data.
• 10 bits are transmitted on TxD or received on RxD.
– Start bit, 8 data bits, 1 stop bit.
• The baud rate is set by the Timer 1 overflow rate.

Mode 1 Transmission

• Transmission starts when anything is written into SBUF.


• The period for each bit is the reciprocal of the baud rate.
• The transmit interrupt (TI) flag is set as soon as the stop bit appears on
TxD.
Mode 1 Reception
• Reception is initiated by a 1-to-0 transition on the RxD line (assuming REN
is 1).
– A divide-by-16 counter is immediately started. The next bit is expected
to arrive at the roll-over of this counter.
– Bits are sampled at the 8th count of this counter.
• “False Start Bit Detection”
– 8 counts after the 1-to-0 transition, the RxD line is sampled again. If it is
not 0, then we have a false start bit. The receiver is reset and waits for
the next 1-to-0 transition.
•Assuming that a valid start bit was detected:
•The start bit is skipped.
•Eight data bits are clocked into the serial port’s register (NOT
SBUF).
•When all eight bits are received:
•The ninth bit (the stop bit) is clocked into RB8
•SBUF is loaded with the right data bits
•The receiver interrupt flag (RI) is set.
•The above three steps only occur if RI was 0 to start with.
•Do not overwrite the previous data if it has not been read.

Mode 2
• The serial port operates as a 9-bit UART with a fixed baud rate.
• 11 bits are transmitted:
– The start bit
– The 8 data bits from SBUF
– A 9th data bit from TB8
– The stop bit
• On reception, the 9th data bit will be placed in RB8.
• The baud rate is fixed at either 1/32 or 1/64 of the oscillator frequency.
Mode 3
•9-Bit UART with Variable Baud Rate.
Combination of modes 1 and 2.
Setting the Serial Port Baud Rate
• Once the Serial Port Mode has been configured, the program must
configure the serial ports baud rate.
•This only applies to Serial Port modes 1 and 3. The Baud Rate is
determined based on the oscillators frequency when in mode 0 and
2.
• In mode 0, the baud rate is always the oscillator frequency divided
by 12. This means if youre crystal is 11.059Mhz, mode 0 baud rate
will always be 921,583 baud.
• In mode 2 the baud rate is always the oscillator frequency divided
by 64, so a 11.059Mhz crystal speed will yield a baud rate of
172,797.
•In modes 1 and 3, the baud rate is determined by how frequently
timer 1 overflows.
•The more frequently timer 1 overflows, the higher the baud rate.

• There are many ways one can cause timer 1 to overflow at a rate
that determines a baud rate, but the most common method is to put
timer 1 in 8-bit auto-reload mode (timer mode 2) and set a reload
value (TH1) that causes Timer 1 to overflow at a frequency
appropriate to generate a baud rate.

• By default, the baud rate in mode 2 is set to 1/64 of the clock


frequency.

However, bit 7 of the PCON (Power Control) Register – known as


SMOD – doubles the baud rate if it is set to 1.

So, if SMOD = 1, the baud rate for mode 2 is 1/32 of the clock
frequency.
• In modes 1 and 3, the baud rate is set by the overflow rate
of Timer 1.

• However, that rate is too high. So, it is divided by 32 (or 16 if


SMOD = 1) to generate the real baud rate.

You can think about as if Timer 1 will be clocked at


XTAL / (12 * 32) = 28,800Hz (when SMOD = 0) or
XTAL / (12 * 16) = 57,600Hz (when SMOD = 1)
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
•To obtain 19,200 baud on a 11.059Mhz crystal we have to set TH1
to 254.5. If we set it to 254 we will have achieved 14,400 baud and if
we set it to 255 we will have achieved 28,800 baud. Thus were
stuck...

• But not quite... to achieve 19,200 baud we simply 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
Here we are able to calculate a nice, even TH1 value. Therefore,
to obtain 19,200 baud with an 11.059MHz crystal we must:

1. Configure Serial Port mode 1 or 3.

2. Configure Timer 1 to timer mode 2 (8-bit auto-reload).

3. Set TH1 to 253 to reflect the correct frequency for 19,200


baud.

4. Set PCON.7 (SMOD) to double the baud rate.


SERIAL COMMUNICATION:

EXAMPLE :

With XTAL = 11.5092MHz, find the TH1 value needed to


have the following baud rates. (a) 9600 (b) 2400 (c)1200

Solution:
With XTAL=11.0592MHz, we have :
The machine cycle frequency of the 8051=11.0592/12=921.6KHz,
and 921.6KHz/32=28,800Hz is the frequency provided by UART to
timer 1 to set baud rate.

(a) 28,800/3 =9600 Where –3=FD (hex) is loaded intoTH1


(b) 28,800/12=2400 Where-12=F4(hex) is loaded into TH1
(c) 28,800/24=1200 Where-24=E8(hex) is loaded into TH1
11.0592MHZ MACHINE
28800HZ
CYCLE
FREQ
DIVIDE BY 32
XTAL OSC DIVIDE BY
12 BY UART

921.6
KHZ
TIMER 1 TH1 VALUES FOR VARIOUS BAUD RATES

BUAD RATE TH1 TH1(HEX)


(DECIMAL)
9600 -3 FD

4800 -6 FA

2400 -12 F4

1200 -24 E8
SBUF register:
It is an eight bit register used for serial communication in 8051.

For a byte to be transformed via TXD line, it must be placed in


SBUF. Similarly, SBUF holds the byte of data when it is
received by 8051’s RXD line.
Steps to Transmit a Byte
1. Program T1 for Mode2 (TMOD  0x20)
2. Load TH1 and TL1 with the initial value (baud rate dependant) (TH1
 FD / FA / F4 / E8)
3. Program SCON for Mode1 (SCON  0x50)
4. Start Timer1 (SETB TR1)
5. Clear TI
6. Load SBUF with the byte to be transferred (SBUF  byte)
7. Wait until TI becomes 1 (JNB TI, not_done)
8. Go back to Step5 for next byte
Examples: Transmit a character

• Transfer ASCII “A” serially at 9600 baud continuously

START: MOV TMOD, #20H ;Put T1 in mode2


MOV TH1, #-3 ;9600 baud
MOV TL1, #-3
MOV SCON, #50H ;8b, 1stop, 1start, REN enabled
SETB TR1 ;start timer T1
AGAIN: CLR TI ;ready to transmit
MOV SBUF, #’A’ ;letter A is to be transmitted
HERE: JNB TI, HERE ;poll TI until all the bits are
transmitted
SJMP AGAIN ;while(1) loop (forever loop)
Steps to Receive a Byte
1. Program T1 for Mode2 (TMOD  0x20)
2. Load TH1 and TL1 with the initial value (baud rate dependant)
(TH1  FD / FA / F4 / E8)
3. Program SCON for Mode1 (SCON  0x50)
4. Start Timer1 (SETB TR1)
5. Clear RI
6. Wait until RI becomes 1 (JNB RI, not_done)
7. Store SBUF (A  SBUF)
8. Go back to Step5 for next byte
Example: Receive Data

• Receive bytes serially and display them on P1, continuously.

START: MOV TMOD, #20H ;T1 in mode 2


MOV TH1, #-3 ;9600 baud
MOV SCON, #50H ;8b, 1start, 1stop
SETB TR1 ;start T1
AGAIN: CLR RI ;ready to receive a byte
HERE: JNB RI, HERE ;wait until one byte is Rx-ed
MOV A, SBUF ;read the received byte from SBUF
MOV P1, A ;display on P1
SJMP AGAIN ;while (1)

You might also like