Serial Port Communication Analysis Engineering Essay

Published:

1.1 Introduction to serial port All compatible computers are originally or typically equipped with two serial ports and one parallel port. These two types of ports are used to communicate with external devices such as another computer or printer and they also worked differently in different ways. The serial port sends and receives data 1 bit at a time over one wire and it takes 8 times longer to transfer each byte of data. The serial port is harder to interface than the parallel port and any devices connected to the serial port will need the serial transmission converted back parallel port so that it can be used. This can be done by using a UART (Universal Asynchronous Receiver/ Transmitter)[1]. In computing

A parallel port sends and receives data 8 bits at a time through 8 separate wires and this will allow the data to be transferred more quickly. Parallel port is normally used to connect a pc to printer and this requires a more bulky cable because of the number of individual wires it contains.

1.2What is a serial port?

Lady using a tablet
Lady using a tablet

Professional

Essay Writers

Lady Using Tablet

Get your grade
or your money back

using our Essay Writing Service!

Essay Writing Service

In computer, a serial port is a serial communication physical interface through which information transfers in and out one bit at a time. Throughout most of the history of personal computers, data transfer through serial ports connected to devices such as terminals and various peripherals. These are two types of serial port male and female ports.

Ref (2) Ref (3)

1.3 Serial ports RS232.

RS232 is a popular communications protocol for connecting modems and data acquisition devices to computers. RS232 can be plugged straight into the computer serial port also known as COM port. Serial ports come in 2 sizes and these are the D-Type 25 pin connector and the D-Type 9 pin connector both of which are male on the back of the pc, therefore you will require a female connector on your device.

1.4 Serial Pin outs (D25 and D9 Connectors)

D-Type-25 Pin No.

D-Type-9 Pin No.

Abbreviation

Full Name

Pin 2

Pin 3

TD

Transmit Data

Pin 3

Pin 2

RD

Receive Data

Pin 4

Pin 7

RTS

Request To Send

Pin 5

Pin 8

CTS

Clear To Send

Pin 6

Pin 6

DSR

Data Set Ready

Pin 7

Pin 5

SG

Signal Ground

Pin 8

Pin 1

CD

Carrier Detect

Pin 20

Pin 4

DTR

Data Terminal Ready

Pin 22

Pin 9

RI

Ring Indicator

Table 1: D Type 9 Pin and D Type 25 Pin Connectors

1.5 Serial port communication

In serial port communication the terminal port of the pc is called the Data Terminal Equipment (DTE) and the modem is called Data Communications Equipment (DCE). RS232 signals have a direction (in or out) depending on whether they are in respect to a DTE or a DCE. The diagram below shows how the cable works from one place to another.

Figure(1)

1.6 Port Addresses & IRQ's

Port Addresses means when two computers are merging to do the same task example communicate to each other when communication is processing. COM 1 should be connected to COM 2 to communicate with each other and this is done using a serial port cable. The cable will be connected from one modem to another modem. If COM 1 is typing COM 2 will be receiving and vice versa. The address for COM 1 and COM 2 is shown in table below and IRQ will be the interrupt request.

Name

Address

IRQ

COM 1

3F8

4

COM 2

2F8

3

COM 3

3E8

4

COM 4

2E8

3

Table 2: Standard Port Addresses

1.7Line Control Register (LCR)

The line control register stores configuration of information such as the number of data, stop and parity bits used in each transmission. It sets the basic parameters for communication and a word length of 8 bits is commonly used and bit 0 and 1 set the word length. Before either of the COM ports can be used they must be properly programmed and this is achieved by writing into the registers for line control. The byte written to the line control register decides the number of bits per character, number of stop bits and whether even or odd parity will be used. The divider latch decides the baud rate of operation of the serial port and you can see divisor latch access bit is active it will show 1 or if it is inactive it will show as 0.

Lady using a tablet
Lady using a tablet

Comprehensive

Writing Services

Lady Using Tablet

Plagiarism-free
Always on Time

Marked to Standard

Order Now

Bit 7

1

Divisor Latch Access Bit

0

Access to Receiver buffer, Transmitter buffer &Interrupt Enable Register

Bit 6

Set Break Enable

Bits 3, 4 And 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)

Bit 2

Length of Stop Bit

0

One Stop Bit

1

2 Stop bits for words of length 6, 7 or 8 bits or 1.5 Stop Bits for Word lengths of 5 bits.

Bits 0 And 1

Bit 1

Bit 0

Word Length

0

0

5 Bits

0

1

6 Bits

1

0

7 Bits

1

1

8 Bits

Table 3: Line Control Register

1.8 What is DLAB?

It stands for Division Latch Access Bit. When DLAB is set to "1" via line control register, two registers become available from which you can set your speed of communications measured in bits per second and it decides the speed of data.

When DLAB is 1 there is no transmission and when it is 0 there is transmission.

1.9 Interrupt Enable Register (IER)

The smartest way to perform serial port communications on a pc is using interrupt driven routines. In that configuration, it is not necessary to poll the registers of the UART periodically for state changes. The Interrupt Enable Register could possibly be one of the easiest register on a UART to understand. Setting Bit 0 enables the Received Data Available Interrupt which generates an interrupt when receiving register / FIFO contains data to be read by the CPU. Register is a memory location inside the hardware to store data. The table below shows what happened to the Bits when there's interrupt.

Bit

Notes

Bit 7

Reserved

Bit 6

Reserved

Bit 5

Enables Low Power Mode (16750)

Bit 4

Enables Sleep Mode (16750)

Bit 3

Enable Modem Status Interrupt

Bit 2

Enable Receiver Line Status Interrupt

Bit 1

Enable Transmitter Holding Register Empty Interrupt

Bit 0

Enable Received Data Available Interrupt

Table 4: Interrupt Enable Register

2.0 What is an UART?

UART stands for universal asynchronous receiver\transmitter. It is responsible for performing the main task in serial communications with computer by a serial card. The device changes incoming parallel information to serial data which can be sent on a communication. To use the UART in different environments, register are accessible to set or review the communication parameters. An UART is capable of generating a processor interrupt when a state change on the communication device occurs.

2.1 Types of UART used for PC.

8250

First UART in this series. It contains no scratch register. The 8250A was an improved version of the 8250 which operates faster on the bus side.

8250A

This UART is faster than the 8250 on the bus side. Looks exactly the same to software than 16450.

8250B

Very similar to that of the 8250 UART.

16450

Used in AT's (Improved bus speed over 8250's). Operates comfortably at 38.4KBPS. Still quite common today.

16550

This was the first generation of buffered UART. It has a 16 byte buffer; however it doesn't work and is replaced with the 16550A.

16550A

Is the most common UART use for high speed communications e.g. 14.4K &28.8K Modems. They made sure the FIFO buffers worked on this UART.

16650

Very recent breed of UART. Contains a 32 byte FIFO, Programmable X-On / X-Off characters and supports power management.

16750

Produced by Texas Instruments. Contains a 64 byte FIFO.

Table: 5

2.2 Registers

Eight I/O bytes are used for each UART to access its registers. The following table shows, where each register can be found. The base address used in the table is the lowest I/O port number assigned. The switch bit DLAB can be found in the line control register LCR as bit 7 at I/O address base + 3.

UART register to port conversion table

 

DLAB = 0

DLAB = 1

I/O port

Read

Write

Read

Lady using a tablet
Lady using a tablet

This Essay is

a Student's Work

Lady Using Tablet

This essay has been submitted by a student. This is not an example of the work written by our professional essay writers.

Examples of our work

Write

Base

RBR

receiver

buffer

THR

transmitter

holding

DLL divisor latch LSB

base + 1

IER

interrupt

enable

IER

interrupt

enable

DLM divisor latch MSB

base + 2

IIR

interrupt

identification

FCR

FIFO

control

IIR

interrupt

identification

FCR

FIFO

control

base + 3

LCR line control

base + 4

MCR modem control

base + 5

LSR

line

status

-

factory

test

LSR

line

status

-

factory

test

base + 6

MSR

modem

status

-

not

used

MSR

modem

status

-

not

used

base + 7

SCR scratch

Table 1:

2.3 First In / First Out Control Register (FCR)

The FIFO register is a write register only. FIFO is an acronym for First In, First Out, and an abstraction in ways of organizing and manipulating data to time of prioritization. This register is used to control the FIFO buffers which are found on 16550's and higher. A FIFO register must often play the role of mediator, helping devices communicate even though their data rates do not match. Early FIFO registers, those with register arrays, suffered from delays because of so-called fall-through and bubble-through times. Fall-through occurs when incoming data must travel through empty locations to get to either the last empty register or the output.

Bit

Notes

Bits 6 and 7

Bit 7

Bit 6

Interrupt Trigger Level

0

0

1 Byte

0

1

4 Bytes

1

0

8 Bytes

1

1

14 Bytes

Bit 5

Enable 64 Byte FIFO (16750 only)

Bit 4

Reserved

Bit 3

DMA Mode Select. Change status of RXRDY &TXRDY pins from mode 1 to mode 2.

Bit 2

Clear Transmit FIFO

Bit 1

Clear Receive FIFO

Bit 0

Enable FIFO's

Table 9: FIFO Control Register

2.4 Bit fields

A bit fields is a common idiom in computer programming to compactly store a value as a short series of bits. A computer is designed to have 1 byte per address and the highest value of parity is 7 (4+2+1). Bit fields have few advantages in portable code. In non-

portable code they are sometimes used to build a struct whose

layout corresponds to an externally-defined format of some

kind (e.g., the sign, exponent, and significant of a `float'). Here is an example how to activate and deactivate the bits of a line control register.

2.4

(i)Line Control Register code:

#include <conio.h>

#include<stdio.h>

#include "def.h"

int main( )

{

LCR *line_ctrl;

clrscr( );

line_ctrl=(LCR *)malloc(sizeof (LCR));

line_ctrl-> DL=ON;

line_ctrl -> SE=OFF;

line_ctrl -> PR=NO_PARITIY;

line_ctrl -> SB=OFF;

line_ctrl -> WL=WL8;

outportb(port, *((char *)( line_ctrl));

outportb (0x378,2)

printf("size of Z is %d\n",sizeof(line_ctrl));

getch( );

}

2.4

(ii)Fifo Control register code:

#include <conio.h>

#include<stdio.h>

#include<stdio.h>

#include<malloc.h>

#include "def.h"

int main()

{

FIFO *cntrl_reg;

clrscr( );

cntrl_reg=(FIFO *)malloc(sizeof (FIFO));

cntrl_reg-> CTF_FIFO=ON;

cntrl_reg -> RS=OFF;

cntrl_reg -> TL=OFF;

cntrl_reg -> CR=OFF;

cntrl_reg -> DMA=OFF;

cntrl_reg -> EF=OFF;

cntrl_reg -> CR_FIFO=ON;

outportb(port, *((char *)( cntrl_reg));

printf("size of %d is \n",sizeof(cntrl_reg));

free(cntrl_reg);

getch( );

}

2.5 Main Code

#include <dos.h>

#include <stdio.h>

#include <conio.h>

#include"defines.h"

#define PORT 0x3F8

#define INTVECT 0x0C

#define MASK 0x06

void main(void)

{

char dr;

int i;

clrscr();

init();

do

{

if (kbhit())

{

c=getche();

if(c==13)

{

for(i=0;i<outct;i++)

{

dr=inportb(PORT+5)&32;

if(dr!=0)

{

outportb(PORT,out_buff[i]);

delay(30);

}

}

outportb(PORT,13);

delay(30);

outct=0;

}

else

{

out_buff[outct]=c;

outct++;

}

}

delay(30);

if in_buff[inct]==13)

{

in_buff[inct]='\0';

inct=0;

printf("\n%s",in_buff);

}

} while (c !=27);

outportb(PORT+1 , 0); /* Turn off interrupts - Port1 */

outportb(0x21,(inportb(0x21) | 0x10)); /* MASK IRQ using PIC */

/* COM1 (IRQ4) - 0x10 */

/* COM2 (IRQ3) - 0x08 */

/* COM3 (IRQ4) - 0x10 */

/* COM4 (IRQ3) - 0x08 */

setvect(INTR_FUNC, isr_old); /* Restore old interrupt vector */

fflush(stdin);

fflush(stdout);

getch();

clrscr();

}

/*Function Name: Init

Inputs: None

Outputs: None

Prototype: void init (void)

Description: This function initializes the buffers and Serial port*/

void init(void)

{

fflush(stdin);

fflush(stdout);

buf_empty(in_buf);

buf_empty(out_buf);

outportb(IER , OFF); /* Interrupts turned off */

delay(30);

isr_old=getvect(INTR_FUNC); /* Save old Interrupt Vector of later recovery */

setvect(INTR_FUNC,isr_new); /* Set Interrupt Vector Entry */

set_com();

outportb(0x21,(inportb(0x21) & 0xEF)); /* Set Programmable Interrupt Controller */

delay(30);

outportb(IER , 0x03); /* Interrupt when data received/transmitted/modem status changes/line status changes */

delay(30);

}

void set_com(void)

{

outportb(LCR , DLAB_ON); /* SET DLAB ON */

delay(30);

outportb(COM , BAUD_9600); /* Set Baud rate - Divisor Latch Low Byte */

delay(30);

outportb(IER , OFF); /* Set Baud rate - Divisor Latch High Byte */

delay(30);

outportb(LCR , 0x03); /* 8 Bits, No Parity, 1 Stop Bit */

delay(30);

outportb(FIFO , MASK);

delay(30);

outportb(MCR , 0x0B); /* Turn on DTR, RTS, and OUT2 */

delay(30);

}

void interrupt isr_new() /* Interrupt Service Routine (ISR) for PORT1 */

{

char intr;

intr=inportb(PORT+2);

if((intr & 6)==4) /*00000100 - Recieved data interrupt*/

{

printf("\nReception Interrupt");

c=inportb(PORT+5);

if(c&1)

{

In_buff[inct]=inportb(PORT);

inct++;

}

}

else if((intr & 6) == 2) /*00000010 - Recieved data interrupt*/

{

printf("\n%d Characters transmitted",++transchar);

}

}

else

printf("\nOther Interrupt");

outportb(0x20,0x20);

}

void buf_empty (char buf[])

{

int i;

for(i=0;i<BUF_SIZE;i++) buf[i]=0;

}

void main(void)

{

char dr;

int i;

clrscr();

fflush(stdin);

fflush(stdout);

init(in_buff);

init(out_buff);

outportb(PORT+1 , 0); /* Turn off interrupts - Port1 */

delay(30);

isr_old=getvect(INTVECT); /* Save old Interrupt Vector of later recovery */

setvect(INTVECT,isr_new); /* Set Interrupt Vector Entry */

/* PORT 1 - Communication Settings */

outportb(PORT + 3 , 0x80); /* SET DLAB ON */

delay(30);

outportb(PORT + 0 , INVECT); /* Set Baud rate - Divisor Latch Low Byte */

delay(30);

outportb(PORT+1 , 0x00); /* Set Baud rate - Divisor Latch High Byte */

delay(30);

outportb(PORT+3 , 0x03); /* 8 Bits, No Parity, 1 Stop Bit */

delay(30);

outportb(PORT+2 , MASK); /* FIFO Control Register */

delay(30);

outportb(PORT+4 , 0x0B); /* Turn on DTR, RTS, and OUT2 */

delay(30);

outportb(0x21,(inportb(0x21) & 0xEF)); /* Set Programmable Interrupt Controller */

delay(30);

outportb(PORT+1 , 0x03);

delay(30);

do

{

if (kbhit())

{

c=getche();

if(c==13)

{

for(i=0;i<outct;i++)

{

dr=inportb(PORT+5)&32;

if(dr!=0)

{

outportb(PORT,out_buff[i]);

delay(30);

}

}

outportb(PORT,13);

delay(30);

outct=0;

printf("\n->");

}

else

{

out_buff[outct]=c;

outct++;

}

}

delay(30);

} while (c !=27);

outportb(PORT+1 , 0); /* Turn off interrupts - Port1 */

outportb(0x21,(inportb(0x21) | 0x10));

setvect(INTVECT, oldisr); /* Restore old interrupt vector */

fflush(stdin);

fflush(stdout);

getch();

clrscr();

}

2.6 Learning Objectives:

Learning Outcomes

Section

Remarks

1

Demonstrate and differentiate the learning of embedded C and standard C programming as an advanced programs.

Embedded C use only limited resources available in chip e.g. limited space in ROM, RAM and ports availability. Embedded C can be a subset of C. The differences between standard C and embedded C are standard C is used for desktop computers for memory allocation and operating system whereas Embedded C is use in microcontroller based for application.

2

Demonstrate how to use bit fields.

Bit fields is used in the code to make it easy and clear understand of the program.

A bit field is an element of a struct or union, having an

integer type but a specified "width,"or number of bits. For example, a three-bit signed integer or a five-bit

unsigned integer. There are restrictions on what

"underlying types"can be used, and on what widths are possible.

It has few advantages in portable code. In non-

portable code they are sometimes used to build a struct whose

layout corresponds to an externally-defined format of some

kind (e.g., the sign, exponent, and significant of a `float').

3

Demonstrate how to use Pointer.

Pointer is used in the codingto allocate a block of memory for an Array. The size of the array is declare as[30].

In C language, a pointer is a variable that points to or references a memory location in which data is stored. Each memory cell in the computer has an address which can be used to access its location.  A pointer variable points to a memory location. By making use of pointer, we can access and change the contents of the memory location.

4

I have to use interrupt optimization codes, I/O operations.

5

To demonstrate the Structures &unions.

Structure and unions is declared in code.

Structure is a combination element, which can be predefined data types or other structure. The length or size of the structure is the sum of the length of its elements.

In C, structures cannot contain functions but in C++ it is possible, whereas a Union is a combination element, which can be predefined data types or other union. But, the size/length of union is the maximum of internal elements.

6

To demonstrate pointer arithmetic, alignment issues.