Feb 13, 2008 ... Computer Systems 2: Hardware Interfacing. 1. Introduction. In this chapter we will
look at how we can connect our computer to hardware, so we ...
COMPUTER SYSTEMS
COMP1208
2008
Computer Systems 2: Hardware Interfacing 1.
Introduction In this chapter we will look at how we can connect our computer to hardware, so we can read in information from the hardware and send out control signals to the hardware. The aim of this chapter is to explore how C++ programs can be used to interface simple data sources to a microcomputer
1.1
What is a data acquisition system? “A system that quantifies and stores data.”
Figure 1: Data Acquisition System
Physical Phenomena Data acquisition systems need to get real-world signals into the computer. These signals come from a diverse range of instruments and sensors, and each type of signal needs special consideration. Sensors and Actuators (transducers) A transducer is a device that converts input energy of one form into output energy of another form. A sensor is a device that receives a signal or stimulus and responds with an electrical signal. An actuator is the device that converts an electrical signal into a physical signal or stimulus of some type. Sensors and actuators can both be transducers. For example, a microphone is a sensor that converts sound energy (in the form of pressure) into electrical energy, while a loudspeaker is an actuator that converts electrical energy into sound energy. Signal conditioning hardware Sensor signals are often incompatible with data acquisition hardware. To overcome this incompatibility, the signal must be conditioned. For example, you may need to condition an input signal by amplifying it or by removing unwanted frequency components. Output signals may need conditioning as well. Data Acquisition Hardware At the heart of any data acquisition system lies the data acquisition hardware. The main function of this hardware is to convert analogue signals to digital signals, and to convert digital signals to analogue signals.
Notes02-Interfacing.doc RevA
Page 1-12
13/02/2008 14:11
COMPUTER SYSTEMS
COMP1208
2008
The computer The computer provides a processor, a system clock, a bus to transfer data, and memory and disk space to store data. Software Data acquisition software allows you to exchange information between the computer and the hardware. For example, typical software allows you to configure the sampling rate of your board, and acquire a predefined amount of data.
1.2
Example of System In this course we will design a simple system to control temperature on a circuit board.
MOTOR OUT PORT
HEATER
Computer TEMP sensor
Analogue to Digital Converter (ADC)
IN PORT
Figure 2: Example – Temperature Control System
A temperature sensor is connected to the computer via a Analogue to digital converter. The computer continuously monitors the temperature. If the temperature is too high, a signal is sent to the motor to turn on a fan until the temperature decreases. If the temperature is too low, a signal is sent by the computer to the heater to turn it on until the temperature rises to the required level.
Notes02-Interfacing.doc RevA
Page 2-12
13/02/2008 14:11
COMPUTER SYSTEMS
COMP1208
1.3
2008
Analogue and Digital Signals Analogue signals are continuous electrical signals that vary in time as shown the figure below. An analogue signal is continuously variable and has has an infinite number of values between some maximum and minimum.
Temperature Degrees C
25
20
9
12
3
6
Hour
Figure 3: Analogue Signal Example
Analogue signals represent some physical quantity, i.e. they are a ‘MODEL’ of the real quantity. Digital signals are non-continuous, they change in individual steps. They consist of pulses or digits with discrete levels or values. The value of each pulse is constant, but there is an abrupt change from one digit to the next. A binary signal can only have one of two levels, logic 1 or 0. For example:5 Volts -> Logic 1 and 0 Volts -> Logic 0 Voltage
5
0
Time
0 1 1 0 0 1 1 0 ..
Figure 4: Digital Signal Example
Here, then, is a summary of the advantages and disadvantages of the two systems: Analogue Advantages Produces a more 'faithful' reproduction of the physical quantity. Usually simple Disadvantages Noise and distortion problems
Notes02-Interfacing.doc RevA
Page 3-12
Digital Advantages Can be very immune to noise Signal can be transmitted over long distances. Disadvantages Output subject to quantity errors from sampling Can be complex
13/02/2008 14:11
COMPUTER SYSTEMS
COMP1208
1.4
2008
Serial vs Digital communications When we wish to connect our computer to external hardware, there are two basic types of communication ports we can use: serial and parallel.
Serial Communications source
1 0 1 0 1 1 0 0
1 0 1 0 1 1 0 0
Destination
Parallel Communications
source
1 0 1 0 1 1 0 0
1 0 1 0 1 1 0 0
Destination
Figure 5: Analogue Signal Example
Serial Communications: When information is sent across a single path, one data bit at a time, this is called serial communications. There may be number of serial communications ports on your PC, for example the 'D' shaped 9-pin connector on the back of the computer. This is a serial connector typically uses 2 loops of wire (1 in each direction) for data communication, plus additional wires to control the flow of information. However, in any given direction, data is still flowing over a single wire. Parallel Communications When information is sent over more wires simultaneously, many bits at a time, this is called parallel communication. For example, the 25-pin parallel port on your PC has eight data-carrying wires so that eight bits can be sent simultaneously. Because there are 8 wires to carry the data, the data finishes being transferred eight times faster than a serial connection.
Notes02-Interfacing.doc RevA
Page 4-12
13/02/2008 14:11
COMPUTER SYSTEMS
COMP1208
2.
Interfacing the Parallel Port
2.1
Introduction
2008
Communications between a computer and an external device require a medium, a common language (a code, such as ASCII code), and rules for the exchange of signals - called a protocol. The protocol includes such things as rate at which the exchange of data can take place and how to control the flow of data when necessary. This section gives an introduction to interfacing the parallel port.
2.2
Ports A port is an eight-bit register. It can be accessed by software, but it is also physically connected to a hardware device, or more usually, it is part of a hardware device. A PC can have 65,536 ports connected to it, numbered from 0 to FFFF in hex. However, only a small number of these addresses are actually used.
2.2.1
Common DOS functions in C/C++: To access a port using the C/C++ language, MicroSoft Visual C++ provides the following functions, which it should be noted do not conform to ANSI C++ standards: _inp() reads a byte from a specified port. _outp() writes a byte to a specified hardware port. •
_inp(portno) reads a byte from the port with the address portno. The variable portno must be an unsigned integer between 0 and FFFF hex, and the function returns an integer value.
•
_outp(portno,value) writes a byte to the port with the address portno. This causes the information in value to be written to the port with the address portno. Again portno must be an unsigned integer as before, and value is also an integer.
Note these functions will only work if running from DOS or Windows 98, if you are working on Windows NT/2K/XP, you will need the following .dll loaded: inpout32.dll and inpout32.lib. How to use inpout32 in VC++ 6.0 application 1 Login as administrator 2 Copy inpout32.dll to your Windows system directory (e.g \Windows\System32), 2 Add the header information below to your VC++ code. 3 Add inpout32.lib file to your project and build. The following header information is required to map the _inp and _outp functions to Inp32 and Out32 respectively: // inpout32.dll function prototypes short _stdcall Inp32(short port); void _stdcall Out32(short port, short data); // For compatibility with Win9x code #define _inp Inp32 #define _outp Out32
Notes02-Interfacing.doc RevA
Page 5-12
13/02/2008 14:11
COMPUTER SYSTEMS
COMP1208
3.
Parallel Port
3.1
Interfacing the Parallel Printer Port:
2008
A PC can be connected to a parallel printer by means of a 25-pin connector as shown in Figure 6 below.
ref
Figure 6: 25-way Female D-Type Connector
There are three ports associated with the computer printer interface as shown. • • • •
DATA Port - access to 8 output pins STATUS Port - access to 5 input pins (one inverted) CONTROL Port - access to 4 input/output pins (three inverted) the remaining 8 pins are grounded
The data port is used to output the printable characters to the printer. The status port is used to input the information from the printer so that the computer will know when and if to send information to it. The control port, is used to output control information to the printer. When a computer is first turned on, the BIOS (Basic Input/Output System) will determine the number of ports that exist and assign device labels LPT1, LPT2 and LPT3 to them. LPT1 is normally assigned base address 378h, while LPT2 is assigned 278h. However this may not always be the case. Address
Ports
Address
Ports
0x378
Data Port for primary parallel port (LPT1)
0x278
Data Port for second parallel port (LPT2)
0x379
Status Port for primary parallel port (LPT1)
0x279
Status Port for second parallel port (LPT2)
0x37A
0x27A Control Port for primary parallel port (LPT1) Control Port for second parallel port (LPT2) Table 1: Port Addresses
Notes02-Interfacing.doc RevA
Page 6-12
13/02/2008 14:11
COMPUTER SYSTEMS
COMP1208
3.1.1
2008
Software Registers for Standard Parallel Port Data Port The base address, usually called the Data Port or Data register is simply used for outputting data on the Parallel Port’s data lines (pin 2-9). This register is normally a write only port. If you read from the port, you should get the last byte sent. However, if your port is bi-directional, it is possible to receive data. The Data Port has the Port Address 0x378, the 0x indicating a hexadecimal number. The Data Port is an output port when used by the printer, but it can be used by other applications as an input port. Data register BIT7
BIT 6
?
?
Pin 9
Pin 8
BIT 5
?
BIT 4
?
Pin 7
Pin 6
Figure 7:
BIT 3
BIT 2
? Pin 5
? Pin 4
BIT 1
? Pin 3
BIT 0
? Pin 2
Data Port Assignment
Control Port The Control Port has the Port Address 0x37A and was intended as a write only port. With a printer attached to the Parallel port, four controls are used, Strobe, Auto LineFeed, Initialise and select printer. However, it is possible to use these lines for inputs also. Bit 4 and 5 are internal controls. Bit 4 will enable the IRQ and bit 5 will enable the bi-directional port, meaning you can input 8 bits using the data registers. unused
unused
Enable BiDir
Enable IRQ
____ Select Printer
Initialise Printer
____ Auto Linefeed
BIT 7
BIT 6
BIT 5
BIT 4
BIT 3
BIT 2
BIT 1
Figure 8:
?
?
?
Pin 17
Pin 16
Pin 14
____ Strobe
BIT 0
? Pin 1
Control Port Assignment
Status Port The third port is the Status Port and it has the Port Address 0x379 and is a read only port. Any data written to this port will be ignored. The Status port is made up of 5 input lines (Pin 10,11,12,13,15), a IRQ status register and two reserved bits. Busy
____ ACK
BIT7
BIT 6
Paper Out
Select In
BIT 5
BIT 4
___ IRQ (Not)
BIT 3
BIT 2
↑
↑
↑
↑
↑
Pin 11
Pin 10
Pin 12
Pin 13
Pin 15
Figure 9:
Notes02-Interfacing.doc RevA
____ Error
Page 7-12
Reserved
BIT 1
Reserved
BIT 0
Status Port Assignment
13/02/2008 14:11
COMPUTER SYSTEMS
COMP1208
3.2
VC++ Examples
3.2.1
Input Data from Port
2008
Consider the following example, which sets up the parallel port to read from the data port. Example: Port_in.cpp //Program to initialise parallel port to read from an Application Card #include #include using namespace std; int main() { int data; _outp(0x37A_PORT,0x20); data = _inp(0x378);
// set bit 5 on Control Reg -> read data // byte read from the Data Port
cout