Parallel Programming Tutorial

Source: Internet
Author: User

Parallel Port

In 1284, the IEEE 1994 standard defined five parallel transmission modes:

1) compatibility mode

2) nibble Mode

3) byte mode

4) EPP enhanced parallel port Mode

5) ECP Extended Parallel Port

The hardware connector of the parallel port is db25, which is divided into three groups, such

 

1) Data lines (Data Bus) data lines
2) control lines
3) status lines
The data line is used to transmit data. The control line is used to control peripheral devices. The peripheral devices return status signals through status lines. These lines are connected to internal data registers, control registers, and status registers respectively.

The signal of the db25 pin is as follows:

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 register:

The data, control, and status lines are connected to the registers that match the computer's internal systems. Therefore, you can program these registers and read and write them through the 'C' or 'basic 'language.

Standard parallel port registers include:

1) Data Register data register
2) Status Register
3) control register

By writing these registers, the corresponding voltage signals will be transmitted online, which can be measured by a multimeter. The signals sent to the parallel port can also be read through these registers, for example: we write "1" to the data register, and data0 will be raised to + 5 V voltage, so that we can open or close the data line or control line through programming.

 

Where are these registers?

In ibm pc, these registers are Io ing and are unique addresses. On a typical PC, the base address of LPT1 is 0x378, and the base address of lpt2 is 0x278, the base address of lpt3 is 0x3bc.

The data register is on the base address, and the Status Register is on the base address + 1. The control register is on the base address + 2. Therefore, once we know the base address, we can calculate the address of other registers.

Register

LPT1

Lpt2

Lpt3

Data registar (baseaddress + 0)

Zero X 378

Zero X 278

0x3bc

Status Register (baseaddress + 1)

Zero x 379

Zero x 279

0x3bd

Control Register (baseaddress + 2)

0x37a

0x27a

0x3be

Programming specifications:

Almost all programming languages provide database functions to access the parallel port. For example, Borland C provides the "inportb" and "outportb" functions to read or write the IO ing addresses of external devices. This tutorial provides examples of VC, but it is easy to transplant to other compilers such as Borland C and TC. VB does not directly provide an access function for the parallel port, but it can be easily added through DLL. VC provides two functions to access the IO ing of the peripheral device, and read "_ indium, "_ outp" is written. These functions are declared in "conio. H.

Simple hardware testing program:

VC ++ example:

Step 1: Create a project:

 

 

 

Enter the code in example. cpp after clicking finish:

 

# Include "stdafx. H"

# Include "conio. H"

# Include "stdio. H"

# Include "string. H"

# Include "stdlib. H"

 

Int main (INT argc, char * argv [])

{

Short data;

 

If (argc <2)

{

Printf ("usage/n ");

Printf ("partest1.exe,/n ");

Return 0;

}

If (! Strcmp (argv [1], "read "))

{

Data = _ Indium (atoi (argv [2]);

Printf ("data read from parallel port is ");

Printf ("% d/n", data );

}

If (! Strcmp (argv [1], "write "))

{

_ Outp (atoi (argv [2]), atoi (argv [3]);

Printf ("data written to parallel port is ");

Printf ("% s/n", argv [3]);

}

Return 0;

}

Compile this program and copy it to "C :/"

How to test the program?

Connect the hardware, open the command prompt box, switch to C: input: partest1.exe write 888 press enter, if all are correct, then the LED1-LED8 will shine, Here 255 is 0x888, that is, the base address (data register) of lpt1 ). 255 is the data written into the data register. If the "partest1 read 888" program is input, the value of the data register will be read from the parallel port and displayed.

From: http://blog.csdn.net/lqrensn/article/details/5654708

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.