Introduction(概述)
Parallel port is a simple and inexpensive tool for building computer controlled devices and projects. The simplicity and ease of programming makes parallel port popular in electronics hobbyist world. The parallel port is often used in Computer controlled robots, Atmel/PIC programmers, home automation, …etc… Here a simple tutorial on parallel port interfacing and programming with some examples.
并行口是一种简单而廉价的工具,用于建立计算机控制的设备和项目。程序设计的简单易用性使得并行口在电子爱好者的世界里很受欢迎。并行口经常用于计算机控制的机器人、Atmel/PIC程序员、家庭自动化等……这里是一个简单的关于并行口接口和编程的教程,并举例说明。
Everybody knows what is parallel port, where it can be found, and for what it is being used. the primary use of parallel port is to connect printers to computer and is specifically designed for this purpose. Thus it is often called as printer Port or Centronics port (this name came from a popular printer manufacturing company ‘Centronics’ who devised some standards for parallel port). You can see the parallel port connector in the rear panel of your PC. It is a 25 pin female (DB25) connector (to which printer is connected). On almost all the PCs only one parallel port is present, but you can add more by buying and inserting ISA/PCI parallel port cards.
每个人都知道什么是并行口,在哪里可以找到它,以及它正在使用什么。并行端口的主要用途是将打印机连接到计算机上,并为此目的专门设计。因此,它通常被称为打印机端口或Centronics端口(这个名称来自一家流行的打印机制造公司Centronics,它为并行端口设计了一些标准)。你可以看到平行端口连接器在你的电脑的后面板。它是一个25针女性(DB 25)连接器(打印机连接到)。在几乎所有的PC上,只有一个并行口存在,但是您可以通过购买和插入ISA/PCI并行口卡来添加更多的并行口。
Parallel port modes(并口模式)
The IEEE 1284 Standard which has been published in 1994 defines five modes of data transfer for parallel port. They are,
1) Compatibility Mode
2) Nibble Mode
3) Byte Mode
4) EPP
5) ECP
IEEE 1284 标准1994版定义了并行端口数据传输的五种模式。他们是,
1)兼容模式
2)交互模式
3)字节模式 可能是SPP模式,即Standard Parallel Port,标准并行端口 (Standard Parallel Port),半双工单向传输的,传输速率仅为150KB/s,速度较慢,一般设为默认的工作模式,现在几乎所有的并口外设都支持该模式。
4)EPP,即Enhanced Parallel Port,增强型高速并口。增强型工作模式,EPP采用双向半双工数据传输,其传输速度比SPP高,可达 2MB/s,EPP可细分为EPP 1.7和EPP 1.9两种模式,目前多数外设使用此工作模式。目前市面上的大多数打印机、 扫描仪都能与PC进行双向通讯,都支持EPP模式。
5)ECP,即 Extended Capability Port,扩充功能并口。采用双向全双工数据传输,传输速率比EPP要高。可以算是目前最先进的并口模式,但是该模式需要设置DMA通道,既消耗资源,又容易引起冲突。这可以算是目前最先进的并口模式,但是该模式需要设置DMA通道,既消耗资源,又容易引起冲突。同时,目前支持ECP的外设很少,因此,一般而言不要选择该模式。
The programs, circuits and other information found in this tutorial are compatible to almost all types of parallel ports and can be used without any problems (Not tested, just because of confidence ! ). More information on parallel port operating modes can be found here.
本文中的程序、电路和其他信息几乎兼容所有类型的并行口,并且可以在没有任何问题的情况下使用(没有测试,仅仅是因为信任!)。有关并行口操作模式的更多信息,请参阅此处。
Hardware(硬件)
The pin outs of DB25 connector is shown in the picture below
DB25接口定义如下:
The lines in DB25 connector are divided in to three groups, they are
1) Data lines (data bus)
2) Control lines
3) Status lines
DB25接线分为三组:
1)数据线
2)控制线
3)状态线
As the name refers , data is transferred over data lines , Control lines are used to control the peripheral and of course , the peripheral returns status signals back computer through Status lines. These lines are connected to Data, Control And Status registers internally . The details of parallel port signal lines are given below
顾名思义,数据通过数据线传输,控制线用于控制外围设备,当然,外围设备通过状态线返回状态信号。这些线路内部连接到数据、控制和状态寄存器。并行口信号线的详细情况如下
Pin No (DB25) | Signal name | Direction | Register - bit | Inverted |
---|---|---|---|---|
1 | nStrobe | Out | Control-0 | Yes |
2 | Data0 | In/Out | Data-0 | No |
3 | Data1 | In/Out | Data-1 | No |
4 | Data2 | In/Out | Data-2 | No |
5 | Data3 | In/Out | Data-3 | No |
6 | Data4 | In/Out | Data-4 | No |
7 | Data5 | In/Out | Data-5 | No |
8 | Data6 | In/Out | Data-6 | No |
9 | Data7 | In/Out | Data-7 | No |
10 | nAck | In | Status-6 | No |
11 | Busy | In | Status-7 | Yes |
12 | Paper-Out | In | Status-5 | No |
13 | Select | In | Status-4 | No |
14 | Linefeed | Out | Control-1 | Yes |
15 | nError | In | Status-3 | No |
16 | nInitialize | Out | Control-2 | No |
17 | nSelect-Printer | Out | Control-3 | Yes |
18-25 | Ground | - | - | - |
Parallel port registers(并口寄存器)
As you know, the Data, Control and status lines are connected to there corresponding registers inside the computer. So by manipulating these registers in program , one can easily read or write to parallel port with programming languages like ‘C’ and BASIC.
如您所知,数据、控制和状态线连接到计算机内部的相应寄存器。因此,通过在程序中操作这些寄存器,人们可以轻松地用诸如C和Basic之类的编程语言读写并行口。
The registers found in standard parallel port are ,
1) Data register
2) Status register
3) Control register
标准并口寄存器定义:
1) 数据寄存器
2) 状态寄存器
3) 控制寄存器
As there names specifies, Data register is connected to Data lines, Control register is connected to controll lines and Status lregistyer is connected to Status lines. (Here the word connection does not mean that there is some physical connection between data/control/status lines. The registers are virtually connected to the corresponding lines.). So what ever you write to these registers , will appear in corresponding lines as voltages, Of course, you can measure it with a multimeter. And What ever you give to Parallel port as voltages can be read from these registers(with some restrictions). For example , if we write ‘1’ to Data register , the line Data0 will be driven to +5v. Just like this ,we can programmatically turn on and off any of the data lines and Control lines.
如名称所指定的,数据寄存器连接到数据线,控制寄存器连接到控制线,状态寄存器连接到状态线。(此处,连接一词并不意味着数据/控制/状态线之间存在某种物理连接。寄存器实际上连接到相应的线路。)。因此,无论你写什么到这些寄存器上,都会以电压的形式出现在相应的线路上,当然,你可以用万用表来测量它。当然电压可以从这些寄存器中读取时,你给并行口的是什么(有一些限制)。例如,如果我们将“1”写到数据寄存器,那么数据0行将被驱动到+5v。就像这样,我们可以编程地打开和关闭任何数据线和控制线。
Where these registers are ?(寄存器在哪儿?)
In an IBM PC, these registers are IO mapped and will have unique address. We have to find these addresses to to work with parallel port. For a typical PC , the base address of LPT1 is 0x378 and of LPT2 is 0x278. The data register resides at this base address , status register at baseaddress + 1 and the control register is at baseaddress + 2. So once we have the base address , we can calculate the address of each registers in this manner. The table below shows the register addresses of LPT1 and LPT2
在IBMPC中,这些寄存器被映射为IO,并且具有唯一的地址。我们必须找到这些地址才能让并行口工作。对于典型的PC,LPT 1的基址是0x378,LPT 2的基址是0x278。数据寄存器驻留在该基址,状态寄存器位于基址+1,控制寄存器位于基址+2。因此,一旦我们有了基地址,我们就可以以这种方式计算每个寄存器的地址。下表显示LPT 1和LPT 2的寄存器地址。
Register | LPT1 | LPT2 |
---|---|---|
data registar(baseaddress + 0) | 0x378 | 0x278 |
status register (baseaddress + 1) | 0x379 | 0x279 |
control register (baseaddress + 2)0x37a0x27a | 0x37a | 0x27a |