Currently, serial ports are commonly used data transmission channels between computer and external control systems in various control systems for laboratory and industrial applications. As serial communication is convenient and easy to use, it is widely used. However, the use of serial communication imposes restrictions on real-time performance, speed, and data size. While the computer's parallel port transfers data at a time by eight (one byte) or more, because of the large amount of transmission, the data transmission speed is faster than the serial port, in many control systems that have to pay attention to transmission speed, using PC parallel ports to connect to them is a good solution.
This article introduces the application of PC parallel port in single chip microcomputer and other control systems as digital I/O Ports. In the control system, many digital switches, digital control signals, and digital signals can be collected through parallel ports of computers; the collected digital signal is processed by the computer to form a digital control signal, and then the computer's parallel port output for various automatic control.
1. Introduction to PC parallel ports
Currently, parallel interfaces in computers are mainly used as printer ports. interfaces are not 36-pin connectors but 25-pin D-connectors. The so-called "Parallel" means that 8-bit data is transmitted through parallel lines at the same time, which greatly improves the data transmission speed.
Currently, there are five common parallel ports: SPP, PS/2, EPP, ECP, and multi-mode interfaces. Most PCs are equipped with SPP ports:
SPP standard parallel ports have four, eight, and half eight bits: four bits can only input four bits at a time, but can output eight bits of data; the 8-bit port can input and output 8-bit data at a time.
PS/2 simple bidirectional parallel port: It introduces bidirectional data port, which allows peripherals to send 8-bit information each time to the PC. The PS/2 parallel port refers to all parallel interfaces that have two-way Data ports, but do not support the EPP or ECP mode described later.
Iii. PC parallel port Digital Input/Output
The so-called digital output is to enable or disable a certain switch point of a device in a program to generate a high or low potential. From the computer's point of view, the low potential is less than 0.7V (logic 0), and the high potential is more than 2.1V (logic 1). If the potential is between 0.7 and ~ When V is used, the logical state of the potential is uncertain. The easiest way to control external devices through a computer is to control digital output.
The so-called digital input, that is, the external situation is recorded by the computer with a value of 0 or 1 and stored, the 0 and 1 represent two situations in which a certain switch point of a device is on or off.
The PC parallel port can be used as a digital output port or a digital input port. The data port and control port can both be used as the digital output port. The data port has 8 bits and the control port has 4 bits. The two ports can form 1 ~ Any 12-bit digital output port. The status port and control port can be used as the digital input port. The status port has five digits and the control port has four digits. The two ports can form one to four ~ Any number of 9-bit input ports. This article provides the Read and Write methods for the three registers of the parallel port, as shown in:
Iv. VC Implementation of Digital Input/Output in PC Parallel Port
Because Windows shields the underlying system operations, the system becomes safer for users, but it brings great difficulties to many hardware or system software developers, as long as the application involves underlying operations, developers have to go deep into the Windows kernel to write system-level device drivers. This is the case for read/write operations on parallel ports. Due to Windows's protection of the system, no direct I/O operations are allowed. Therefore, *. DLL ,*. sys or *. vxD file, which is used to let the operating system know that a specific I/O may be called. After the system starts up, the content in these files will be loaded into the memory. Once a corresponding action occurs, the actual I/O action will be triggered.
This article only introduces the use of parallel ports as digital I/O ports, not the compilation of parallel I/O port drivers. Therefore, the winio library written by Yariv Kaplan is used directly in this article. It has the following features: The winio library bypasses the Windows security protection mechanism by using the device driver in kernel mode and some other underlying programming skills, allow 32-bit Windows programs to operate on the I/O port directly;
Windows 9x, Windows NT, Windows 2000, and Windows XP are supported. Windows NT, 2000, and XP allow non-administrator users to apply winio applications.
Note: When using this class of code, make sure that it is not in conflict with other programs that use conventional Win32 to call parallel ports.
Use of WinIo library in VC applications
To use the winio library properly in VC, you must follow these steps:
(1): place the winio. dll, winio. sys, and winio. VxD files in the directory where the executable files are located;
(2): Add WinIo. lib to the project. The WinIo. lib and winio. H files must be placed in the project directory;
(3): add the # include "winio. h" Statement to the StdAfx. h header file;
(4): Call the InitializeWinIo function to initialize the WinIo driver library;
(5): Call the GetPortVal or SetPortVal function of the read/write IO port;
(6): Call the ShutdownWinIo function;
To run the task without administrator permissions, you must first complete the following steps:
(1): place the WinIo. dll, WinIo. sys, and WINIO. VXD files in the directory where any WinIo application executable files are located;
(2): log in as an administrator or other user with administrator privileges;
(3) Call the InstallWinIoDriver function. Set the first parameter to the directory path of the WinIo. sys file, and set the second parameter to false;
(4) restart the system;
(5): Log On As a normal user. Now you can call the WinIo library function;
(6) When the WinIo library is no longer needed, you can log on to the system as an administrator or another user with administrator privileges and call RemoveWinIoDriver to uninstall the library;
Description of several functions in the WinIo Library:
(1) initialization and Termination
bool _stdcall InitializeWinIo();void _stdcall ShutdownWinIo();
(2) Installation and uninstallation
bool _stdcall InstallWinIoDriver(PSTR pszWinIoDriverPath, bool IsDemandLoaded = false);bool _stdcall RemoveWinIoDriver();
(3): read/write I/O Ports
bool _stdcall GetPortVal(WORD wPortAddr, PDWORD pdwPortVal, BYTE bSize);bool _stdcall SetPortVal(WORD wPortAddr, DWORD dwPortVal, BYTE bSize);
The GetPortVal function reads a BYTE/WORD/DWORD type value from the specified port;
WPortAddr specifies a port address value;
PdwPortVal is a pointer to a dual-Byte variable that stores the value read from the wPortAddr port;
BSize indicates the number of bytes to read. The value can be 1, 2, or 4.
The SetPortVal function writes a BYTE/WORD/DWORD type value to the specified port;
Except dwPortVal as the input parameter, it indicates that the variable to be written has the same meaning as GetPortVal.
EPP enhanced parallel port: it allows 8-bit bidirectional data transmission. It can complete one byte data transmission, including handshake within about 1 ms; the SPP or SP/2 interface takes about 4 ms to complete the same job. Therefore, you can connect to a variety of non-printer devices, such as scanners, LAN adapters, disk drives, and CDROM drives.
ECP port expansion parallel port: it is a two-way data port that can transmit data at the speed of the ISA bus. ECP has a buffer and supports command cycle, data cycle, and addressing of multiple logical devices. You can use DMA (Direct Memory Access) in a multi-task environment ).
Multi-Mode Interface: many new interfaces support multiple modes and can work in some or all of the modes mentioned above. You can select configurations and use the above interfaces, or use only some of them and disable others.
II. Introduction to parallel ports in PC standard equipment
This article mainly introduces the application of computer standard port with parallel port, that is, 25-pin connector port. On this basis, parallel ports in other modes can be used in the same principle. The parallel port has a total of 25 feet, but not every foot is used. These scripts are divided into three main functions: data transmission, printer status check, and printer control. Their interfaces are as follows:
In a PC, the standard parallel port uses three eight-bit port registers, that is to say, the data, status, and control register read/write access port signal. This article uses some common naming rules. The eight data bits are D0 ~ D7, five status bits are S3 ~ S7, the four controls are C0 ~ C3. A letter indicates the port register, and a number indicates the bit of the signal in the register.
Data Register
Data port or data register (D0 ~ D7) stores one byte of information written to the data output port. Data can be written or read from the data port (data can be erased). Of course, we want to output data from the data port pins, but what we read is only the data we wrote last time, or the data that was originally stored in it, not the data that was input from the port pin into the PC. The data port pin is PIN2 ~ PIN9 is defined as follows:
The data register (that is, the data output port) can be erased and the base address |
Bit |
Pin: D-sub |
Signal name |
Signal Source |
Whether to reverse the phase at the connector |
0 |
Pin2 |
D0 |
PC |
No |
1 |
Pin3 |
D1 |
PC |
No |
2 |
Pin4 |
D2 |
PC |
No |
3 |
Pin5 |
D3 |
PC |
No |
4 |
Pin6 |
D4 |
PC |
No |
5 |
Pin7 |
D5 |
PC |
No |
6 |
Pin8 |
D6 |
PC |
No |
7 |
Pin9 |
D7 |
PC |
No |
If we treat these eight feet as the ordinary number output position, the above eight feet are equivalent to the eight numbers output position, we can regard them as eight output points that can be freely controlled. When we transfer data through the data port, it is to change the level status of these eight feet; and the recipient can also obtain the transmitted data according to the same encoding principles.
Status Register
The status port or Status Register stores 5 inputs (S3 ~ S7. S0 ~ S2 is not included in the parallel port connector. In addition to S0, status registers are read-only, and read data information is the logical status on the Status port pins. S0 is a timeout sign that supports EPP transmission and port. It can be cleared by software. In many parallel ports, the status input is connected to the upper-tension resistance. The status Port Pin is Pin10 ~ Pin13 and Pin15 are defined as follows:
Base Address of Status Register (that is, STATUS input port) + 1 |
Bit |
Pin: D-sub |
Signal name |
Signal Source |
Whether to reverse the phase at the connector |
0 |
|
Time-Out |
|
|
1 |
|
Unused |
|
|
2 |
|
Unused |
|
|
3 |
Pin15 |
NError (nFault) |
Peripherals |
No |
4 |
Pin13 |
Select |
Peripherals |
No |
5 |
Pin12 |
PaperEnd |
Peripherals |
No |
6 |
Pin10 |
NAck |
Peripherals |
No |
7 |
Pin11 |
Busy |
Peripherals |
Yes |
In the above table, the so-called (base address + 1) refers to: if our LPT address is 378 H, then 1 is 379 H. This address is used to transmit the printer status. What is different from the data address is that the address does not have a corresponding point on the connector's foot position. In this status, only five BITs correspond to each other, and the bits are S0 ~ S2 is not available-at least the computer cannot have the corresponding value readable.
If the printer is connected to the parallel port, the status of the printer will be transmitted to the PC through these feet. The program only needs to read the value at the base address + 1 to know the current status of the printer. Since these feet can be transmitted to the PC by the printer, we can use these feet as a digital input channel; we can change the status of these feet to a potential, and use a program to read the values of these feet to input data.
Control Register
The control port or the control register stores C0 ~ The four-digit control information of C3. C4 ~ C7 cannot appear in the parallel port connector. In general, these bits are used for output, but in most SPP, the control bits are in open/Drain open mode, that is, they can also be used as input. To read the external logic signal from the control bit, first write "1" to the corresponding output, and then read the value of the control register. However, in most EPP and ECP interfaces, the control bit works in a push-pull mode that cannot be used as an input to increase the switching speed. In some multi-mode interfaces, the control bit adopts an improved push-pull mode and can be used as an input. The control port pins are Pin1, Pin14, Pin16, and Pin17, which are defined as follows:
Control Register (control output port) base address + 2 |
Bit |
Pin: D-sub |
Signal name |
Signal Source |
Whether to reverse the phase at the connector |
0 |
Pin1 |
NStrobe |
PC |
Yes |
1 |
Pin14 |
NAutoLF |
PC |
Yes |
2 |
Pin16 |
NInit |
PC |
No |
3 |
Pin17 |
NSelectIn |
PC |
Yes |
4 |
|
IRQ |
|
|
5 |
|
Unused |
|
|
6 |
|
Unused |
|
|
7 |
|
Unused |
|
|
In the above table, the so-called (base address + 2) refers to: if our LPT address is 378 H, add 1 to 37ah; this address is used to control printer operations.
Like sending data, our program can output data by sending our information to the address (base address + 2, the receiver can receive the corresponding logical potential status on the corresponding PIN. When the signal source of the control port is high, these pins can be used as input pins, just like the status port pins.
In the preceding definition table, the so-called "Whether the connector is in the inverted phase" means that the parallel port hardware completes the inverted phase processing of the four signals between the connector and the corresponding register bit. Specifically, the logic status of S7, C0, C1, and C3 signals is reversed to the corresponding register bit at the connector. When you write these bits, you must keep in mind that the write value should be the opposite of the value you want to set at the connector; when you want to read these bits, you must also remember that the read value is opposite to the value at the connector.
The computer standard is equipped with parallel ports in addition to the data port pins described above pin2 ~ Pin9, status port pins pin15, pin10 ~ Pin13, control Port Pin pin1, pin14, pin16, pin17, other pins on the connector pin18 ~ Pin25 is the home pin Gnd.
VC implementation for digital output of PC Parallel Port
In order to test the digital output of the parallel port, 12 LEDs can be prepared, and the anode of the LED and the pin2 pin of the data port can be respectively ~ Pin9 is connected with pin1, pin14, pin16, and pin17 control ports. The led cathode is connected together with the Gnd of the parallel port. In actual control applications, this cannot be connected because the output current of the Data Port Pin and control Port Pin is very small and only about 10mA. Other hardware circuits must be added.
(1): VC implementation of digital output on the data port
// Obtain the data port address Word m_nport = (Word) 0x378; // obtain the value of the data port to be written writevalue (data range: 0 ~ 255) DWORD m_nvalue = (DWORD) writevalue; // call the winio library function setportval to write the port value setportval (m_nport, m_nvalue, 1 ); // write a byte value to an I/O port
(2): VC implementation of control port digital output
// Obtain the control port address Word m_nport = (Word) 0x37a; // obtain the value of the control port. Keep the value of the control port unchanged. The value to be output is output from the lower 4 bits, make the potential status on the connector consistent with the value to be output DWORD temp_dwportval; unsigned int temp_aa; getportval (m_nport, & temp_dwportval, 1 ); // reads a byte value from an I/O porttemp_aa = (unsigned INT) temp_dwportval; temp_aa = temp_aa & 0x0f0; // take the 8-bit lower value and set the 4-bit lower to 0; the four digits in height remain unchanged; temp_aa = temp_aa ^ 0x0b; // set C0, C1, and C3 to 1, C2 to 0, and the four digits in height remain unchanged;
// Obtain the value of the control port to be written (data range: 0 ~ 15) unsigned int wrivalue; wrivalue = writevalue & 0x0f; // retrieve 4 bits; temp_aa = temp_aa ^ wrivalue; // reverse the C0, C1, and C3 values in the lower 4 bits of the write value. The C2 BITs remain unchanged. The high 4 bits keep the port value unchanged. setportval (m_nport, (DWORD) temp_aa, 1); // In the written value, the 4-bit high keeps the original port value unchanged. // What level is the 4-bit low written, and what level is the connector?
(3) data port and control port combined into 12-bit digital output VC implementation
// Obtain the port address Word m_nportdata = (Word) 0x378; Word m_nportcontrol = (Word) 0x37a; // obtain the value writevalue (data range: 0 ~ 4095) DWORD m_nvalue = (DWORD) (writevalue & 0x0ff); // get the low 8-bit value setportval (m_nportdata, m_nvalue, 1 ); // write a byte value to data portdword temp_dwportval; unsigned int temp_aa; getportval (m_nportcontrol, & temp_dwportval, 1 ); // reads a byte value from an I/O porttemp_aa = (unsigned INT) temp_dwportval; temp_aa = temp_aa & 0x0f0; // take the 8-bit lower value and set the 4-bit lower to 0; the four digits in height remain unchanged; temp_aa = temp_aa ^ 0x0b; // set C0, C1, and C3 to 1, C2 to 0, and the four digits in height remain unchanged; unsigned int wrivalue; wrivalue = wrivalue> 8; // obtain the 4-bit high value temp_aa = temp_aa ^ wrivalue; // reverse the C0, C1, and C3 values in the lower 4 bits of the write value. The C2 BITs remain unchanged. The high 4 bits keep the port value unchanged. setportval (m_nportcontrol, (DWORD) temp_aa, 1); // In the written value, the 4-bit high keeps the original port value unchanged. // What level is the 4-bit low written, and what level is the connector?
VC Implementation of Digital Input in PC Parallel Port
(1): VC Implementation of status port number input
To test the digital input of the parallel port status port, you can pin the data Port Pin pin2 ~ Pin6 connects to status port pins pin15, pin13, pin12, pin10, and pin11. After the PIN is connected, data is output from the data port and read from the status port and control port. The read data should be consistent with the written data, with a data range of 0 ~ 31.
DWORD dwPortVal; unsigned int ValueGet = 0; // obtain the port address WORD m_nport = (WORD) 0x379; // obtain the Port Data GetPortVal (m_nport, & dwPortVal, 1 ); valueGet = (unsigned int) dwPortVal; ValueGet = ValueGet ^ 0x80; // keep the State value consistent with the value at the connector; ValueGet = ValueGet & 0xF8; // remove S0 ~ S2 bit; ValueGet = ValueGet> 3; // shifts the value three places to the right ~ S3 becomes 5 lower
(2): VC implementation of control port Digital Input
To test the digital input of the parallel port control port, you can pin the data Port Pin Pin2 ~ Pin5 is connected to the control port pins Pin1, Pin14, Pin16, and Pin17. After the PIN is connected, data is output from the data port and read from the status port and control port. The read data should be consistent with the written data, with a data range of 0 ~ 15.
// Obtain the port address WORD m_nport = (WORD) 0x37A; // ==== convert C0 ~ Position C3: 1. Even if the connector is high, set the control port to the input port ==== DWORD temp_dwPortVal; unsigned int temp_aa; GetPortVal (m_nport, & temp_dwPortVal, 1 ); // obtain the current port value temp_aa = (unsigned int) temp_dwPortVal; temp_aa = temp_aa & 0x0F0; // set the value of the Lower 8 bits to 0 when the value is 4 bits; the four digits in height remain unchanged; temp_aa = temp_aa ^ 0x4; // set C0, C1, and C3 in the four digits below to 0, C2 to 1, and the four digits in height remain unchanged; setPortVal (m_nport, (DWORD) temp_aa, 1); // In the written value, the original value of the port remains unchanged for the 4-bit high, and the 4-bit low value is the write high level, even if the connector is high ============================ unsigned int ValueGet = 0; DWORD dwPortVal; // get Port Data GetPortVal (m_nport, & dwPortVal, 1); ValueGet = (unsigned int) dwPortVal; ValueGet = ValueGet ^ 0x0B; // keep C0, C1, the C3 bit value is equal to the value of the connector. ValueGet = ValueGet & 0x0F; // remove the 4-bit high value.
(3): The control port and status port are combined into 9-digit VC input.
To test the digital input of the parallel port, you can pin the data Port Pin Pin2 ~ Pin9 is connected to control port pins Pin1, Pin14, Pin16, Pin17, and status port pins Pin15, Pin13, Pin12, and Pin10, and Pin11 pin is connected to the home pin GND or suspended. After the PIN is connected, the data is output from the data port and read from the status port and control port. The data read should be consistent with the data written, when the Pin11 pin is connected to the GND pin, the data range is 0 ~ 255; when the Pin11 pin is left blank, the data range is 256 ~ 511.
Unsigned int ValueGet = 0; // obtain the port address WORD m_nportState = (WORD) 0x379; WORD m_nportControl = (WORD) 0x37A; // Read State PortDWORD dwPortVal; unsigned int ValueState = 0; GetPortVal (m_nportState, & dwPortVal, 1); ValueState = dwPortVal; ValueState = ValueState ^ 0x80; // keep the obtained State value with the connector value; ValueState = ValueState & 0xF8; // remove S0 ~ S2 bits; ValueState = ValueState <1; // shifts the value 1 to the left ~ S3 changes to 5-bit high // Read control Port // ========== convert C0 ~ Position C3: 1. Even if the connector is high, set the control port to the input port ==== GetPortVal (m_nportControl, & dwPortVal, 1 ); // obtain the current value of the port; 4-digit height unchanged; ValueGet = ValueGet ^ 0x4; // set C0, C1, and C3 to 0, C2 to 1, and 4-digit height unchanged; setPortVal (m_nportControl, (DWORD) ValueGet, 1); // In the written value, the 4-bit high keeps the original value of the port unchanged. // The 4-bit low value is the write high level, even if the connector is high ============================ unsigned int ValueControl = 0; getPortVal (m_nportControl, & dwPortVal, 1); ValueControl = (unsigned int) dwPortVal; ValueControl = ValueControl ^ 0x0B; // keep C0, C1, the C3 bits are equal to the connector values. ValueControl = ValueControl & 0x0F; // remove the 4-bit high value. // get 9bit valueValueGet = ValueState ^ ValueControl;
5. Conclusion
This article only introduces the application of PC parallel port as a digital I/O port. When actually applying it to the control system for digital signal communication, you must pay attention to other processing of the parallel port signal, to improve the anti-interference capability, stability and reliability of port signals.