AN INTRODUCTION TO SERIAL PORT INTERFACING

32
AN INTRODUCTION TO AN INTRODUCTION TO SERIAL PORT SERIAL PORT INTERFACING INTERFACING

description

AN INTRODUCTION TO SERIAL PORT INTERFACING- www.onlineTPS.com

Transcript of AN INTRODUCTION TO SERIAL PORT INTERFACING

Page 1: AN INTRODUCTION TO SERIAL PORT INTERFACING

AN INTRODUCTION TO AN INTRODUCTION TO SERIAL PORT SERIAL PORT INTERFACINGINTERFACING

Page 2: AN INTRODUCTION TO SERIAL PORT INTERFACING

An Introduction to Serial An Introduction to Serial communicationcommunication

As the name implies, in the Serial As the name implies, in the Serial communication you send bits of data serially communication you send bits of data serially i.e. one bit at a time.i.e. one bit at a time.

Normally we predefine rate of transfer such Normally we predefine rate of transfer such asas

2400 bits/sec. (2400bps)2400 bits/sec. (2400bps) 56,000 bits/sec. (56 kbps)56,000 bits/sec. (56 kbps) And then depending upon this rate we And then depending upon this rate we

interpret bits boundaries.interpret bits boundaries.

Page 3: AN INTRODUCTION TO SERIAL PORT INTERFACING

Example of Serial WaveformExample of Serial Waveform

For example you receive following waveform For example you receive following waveform from serial port of your PC and it is stated that from serial port of your PC and it is stated that data rate is 2400bpsdata rate is 2400bps

Page 4: AN INTRODUCTION TO SERIAL PORT INTERFACING

Methods of timing used to find bit Methods of timing used to find bit boundariesboundaries

Synchronous communicationSynchronous communication

Page 5: AN INTRODUCTION TO SERIAL PORT INTERFACING

Asynchronous CommunicationAsynchronous Communication

In Asynchronous communication the sender In Asynchronous communication the sender and receiver decide a data rate before and receiver decide a data rate before communication. They decide upon signaling communication. They decide upon signaling used for start & stop of data transmission.used for start & stop of data transmission.

Both sender & receiver use a precise timing Both sender & receiver use a precise timing reference internally to divide serial received reference internally to divide serial received data into bits.data into bits.

Page 6: AN INTRODUCTION TO SERIAL PORT INTERFACING

Basics of Serial CommunicationBasics of Serial Communication

Baud RateBaud Rate The baud rate is simply “the rate of data transmission The baud rate is simply “the rate of data transmission

expressed in bits per second, kilo Bits per second or expressed in bits per second, kilo Bits per second or Mega bits per second etc”.Mega bits per second etc”.

The sender & receiver decide upon no of bits in one data The sender & receiver decide upon no of bits in one data word such as 8 bits (1byte) etc. More over they decide word such as 8 bits (1byte) etc. More over they decide that:that:

A first bit before transmission of data word will always be A first bit before transmission of data word will always be 0 (or 1) after which data bits will follow. It is called 0 (or 1) after which data bits will follow. It is called start start bit.bit.

The last bit followed by data bits will always be 1(or 0) The last bit followed by data bits will always be 1(or 0) after which it requires start bit for transmission of next after which it requires start bit for transmission of next word. This bit is called word. This bit is called stop bitstop bit

Page 7: AN INTRODUCTION TO SERIAL PORT INTERFACING

Advantage of Start / Stop bitsAdvantage of Start / Stop bits

Page 8: AN INTRODUCTION TO SERIAL PORT INTERFACING

Some measures for error detection Some measures for error detection

ParityParity Is of two typesIs of two types 1. Even parity1. Even parity 2. Odd parity2. Odd parity Suppose your data word is 8 bit in length i.e. one byte.Suppose your data word is 8 bit in length i.e. one byte. The sender, before transmitting byte, determines whether the no of bits The sender, before transmitting byte, determines whether the no of bits

in the byte to be sent are even. Suppose sender’s wants to sendin the byte to be sent are even. Suppose sender’s wants to send 1001110110011101 In which there are 5 “ 1’s ” and 3 “ 0’s ”.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 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 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 called parity bit. Since this bit keeps the no. of 1’s even, so it is called even parity.even parity.

Page 9: AN INTRODUCTION TO SERIAL PORT INTERFACING

Overview of total bits with ParityOverview of total bits with Parity

Odd ParityOdd parity is added to keep the no. of 1’s odd in transmission. For example for above case of data being

10011101The odd parity will be added as 0 as no of 1’s are already 5 (an odd no.).

Page 10: AN INTRODUCTION TO SERIAL PORT INTERFACING

Failure of ParityFailure of Parity

What if you transmit above byte 10011101 with What if you transmit above byte 10011101 with even parity being used and on receiver side you even parity being used and on receiver side you receivereceive

1001000000101 1101 1 shows bits inverted during transmissionshows bits inverted during transmission On receiver side the no. of 1’s including parity=4 On receiver side the no. of 1’s including parity=4

(no error for even parity). But actually it is false.(no error for even parity). But actually it is false. So parity does not ensure error detection in all So parity does not ensure error detection in all

cases.cases.

Page 11: AN INTRODUCTION TO SERIAL PORT INTERFACING

EIA RS232C Serial Interface EIA RS232C Serial Interface StandardStandard

A “Space” (logic 0) will be between 3 and 25 volts.A “Space” (logic 0) will be between 3 and 25 volts. A “ Mark” (logic 1) 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.The region between 3 & -3 volts is undefined. Maximum data rates may be up to 20 kbps.Maximum data rates may be up to 20 kbps. Maximum serial cable length may be 15 meters.Maximum serial cable length may be 15 meters. The reason to study RS-232C is that the serial part (Com The reason to study RS-232C is that the serial part (Com

port) found in PC’S uses this standard.port) found in PC’S uses this standard. Above are the sufficient points to start implementation of Above are the sufficient points to start implementation of

the serial interfacing on PC.the serial interfacing on PC.

Page 12: AN INTRODUCTION TO SERIAL PORT INTERFACING

Serial / RS-232 Port on PC & its Serial / RS-232 Port on PC & its Pin OutPin Out

Page 13: AN INTRODUCTION TO SERIAL PORT INTERFACING

Introduction of UART & USARTIntroduction of UART & USART

UART – Stands for Universal Asynchronous UART – Stands for Universal Asynchronous Receiver TransmitterReceiver Transmitter

USART – Stands for Universal Synchronous USART – Stands for Universal Synchronous Asynchronous Receiver TransmitterAsynchronous Receiver Transmitter

In RS-232 we implement serial port with UARTIn RS-232 we implement serial port with UART Actually UART receives/sends data to Actually UART receives/sends data to

microprocessor through data bus. The remaining microprocessor through data bus. The remaining part of signal handing of RS-232 is done by UART part of signal handing of RS-232 is done by UART i.e. start bit, stop bit, parity etc.i.e. start bit, stop bit, parity etc.

Page 14: AN INTRODUCTION TO SERIAL PORT INTERFACING

Port Address of Serial PartPort Address of Serial Part

NameName AddressAddress IRQIRQ COM 1COM 1 3F8-3FF3F8-3FF 44 COM 2COM 2 2F8-2FF2F8-2FF 33 COM 3COM 3 3E8-3EF3E8-3EF 44 COM 4COM 4 2E8-3FF2E8-3FF 33

Page 15: AN INTRODUCTION TO SERIAL PORT INTERFACING

Configuration of Serial PortConfiguration of Serial Port

Base Address

DLAB Read/Write

Abr. Registered Mar--

+0 001

WR

R/W

---

Transmit holding bufferReceive Buffer Divisor latch low byte

+1 01

R/WR/W

IER Interrupt enable registerDivisor latch high byte

+2 --

RW

IIRFCR

Interrupt Identification Register

FIFO Control register.

+3+4+5+6+7

-----

R/WR/W

RR

R/W

LCRMCRLSRMSR

-

Line Control Register modem Control Register

Line Status Register Modem Status Register

Scratch Register.

Page 16: AN INTRODUCTION TO SERIAL PORT INTERFACING

Setting Baud RateSetting Baud Rate

Usually the internal clock frequencies of UART=115, 200 Usually the internal clock frequencies of UART=115, 200 Hz.Hz.

Suppose we want to set baud rate to 2400 bps.Suppose we want to set baud rate to 2400 bps. Now we find the quotient.Now we find the quotient. 115,200/2400=48=0x30115,200/2400=48=0x30 Taking it as 16 bit no the quotient is =0x00 high byte 0x30 Taking it as 16 bit no the quotient is =0x00 high byte 0x30

low bytelow byte Now we set a bit DLAB (divisor latch access bit) which is Now we set a bit DLAB (divisor latch access bit) which is

bit 7 of the line control register (LCR)at base address +3bit 7 of the line control register (LCR)at base address +3 Usually the command is Usually the command is outportb(0x3f8 +3,inportb (0x3f8+3)|0x80);outportb(0x3f8 +3,inportb (0x3f8+3)|0x80);

Page 17: AN INTRODUCTION TO SERIAL PORT INTERFACING

Setting Baud Rate (Contd.)Setting Baud Rate (Contd.) After setting DLAB bit in LCR, the function of registers at 0x3F8 & After setting DLAB bit in LCR, the function of registers at 0x3F8 &

0x3F9 changes as follows0x3F9 changes as follows

Base AddressBase Address Normal FunctionNormal Function Function When Function When DLAB=1DLAB=1

Ox3F8Ox3F8 Tx/Rx register Tx/Rx register Divisor latch Low byteDivisor latch Low byte Ox3F9Ox3F9 Interrupt enable registerInterrupt enable register Divisor latch high byte.Divisor latch high byte.

For baud rate = 2400bpsFor baud rate = 2400bps Divisor Latch Low byte = 0x30Divisor Latch Low byte = 0x30 Divisor Latch high byte= 0x00Divisor Latch high byte= 0x00 Loading these registers with above values configures your serial port Loading these registers with above values configures your serial port

for baud rate of 2400bps.for baud rate of 2400bps.

Page 18: AN INTRODUCTION TO SERIAL PORT INTERFACING

Specifying Start / Stop Bit & ParitySpecifying Start / Stop Bit & Parity

The functionality of each bit of line control The functionality of each bit of line control register (LCR) is shown in table below:register (LCR) is shown in table below:

Page 19: AN INTRODUCTION TO SERIAL PORT INTERFACING

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

0011

0101

1111

Odd ParityEven ParityHigh Parity ( Sticky )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

0011

0101

5 Bits6 Bits7 Bits8 Bits

Page 20: AN INTRODUCTION TO SERIAL PORT INTERFACING

The Function of various pins on The Function of various pins on Serial PortSerial Port

Pin # onDB – 9

PinSymbol

Function

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.

Page 21: AN INTRODUCTION TO SERIAL PORT INTERFACING

The Function of various pins on The Function of various pins on Serial Port (Contd)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.

Page 22: AN INTRODUCTION TO SERIAL PORT INTERFACING

Common way of Handling Flow Common way of Handling Flow Control Signals.Control Signals.

PC1 Serial PortPC1 Serial Port PC2 Serial PortPC2 Serial Port DB 9 Pins DB 9 Pins DB 9 pins DB 9 pins

33 TDTD RDRD 22 22 RDRD TDTD 33 55 SGSG SGSG 55 44 DTRDTR DTR 4DTR 4 6 6 DSRDSR DSR 6DSR 6 11 CDCD CDCD 11 77 RTSRTS RTS 7RTS 7 88 CTSCTS CTSCTS 88 99 Open Ckt.Open Ckt. 99

Page 23: AN INTRODUCTION TO SERIAL PORT INTERFACING

Details of remaining registersDetails of remaining registers

To study details of each bit of remaining To study details of each bit of remaining registers of serial port you can refer to the registers of serial port you can refer to the documentation provided in the documentdocumentation provided in the document

““Interfacing the Serial /RS232 port”Interfacing the Serial /RS232 port” found on found on www.beyondlogic.org by Craig C by Craig C

Peacock Peacock A discussion of important registers follows:A discussion of important registers follows:

Page 24: AN INTRODUCTION TO SERIAL PORT INTERFACING

Modem Control Register (MCR)Modem Control Register (MCR)

Bit Bit NotesNotes Bit 7 Bit 7 ReservedReserved Bit 6 Bit 6 ReservedReserved Bit 5 Bit 5 Autoflow Control Enabled (16750 Autoflow Control Enabled (16750

only)only) Bit 4 Bit 4 LoopBack ModeLoopBack Mode Bit 3 Bit 3 Aux Output 2Aux Output 2 Bit 2 Bit 2 Aux Output 1Aux Output 1 Bit 1 Bit 1 Force Request to SendForce Request to Send Bit 0 Bit 0 Force Data Terminal Ready Force Data Terminal Ready

Page 25: AN INTRODUCTION TO SERIAL PORT INTERFACING

Modem Status Register (MSR)Modem Status Register (MSR)

Bit Bit NotesNotes Bit 7 Bit 7 Carrier DetectCarrier Detect Bit 6 Bit 6 Ring IndicatorRing Indicator Bit 5 Bit 5 Data Set ReadyData Set Ready Bit 4Bit 4 Clear To SendClear To Send Bit 3 Bit 3 Delta Data Carrier DetectDelta Data Carrier Detect Bit 2Bit 2 Trailing Edge Ring IndicatorTrailing Edge Ring Indicator Bit 1 Bit 1 Delta Data Set ReadyDelta Data Set Ready Bit 0 Bit 0 Delta Clear to SendDelta Clear to Send

Page 26: AN INTRODUCTION TO SERIAL PORT INTERFACING

Line Status Register (LSR)Line Status Register (LSR)

Bit Bit NotesNotes Bit 7 Bit 7 Error in Received FIFOError in Received FIFO Bit 6Bit 6 Empty Data Holding RegistersEmpty Data Holding Registers Bit 5Bit 5 Empty Transmitter Holding Empty Transmitter Holding

RegisterRegister Bit 4Bit 4 Break InterruptBreak Interrupt Bit 3 Bit 3 Framing ErrorFraming Error Bit 2 Bit 2 Parity ErrorParity Error Bit 1 Bit 1 Overrun ErrorOverrun Error Bit 0 Bit 0 Data ReadyData Ready

Page 27: AN INTRODUCTION TO SERIAL PORT INTERFACING

Writing a C Program to Send Writing a C Program to Send Serial Data to Micro-Controller for Serial Data to Micro-Controller for

Motor ControlMotor Control#include <stdio.h>#include <stdio.h>#include <conio.h>#include <conio.h>#define PORT1 0x3F8#define PORT1 0x3F8void main(void)void main(void){{int c;int c;outportb(PORT1+1,0);/* Turn off interrupts*/outportb(PORT1+1,0);/* Turn off interrupts*//* of Port1 to use software testing mode*//* of Port1 to use software testing mode*//* Now we set baud rate to 2400 bps *//* Now we set baud rate to 2400 bps */outportb(PORT1+3,0x80); /* SET DLAB ON */outportb(PORT1+3,0x80); /* SET DLAB ON */outportb(PORT1+0,0x30);outportb(PORT1+0,0x30);/*Divisor Latch Low Byte *//*Divisor Latch Low Byte */outportb(PORT1 + 1 , 0x00);outportb(PORT1 + 1 , 0x00);/*Divisor Latch High Byte *//*Divisor Latch High Byte */

Page 28: AN INTRODUCTION TO SERIAL PORT INTERFACING

outportb(PORT1+3,0x03);outportb(PORT1+3,0x03);/* 8 Bits, No Parity, 1 Stop Bit *//* 8 Bits, No Parity, 1 Stop Bit */printf("\n Motor Speed Control Program\n");printf("\n Motor Speed Control Program\n");printf(" Enter 27 to quit \n");printf(" Enter 27 to quit \n");do {do { printf("\n Please enter a number between 0 to 255 printf("\n Please enter a number between 0 to 255

except 27 : ");except 27 : "); scanf("%d",&c);scanf("%d",&c); printf("\n");printf("\n"); outportb(PORT1,c);outportb(PORT1,c);/* Send the input number to Serial Port *//* Send the input number to Serial Port */ } while (c !=27);} while (c !=27); /* Quit when 27 is entered *//* Quit when 27 is entered */}}

Page 29: AN INTRODUCTION TO SERIAL PORT INTERFACING

Introduction to USBIntroduction to USB

USB –Universal Serial BusUSB –Universal Serial Bus A port to provide Plug and Play peripheral A port to provide Plug and Play peripheral

connectivity.connectivity. Developed and Standardized by joint Developed and Standardized by joint

venture of five major companies Compaq, venture of five major companies Compaq, DEC, IBM, Intel, Microsoft, NEC and DEC, IBM, Intel, Microsoft, NEC and Northern Telecom .Northern Telecom .

An expandable bus connectivity.An expandable bus connectivity. Facilitates use of Bus Powered DevicesFacilitates use of Bus Powered Devices

Page 30: AN INTRODUCTION TO SERIAL PORT INTERFACING

Advantages offered by USBAdvantages offered by USB

USB provide fast data rate of transmission USB provide fast data rate of transmission from 1.5 Mb/sec to 12 megabit/s from 1.5 Mb/sec to 12 megabit/s

Single model for cabling and connectorsSingle model for cabling and connectors Self identifying peripherals, automatic Self identifying peripherals, automatic

mapping of function to driver, and mapping of function to driver, and configurationconfiguration

Dynamically attachable and reconfigurable Dynamically attachable and reconfigurable peripheralsperipherals

Page 31: AN INTRODUCTION TO SERIAL PORT INTERFACING

USB implementation modelUSB implementation model

Page 32: AN INTRODUCTION TO SERIAL PORT INTERFACING

www.onlineTPS.com Components At Your Doorstep…Components At Your Doorstep… Total Project SolutionsTotal Project Solutions

E7/83F, Ashoka SocietyE7/83F, Ashoka SocietyArera Colony,Bhopal 462016(M.P.),IndiaArera Colony,Bhopal 462016(M.P.),India+91-9826015410/9826050109+91-9826015410/9826050109+91-755-2420735+91-755-2420735