8051 - Serial Communication
8051 - Serial Communication
8051 - Serial Communication
transmitter receiver
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
sync sync d d
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).
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.
Digital data communication uses modem and & standard phone lines.
Max 232:
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 (10), LSB, …, MSB, <parity bit>, Stop bit (01),
<2nd stop bit (1)>
• 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
• 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.
• 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.
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
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.
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.
If PCON.7 is set then the baud rate is effectively doubled, thus the
equation becomes:
• 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:
EXAMPLE :
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.
921.6
KHZ
TIMER 1 TH1 VALUES FOR VARIOUS BAUD RATES
4800 -6 FA
2400 -12 F4
1200 -24 E8
SBUF register:
It is an eight bit register used for serial communication in 8051.