An Introduction To Serial Port Interfacing

  • Uploaded by: omar farooq
  • 0
  • 0
  • December 2019
  • 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 An Introduction To Serial Port Interfacing as PDF for free.

More details

  • Words: 1,612
  • Pages: 8
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

1

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.

2

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. 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

3

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 Address

DLAB

Read/Write

Abr.

Registered Mar--

+0

0 0 1

W R R/W

-

+1

0 1

R/W R/W

IER

+2

-

R W

IIR FCR

+3 +4

-

R/W R/W

LCR MCR

+5 +6

-

R R

LSR MSR

+7

-

R/W

-

Transmit holding buffer Receive Buffer Divisor latch low byte Interrupt enable register Divisor latch high byte Interrupt Identification Register FIFO Control register. Line Control Register Modem Control Register Line Status Register Modem Status Register 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 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);

4



After setting DLAB bit in LCR, the function of registers at 0x3F8 & 0x3F9 changes as follows

Base Address  Ox3F8  Ox3F9    

Normal Function Tx/Rx register Interrupt enable register

Function When DLAB=1 Divisor latch Low byte 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 # 7

6 3:5

2

0:1

Functional Description 1 Divisor Latch Access Bit ( DLAB ON ) 0 DLAB=0, Access to Tx/Rx & IER for normal operation Set Brake Enable 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 ) 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 Bit 1 0 0 1 1

Bit 0 0 1 0 1

Word Length 5 Bits 6 Bits 7 Bits 8 Bits

5

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

Pin Symbol

Function

CD

2 3 4

RD TD DTR

5

SG

6

DSR

7

RTS

Carrier Detect: It is used by Modem to inform PC that it has detected Carrier on Phone Line. Serial data is received on this line by PC. Serial Data is transmitted on this pin by PC. Data Terminal Ready When terminal (computer) powers up it asserts DTR high. It is signal ground with reference to which voltages are interpreted as high or low. Data Set Ready. When modem powers up it asserts DSR high. 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.

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

6

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

7

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 */ }

8

Related Documents


More Documents from ""