AN INTRODUCTION TO
SERIAL PORT
INTERFACING
An Introduction to Serial
communication
As the name implies, in the Serial
communication you send bits of data serially
i.e. one bit at a time.
Normally we predefine rate of transfer such
as
2400 bits/sec. (2400bps)
56,000 bits/sec. (56 kbps)
And then depending upon this rate we
interpret bits boundaries.
Example of Serial Waveform
For example you receive following waveform
from serial port of your PC and it is stated that
data rate is 2400bps
Methods of timing used to find bit
boundaries
Synchronous communication
Asynchronous Communication
In Asynchronous communication the sender
and receiver decide a data rate before
communication. They decide upon signaling
used for start & stop of data transmission.
Both sender & receiver use a precise timing
reference internally to divide serial received
data into bits.
Basics of Serial Communication
Baud Rate
The baud rate is simply “the rate of data transmission
expressed in bits per second, kilo Bits per second or
Mega bits per second etc”.
The sender & receiver decide upon no of bits in one data
word such as 8 bits (1byte) etc. More over they decide
that:
A first bit before transmission of data word will always be
0 (or 1) after which data bits will follow. It is called start
bit.
The last bit followed by data bits will always be 1(or 0)
after which it requires start bit for transmission of next
word. This bit is called stop bit
Advantage of Start / Stop bits
Some measures for error detection
Parity
Is of two types
1. Even parity
2. Odd parity
Suppose your data word is 8 bit in length i.e. one byte.
The sender, before transmitting byte, determines whether the no of bits
in the byte to be sent are even. Suppose sender’s wants to send
10011101
In which there are 5 “ 1’s ” and 3 “ 0’s ”.
To keep the no. of 1’s even the sender adds an extra bit at the end of
byte so that the total no. of 1’s are 6 (an even no.). This extra bits is
called parity bit. Since this bit keeps the no. of 1’s even, so it is called
even parity.
Overview of total bits with Parity
Odd Parity
Odd parity is added to keep the no. of 1’s odd in transmission. For
example for above case of data being
10011101
The odd parity will be added as 0 as no of 1’s are already 5 (an odd
no.).
Failure of Parity
What if you transmit above byte 10011101 with
even parity being used and on receiver side you
receive
10000101 1
shows bits inverted during transmission
On receiver side the no. of 1’s including parity=4
(no error for even parity). But actually it is false.
So parity does not ensure error detection in all
cases.
EIA RS232C Serial Interface
Standard
A “Space” (logic 0) will be between 3 and 25 volts.
A “ Mark” (logic 1) will be between -3 and -25 volts.
The region between 3 & -3 volts is undefined.
Maximum data rates may be up to 20 kbps.
Maximum serial cable length may be 15 meters.
The reason to study RS-232C is that the serial part (Com
port) found in PC’S uses this standard.
Above are the sufficient points to start implementation of
the serial interfacing on PC.
Serial / RS-232 Port on PC & its
Pin Out
Introduction of UART & USART
UART – Stands for Universal Asynchronous
Receiver Transmitter
USART – Stands for Universal Synchronous
Asynchronous Receiver Transmitter
In RS-232 we implement serial port with UART
Actually UART receives/sends data to
microprocessor through data bus. The remaining
part of signal handing of RS-232 is done by UART
i.e. start bit, stop bit, parity etc.
Port Address of Serial Part
Name Address IRQ
COM 1 3F8-3FF 4
COM 2 2F8-2FF 3
COM 3 3E8-3EF 4
COM 4 2E8-3FF 3
Configuration of Serial Port
Base DLAB Read/ Abr. Registered Mar--
Address Write
+0 0 W - Transmit holding buffer
0 R - Receive Buffer
1 R/W - Divisor latch low byte
+1 0 R/W IER Interrupt enable register
1 R/W Divisor latch high byte
+2 - R IIR Interrupt Identification Register
- W FCR FIFO Control register.
+3 - R/W LCR Line Control Register modem Control
+4 - R/W MCR Register
+5 - R LSR Line Status Register Modem Status
+6 - R MSR Register
+7 - R/W - Scratch Register.
Setting Baud Rate
Usually the internal clock frequencies of UART=115, 200
Hz.
Suppose we want to set baud rate to 2400 bps.
Now we find the quotient.
115,200/2400=48=0x30
Taking it as 16 bit no the quotient is =0x00 high byte 0x30
low byte
Now we set a bit DLAB (divisor latch access bit) which is
bit 7 of the line control register (LCR)at base address +3
Usually the command is
outportb(0x3f8 +3,inportb (0x3f8+3)|0x80);
Setting Baud Rate (Contd.)
After setting DLAB bit in LCR, the function of registers at 0x3F8 &
0x3F9 changes as follows
Base Address Normal Function Function When DLAB=1
Ox3F8 Tx/Rx register Divisor latch Low byte
Ox3F9 Interrupt enable register Divisor latch high byte.
For baud rate = 2400bps
Divisor Latch Low byte = 0x30
Divisor Latch high byte= 0x00
Loading these registers with above values configures your serial port
for baud rate of 2400bps.
Specifying Start / Stop Bit & Parity
The functionality of each bit of line control
register (LCR) is shown in table below:
Bit # Functional Description
7 1 Divisor Latch Access Bit ( DLAB ON )
0 DLAB=0, Access to Tx/Rx & IER for normal operation
6 Set Brake Enable
3:5 Bit 5 Bit 4 Bit 3 Parity Select
x x 0 No Parity
0 0 1 Odd Parity
0 1 1 Even Parity
1 0 1 High Parity ( Sticky )
1 1 1 Low Parity ( Sticky )
2 Length of stop bit
0 One stop bit
1 2 stop bits for words of length 6, 7 or 8 bits
1.5 stop bits for words of length 5 bits
0:1 Bit 1 Bit 0 Word Length
0 0 5 Bits
0 1 6 Bits
1 0 7 Bits
1 1 8 Bits
The Function of various pins on
Serial Port
Pin # on Pin Function
DB – 9 Symbol
1 CD Carrier Detect: It is used by Modem to inform PC that it has
detected Carrier on Phone Line.
2 RD Serial data is received on this line by PC.
3 TD Serial Data is transmitted on this pin by PC.
4 DTR Data Terminal Ready
When terminal (computer) powers up it asserts DTR high.
5 SG It is signal ground with reference to which voltages are
interpreted as high or low.
6 DSR Data Set Ready.
When modem powers up it asserts DSR high.
7 RTS Request to Send.
Request to send is sent from (DTE) terminal (PC) to modem
(DCE) to inform it that PC wants to send some data to
modem.
The Function of various pins on
Serial Port (Contd)
8 CTS Clear To Send.
Upon received RTS from DTE (PC),
the modem (DCE) asserts CTS high
whenever it is ready to receive data.
9 RI Ring Indicator.
It is set by modem to indicate the PC
that a ringing signal has been detected
on line.
Common way of Handling Flow
Control Signals.
PC1 Serial Port PC2 Serial Port
DB 9 Pins DB 9 pins
3 TD RD 2
2 RD TD 3
5 SG SG 5
4 DTR DTR 4
6 DSR DSR 6
1 CD CD 1
7 RTS RTS 7
8 CTS CTS 8
9 Open Ckt. 9
Details of remaining registers
To study details of each bit of remaining
registers of serial port you can refer to the
documentation provided in the document
“Interfacing the Serial /RS232 port”
found on www.beyondlogic.org by Craig C
Peacock
A discussion of important registers follows:
Modem Control Register (MCR)
Bit Notes
Bit 7 Reserved
Bit 6 Reserved
Bit 5 Autoflow Control Enabled (16750
only)
Bit 4 LoopBack Mode
Bit 3 Aux Output 2
Bit 2 Aux Output 1
Bit 1 Force Request to Send
Bit 0 Force Data Terminal Ready
Modem Status Register (MSR)
Bit Notes
Bit 7 Carrier Detect
Bit 6 Ring Indicator
Bit 5 Data Set Ready
Bit 4 Clear To Send
Bit 3 Delta Data Carrier Detect
Bit 2 Trailing Edge Ring Indicator
Bit 1 Delta Data Set Ready
Bit 0 Delta Clear to Send
Line Status Register (LSR)
Bit Notes
Bit 7 Error in Received FIFO
Bit 6 Empty Data Holding Registers
Bit 5 Empty Transmitter Holding
Register
Bit 4 Break Interrupt
Bit 3 Framing Error
Bit 2 Parity Error
Bit 1 Overrun Error
Bit 0 Data Ready
Writing a C Program to Send
Serial Data to Micro-Controller for
#include <stdio.h>
Motor Control
#include <conio.h>
#define PORT1 0x3F8
void main(void)
{
int c;
outportb(PORT1+1,0);/* Turn off interrupts*/
/* of Port1 to use software testing mode*/
/* Now we set baud rate to 2400 bps */
outportb(PORT1+3,0x80); /* SET DLAB ON */
outportb(PORT1+0,0x30);
/*Divisor Latch Low Byte */
outportb(PORT1 + 1 , 0x00);
/*Divisor Latch High Byte */
outportb(PORT1+3,0x03);
/* 8 Bits, No Parity, 1 Stop Bit */
printf("\n Motor Speed Control Program\n");
printf(" Enter 27 to quit \n");
do {
printf("\n Please enter a number between 0 to 255
except 27 : ");
scanf("%d",&c);
printf("\n");
outportb(PORT1,c);
/* Send the input number to Serial Port */
} while (c !=27);
/* Quit when 27 is entered */
}
Introduction to USB
USB –Universal Serial Bus
A port to provide Plug and Play peripheral
connectivity.
Developed and Standardized by joint
venture of five major companies Compaq,
DEC, IBM, Intel, Microsoft, NEC and
Northern Telecom .
An expandable bus connectivity.
Facilitates use of Bus Powered Devices
Advantages offered by USB
USB provide fast data rate of transmission
from 1.5 Mb/sec to 12 megabit/s
Single model for cabling and connectors
Self identifying peripherals, automatic
mapping of function to driver, and
configuration
Dynamically attachable and reconfigurable
peripherals
USB implementation model