Serial Port

  • May 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Serial Port as PDF for free.

More details

  • Words: 1,772
  • Pages: 31
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 COM 1 COM 2 COM 3 COM 4

Address 3F8-3FF 2F8-2FF 3E8-3EF 2E8-3FF

IRQ 4 3 4 3

Configuration of Serial Port Base Address

DLAB

Read/ Write

Abr.

Registered Mar--

+0

0 0 1

W R R/W

-

Transmit holding buffer Receive Buffer Divisor latch low byte

+1

0 1

R/W R/W

IER

Interrupt enable register Divisor latch high byte

+2

-

R W

IIR FCR

Interrupt Identification Register

+3 +4 +5 +6 +7

-

R/W R/W R R R/W

LCR MCR LSR MSR -

Line Control Register modem Control Register Line Status Register Modem Status Register Scratch Register.

FIFO Control 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 0x30low 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 1

0 1 0 1

1 1 1 1

Odd Parity Even Parity High 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

0 0 1 1

0 1 0 1

5 Bits 6 Bits 7 Bits 8 Bits

The Function of various pins on Serial Port Pin # on DB – 9

Pin Symbol

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.

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 DB 9 Pins  3 TD  2 RD  5 SG  4 DTR  6 DSR  1 CD  7 RTS  8 CTS  9 Open Ckt.

PC2 Serial Port DB 9 pins RD 2 TD 3 SG 5 DTR 4 DSR 6 CD 1 RTS 7 CTS 8 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  Bit 7  Bit 6  Bit 5     

Bit 4 Bit 3 Bit 2 Bit 1 Bit 0

Notes Reserved Reserved Autoflow Control Enabled (16750 only) LoopBack Mode Aux Output 2 Aux Output 1 Force Request to Send Force Data Terminal Ready

Modem Status Register (MSR) Bit  Bit 7  Bit 6  Bit 5  Bit 4  Bit 3  Bit 2  Bit 1  Bit 0

Notes Carrier Detect Ring Indicator Data Set Ready Clear To Send Delta Data Carrier Detect Trailing Edge Ring Indicator Delta Data Set Ready Delta Clear to Send

Line Status Register (LSR) Bit  Bit 7  Bit 6  Bit 5     

Bit 4 Bit 3 Bit 2 Bit 1 Bit 0

Notes Error in Received FIFO Empty Data Holding Registers Empty Transmitter Holding Register Break Interrupt Framing Error Parity Error Overrun Error Data Ready

Writing a C Program to Send Serial Data to Micro-Controller for Motor Control #include <stdio.h> #include #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

Related Documents