Serial Port Communication
Serial Port Communication
Time
3
Framing The SBUF Register
• An 8-bit message needs to be “framed” so that the receiver can • SBUF is 8 bit register.
detect correctly its beginning and end. • SBUF is actually two separate registers at the same address.
• Standard framing: • Write-only transmit register.
• Start bit – always 0, Stop bit – always 1. • Read-only receive register.
• Optional parity bit • Cannot read back what was sent for transmission.
• Stop bit can be one or two bits • The byte to be transmitted on the serial port is “written” into SBUF.
• The message now becomes: • Serial transmission starts immediately.
• Start bit (1 0), LSB, …, MSB, <parity bit>, Stop bit (0 1), • The byte received from the serial port will be stored in SBUF once the
<2nd stop bit (1)> last bit is received.
• This is called “double buffering”.
RS-232 Protocol • Received data is buffered in the serial port itself until the full byte is
• Serial communication standard for small computing systems. received. This allows a little more time to deal with the previous data
• Original intent was for communication between small computer. Mostly before its over-written with the new one.
used • HOWEVER, the previous data must be read before the new byte
for communication with slow peripherals. completes. Otherwise, the old data will be lost.
• The cable connecting the PC to the Kit in the lab follows this standard.
• Defines many signals – about 25 – however, only three are used in MCS-51 Serial Port
most cases. • MCS-51 has a full-duplex serial port that can be used as a normal
• RxD – Received Data serial interface (non-framed) or as an internal UART (framed).
• TxD – Transmitted Data • This serial port controls the RxD and TxD dual functions for pins
• GND – Common Ground P3.0 and P3.1.
• The MCS-51 serial port is controlled using the SCON SFR (98H).
RS-232 Line Driver • The MCS-51 serial port communicates with the rest of the chip
• RS-232 requires non-TTL compatible voltage levels using
• -3 to –25 for 1 and +3 to +25 for 0 the SBUF SFR (99H).
• Therefore, we need an interface to connect to standard TTL chips.
• MAX 232 and MAX 233 chips.
• Accept standard TTL logic levels and produce RS-232 levels.
• Utilize a normal +5 V supply.
Mode 1
Modes of the Serial Port • In mode 1, the 8051 serial port operates an 8-bit UART with variable
• Mode 0 – SM0 = SM1 = 0 baud rate.
• Half Duplex Synchronous Operation. • The essential operation of a UART is parallel-to-serial conversion of output
• Data is sent and received (not simultaneously) using the RxD pin. data and serial-to-parallel conversion of input data.
• The TxD pin carries “the shift clock” during both receiving • 10 bits are transmitted on TxD or received on RxD.
and transmitting. (Reference • Start bit, 8 data bits, 1 stop bit.
clock for synchronization). • The baud rate is set by the Timer 1 overflow rate.
• Data is sent in 8-bit un-framed packets.
• LSB first. Mode 1 Transmission
• Data rate is set to 1/12 clock frequency. • Transmission starts when anything is written into SBUF.
• Machine Cycle Frequency. • The period for each bit is the reciprocal of the baud rate.
• Same as the shift clock. • The transmit interrupt (TI) flag is set as soon as the stop bit appears
on TxD.
Mode 0 – Transmission
• Transmission starts as soon as byte is written into SBUF. Mode 1 Reception
• During transmission, each bit stays valid on the RxD pin for one • Reception is initiated by a 1-to-0 transition on the RxD line (assuming
complete machine cycle. REN is 1).
• The shift clock goes low in the middle of the cycle and returns high right • A divide-by-16 counter is immediately started. The next bit is expected to
before the end. arrive at the roll-over of this counter.
• The TI flag is set when the 8th bit is done transmitting • Bits are sampled at the 8th count of this counter.
• “False Start Bit Detection”
Mode 0 – Reception • 8 counts after the 1-to-0 transition, the RxD line is samples again. If it is not 0,
• Reception is initiated as soon as REN bit is set to 1 and the receive then we have a false start bit. The receiver is reset and waits for the next 1-to-
interrupt (RI) bit is cleared. 0 transition
• 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. • Assuming that a valid start bit was detected:
• As soon as RI is cleared, the shift clock will be produced on the TxD • The start bit is skipped.
pin. • Eight data bits are clocked into the serial port’s register (NOT SBUF).
• At the beginning of the following machine cycle, data will be clocked in from • When all eight bits are received:
the RxD line. • The ninth bit (the stop bit) is clocked into RB8
• The clocking occurs on the rising edge of the TxD line. • SBUF is loaded with the right data bits
• After the 8th clocking cycle, the data is copied to SBUF and the RI bit is set. • 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 Setting Timer 1 to Generate Baud Rate
• The serial port operates as a 9-bit UART with a fixed baud rate. • How do we produce a baud rate of 1200 using an 8051 with a clock
• 11 bits are transmitted: frequency of 12 MHz?
• The start bit • Baud Rate = 28.8K / count
• The 8 data bits from SBUF • Or 56.6K / count
• A 9th data bit from TB8 • To produce 1200 baud, we need to set timer 1 to count for: 28.8K / 1200 = 24
• The stop bit counts.
• On reception, the 9th data bit will be placed in RB8. • Set Timer 1 to operate in mode 2 (auto-reload) and set TH1 to 0E8H (-24).
• The baud rate is fixed at either 1/32 or 1/64 of the oscillator frequency.
Steps to Transmit a Byte
Mode 3 • Program T1 for Mode2 (TMOD = 0x20)
• 9-Bit UART with Variable Baud Rate. • Load TH1 with the initial value (baud rate dependant) (TH1 = FD / FA /
• Combination of modes 1 and 2. F4 / E8)
• Program SCON for Mode1 (SCON 0x50)
The Baud Rates • Start Timer1 TR=1
• In Mode 0, the baud rate is fixed at the clock frequency divided by 12. • Clear TI
• By default, the baud rate in mode 2 is set to 1/64 of the clock • Load SBUF with the byte to be transferred (SBUF byte)
frequency. • Wait until TI becomes 1 while (T1==0); T1=0;
• However, bit 7 of the PCON (Power Control) Register – known as SMOD – • Go back to Step5 for next byte
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)
Baud Rate setting
(Assuming XTAL=11.0592MHz
22