0% found this document useful (0 votes)
32 views7 pages

Lab3 v2

This document outlines a laboratory exercise on using the parallel port for input/output operations in a Computer Engineering course at Singapore Polytechnic. It details the objectives, requirements, and procedures for writing C programs to interface with the parallel port, including configuring registers and performing data transfer. The lab includes practical programming tasks to control LEDs and read dip-switch settings using the parallel port.

Uploaded by

racomes304
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views7 pages

Lab3 v2

This document outlines a laboratory exercise on using the parallel port for input/output operations in a Computer Engineering course at Singapore Polytechnic. It details the objectives, requirements, and procedures for writing C programs to interface with the parallel port, including configuring registers and performing data transfer. The lab includes practical programming tasks to control LEDs and read dip-switch settings using the parallel port.

Uploaded by

racomes304
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Diploma in Computer Engineering

School of Electrical & Electronic Engineering


Singapore Polytechnic

Computer Interfacing
Laboratory 3: Parallel Port Input/Output

1. Introduction
Before IBM introduced the PC, many printer manufacturers adopted the “Centronics”
connector(36-pin) format for connecting their printers to a computer, be it a mainframe,
a mini- or a microcomputer. In other words, the “Centronics” interface was the industry
standard. Today, the PC however, uses a 25-pin D-connector. This means that some of
the original (non-essential) signals are sacrificed. Some of the contacts have also been
assigned new functions. This legacy port on the PC is normally known as the Line
Printer (LPT) Port or the parallel port.

Objective:
This laboratory exercise will introduce you to writing C programs for transferring
information between the PC and external devices using the parallel port. In this lab, you
will learn:

• To identify the parallel port register addresses


• To write C programs to demonstrate the input/output operations of the parallel
port.

Schedule:
• This laboratory is expected to take up to 2 hours.
• You will also be required to do some research and further reading on parallel
data transfer.

Requirements:
• Personal Computer with Microsoft Windows 10 operating system.
• Parallel Port I/O kit.
• Microsoft Visual Studio 2013.

Lab 3: Parallel Port Input/Output Page 1 of 7


Diploma in Computer Engineering
School of Electrical & Electronic Engineering
Singapore Polytechnic

2. LPT Port Connector


The Line Printer port connector for the PC is shown below:

2.1 Using Parallel Port for I/O


To use the parallel port for I/O interfacing, we need to know what are the registers
involved with the parallel port. There are three registers altogether for us to
manipulate, and these are:

Data Register (Base address)


Status Register (Base address + 1)
Control Register (Base address + 2)

Figure below shows the link between the three parallel port registers and the D25
connector.

Lab 3: Parallel Port Input/Output Page 2 of 7


Diploma in Computer Engineering
School of Electrical & Electronic Engineering
Singapore Polytechnic

Data Register
The Data Register holds the 8-bit data written to the data lines of the parallel port,
DB0~7. In the case, when the parallel port is configured as an input port, the Data
Register holds the 8-bit data read at the D25 connector’s data pins (DB0~7).

Status Register
In this lab, the Status Register is used as an input port (bits S3~S7 only).

Control Register
In this lab, the Control Register is used as an output port (bits C0~C3 only). Note that
data bits loaded into bits C0, C1 and C3 of the Control Register would be inverted at the
corresponding pins of the D25 connector.

In the case where the data port of the parallel port can be configured as bidirectional
port, then Bit C5 controls the direction of data flow; ‘0’ for output and ‘1’ for input.

2.2 Addressing the Parallel Port


The PC has some parallel port support firmware built into its BIOS (Basic Input/Output
Services), a set of program routines that perform many common tasks.

When a PC powered up, a BIOS routine automatically tests for parallel port(s) at each
of the following three addresses in order:

(To determine the existence of a port, the BIOS simply sends a data byte to the port and
then reads back from the port what it sent. If the read back tallied, the port exists.)

Lab 3: Parallel Port Input/Output Page 3 of 7


Diploma in Computer Engineering
School of Electrical & Electronic Engineering
Singapore Polytechnic

2.3 Communicating with the Parallel Port

To communicate with parallel port, all you need to do is to write a byte into or to read a
byte from the parallel port register. The C functions that we use to write and read to a
hardware I/O address is Out32( ) and Inp32( ) respectively. [Out32() and Inp32() both
belong to the device driver inpout32.dll device driver.]

To include the inpout32.lib library for inpout32.dll, follow the steps:

A parallel port register will have only two properties, i.e. its address and data (8bit). To
write a byte into a parallel port register, you need to provide these two properties, i.e.
thedata that you want to write into the register and the register’s address. Hence, we
would pass 2 arguments to the function Out32( ).

Syntax: Out32( <address>, <8-bit data> );


e.g. Out32(0x3028, 0xFF);

To read a byte from a parallel port register, you need to provide only its address, i.e. the
address of the register that you want to read. Hence, we would pass only 1 argument to
the function Inp32( ). The return value of this function is the data we read from the
register.

Syntax: Inp32( <address> );


e.g. Inp32(0x3029);

Lab 3: Parallel Port Input/Output Page 4 of 7


Diploma in Computer Engineering
School of Electrical & Electronic Engineering
Singapore Polytechnic

3. Input/Output of data using Parallel Port

3.1 Configuring the Data Port as Output


Determine the value (in decimal or hexadecimal) that you need to write into the Control
Register (CR) in order to enable the Data Port to act as an output port.

Content of Control Register: ____________________


Note: The Data Port will be set as an output port if C5 is 0.

3.2 Output Operations


In order to output data to the data port we have to perform the following sequence:

1. Configure the Parallel Port by writing to CR using Out32 function.


Out32(0x302A, _________);

2. Output data to Data Register (DR) using the Out32 function.

e.g. the following statement sends the integer value of 8 to DR.

Out32(0x3028, 8);

Should we decide to send another value to DR, we can use the same statement but
change only the data value, for example:

Out32(0x3028, 0xF0); will light up the top half of the LEDs on the board.

Let us try writing a program in C to display data on the LEDs.

Type in the following program in Visual Studio and run it.


(Refer to the steps in Lab1 on how to set up projects on Microsoft Visual Studio 2013)

Listing 3.1 LAB3a.cpp


#include <windows.h> //for Sleep( )
#include <conio.h> //for kbhit( )

#define DR 0x3028 //Address of Data Register.


#define CR 0x302A //Address of Control Register.

/* Function prototypes for Inp32( ) and Out32( ) */


short _stdcall Inp32(short PortAddress);
void _stdcall Out32(short PortAddress, short data);

void main()
{
int light;

Lab 3: Parallel Port Input/Output Page 5 of 7


Diploma in Computer Engineering
School of Electrical & Electronic Engineering
Singapore Polytechnic

Out32(CR, 0x00); //Configure Data Port as output, i.e. C5=0


light = 0x55; //1 = LED on, 0 = LED off

while (!kbhit()) //while no keyboard pressed


{
Out32(DR, light); //Output to LEDs
Sleep(500); //for 500 ms
light = ~light; //Invert data bits
}
}

Run the program and enter different values for the variable ‘light’ and observe the
results on the LEDs.

1. Briefly describe what the program does:

___________________________________________________________

2. What happens when the statement “Sleep(500);” is deleted ?

___________________________________________________________

3. What happens if both “Sleep(500);” and “light = ~light;” are deleted ?

___________________________________________________________

4. By keeping the setting of Question 3, try to program the DR to 0x13, 0xD1,


0xFF. Do the LEDs display the data that you have entered? ___________

5. Display moving LEDs going from left to right. When the right-most LED
is reached, the sequence starts all over again. (HINT: use >> for shifting the
bits). Name your program as LAB3b.cpp.

6. Write a program that does the above, however this time the lights run from
right to left. Name your program as lab3c.cpp.

3.3 Input Operations


Type in the following program:

Listing 3.2: LAB3d.cpp


#include <stdio.h>
#include <conio.h>

#define SR 0x3029 // Address of Data Register

Lab 3: Parallel Port Input/Output Page 6 of 7


Diploma in Computer Engineering
School of Electrical & Electronic Engineering
Singapore Polytechnic

/* The two function prototypes below are needed for Inp32() and Out32() */
short _stdcall Inp32(short PortAddress);
void _stdcall Out32(short PortAddress, short data);

void main()
{
unsigned char key;

printf("Key is at logic:");
while (!kbhit()) //Repeat while no keyboard pressed
{
key = Inp32(SR); //Read input from SR
key = key & 0x40; //Read input from only S6
if (key == 0x40) // Display status of input
printf("1 (HIGH)\n");
else
printf("0 (LOW)\n");
}
}

1. Write a program to input the dip-switch setting (S3 to S6) and control the LEDs
accordingly, e.g. if switch S3 is `1', the respective LED at D3 is turned on.
Name your program as lab3e.cpp.

2. Modify the program in Listing 3.2 to incorporate the followings. When switch S5
is at logic `0', LEDs run from left to right. When it is at logic `1', LEDs run from
right to left. When a key is pressed, the program stops.

Show the program to your lecturer. Name your program as LAB3f.cpp.

Lab 3: Parallel Port Input/Output Page 7 of 7

You might also like