Control LED light using C # operation IO port 1-
Author: Levent S. Translation: Aweay
Why do you want to write this article, it is undeniable that this program is not very suitable with C #, I believe that many friends who come 9CBS can easily write such a program in other languages, but if one day you have .NET project Such a demand, then this article may be suitable for you.
What is the port?
The port contains a series of signal lines that can be exchanged with other external devices through this port CPU, such as the Modem, printer, etc. we often see.
Typically, the open signal is "1", the closed signal is "0". Parallel can send 8 bits of data in the same time, receive 5 bits of data. And serial port RS-232 can only send 1 bit data. However, the serial port is multi-direction, so it can be transmitted or received at the same time.
parallel port
D0-D7 is the data cable, S0-S7 is a status line, but S0, S1, S2 are invisible (you can also see from the figure), the status line is used to read data, but S0 is different. It is a timeout flag, and other status lines are used to send data from Section 10-11-12-13-15 (which can be seen is 5). So how can we get these data ports? Very simple: Each parallel port has an address. In Windows 2000, you can see them in the properties of the printer port (lpt1). For example: I am 0378-037F, if it is 10, then 888. You can also See the address of your COM port. Let me explain the meaning of these needle places with printers:
l S0: In EPP (Enhanced Serial Port) mode, this location 1 is in the timeout.
L S1: Useless (estimated is decorative).
L S2: Nothing in most cases.
L S3: Set 0. It is usually called NERROR or NFAULT if the printer occurs.
L S4: If the data is reached, it is set 1. We are usually called SELECT.
L S5: If there is no paper, it is set 1. It is usually called Paperend or PapeRempty or PERROR.
L S6: If the printer gets a byte of data, this location is 0, usually called NACK or NACKNOWLEDGE.
L S7: If the printer is busy, this location is 0, usually called busy.
Control line:
These lines are usually used to output, but sometimes they can be used. They occupy C0-C7 (as shown), but C4, C5, C6, C7 on the interface are not visible, and the port address they take up is 0x37a.
l C0: This pin is reserved. He sends command to read the data on the port (D0-D7). When the computer is started, this bit is set 1, usually called NSTROBE.
l C1: Reserved. You can get the next print line by sending a command to the printer. (LF)
l C2: Reset the printer and empty the data buffer. (Ninitialize)
l C3: Reserved. Set high level open data input. Start the computer to set 0. (NSELECTIN)
Ground:
The needle from G0-G7 is grounded, they are generally used to improve the circuit.
In summary, use the data cable in my application instead of a control line or status line with a reserved bit as a data transmission. The reason is obvious: We can send any data to the data cable, such as 00000000, such 8 needles without any voltage (0 volts); of course, 11111111 (255), so that each needle has a 5 volt voltage. But if we use the control line, he has C0, C1 and C3 are reserved. When we send 0000000, he can only be 0100, so this data is not correct.
signal
Data bit
Pin
direction
-Strobe
¬C0
1
OUTPUT
DATA bit 0
D0
2
OUTPUT
Data Bit 1
D1
3
OUTPUT
Data Bit 2
D2
4
OUTPUT
Data Bit 3
D3
5
OUTPUT
Data Bit 4
D4
6
OUTPUT
DATA bit 5
D5
Seduce
OUTPUT
Data Bit 6
D6
8
OUTPUT
Data Bit 7
D7
9
OUTPUT
-Acknowledge
S6
10
INPUT
Busy
¬
11
INPUT
Paper end
S5
12
INPUT
SELECT IN
S4
13
INPUT
-AUTO feed
¬C1
14
OUTPUT
-Rror
S3
15
INPUT
-Initialize
C2
16
OUTPUT
-Select
¬C3
In one
OUTPUT
Ground
-
18-25
Ground
Circuit
The following circuit diagram illustrates how our circuit works:
The following picture is taken from different angles, indicating the final completion circuit:
Let's take a look at what you need:
l 1-2 m parallel cable (3 meters is also possible, but this voltage will be reduced to 4.7V)
l 9 core cable (8 for connecting diodes, 1 for ground)
l A circuit experiment board (white things in the figure)
l 8 light emitting diodes
lu wild table (not necessary)
l My procedure, make your circuit work J
Here is an introduction to the number of modular transformations here, I believe that this article is small.
coding
Before encoding, you should first determine your OS. If you use Win98, you can access the port through the "Outputb" function. When you upgrade to Win2000, you can't do it. In WinNT, Win2k, WinXP is You can't access port directly. Use INPOUT32.DLL in my program to access the port, this address has this DLL source code.
Note: I am using this address (http:/www.logix4u.net) to import functions, this information may have been updated when you visit.
In my portinterop.cs file, I use this DLL as follows.
Using system;
Using system.Runtime.InteropServices;
Public Class Portaccess, PUBLIC CLASS PORTACCESS
{
[DLLIMPORT ("INPOUT32.DLL", entrypoint = "out32")]
Public Static Extern void Output (int adress, int value);
}
This function carries 2 parameters, one is the address, one is data. If you send data to LPT1, set the address parameter 888 (16) 0x378), if it is lpt2, then 0x278, such as:
Portaccess.output (888, 255);
This is the case:
Portaccess.output (888, 0);
Oh, if you use truboc , it is like this:
#include
#include
#define port 0x378 // Port Address
#define Data Port 0 //
Data
Port
Of the Parallel Cable
Void main (void)
{
_Out (Data, 255); // for all lights on
_OUT (data, 0); // for all lights OFF
}
Simple use of C # operation port is this, if you play your imagination, you can design a lot of practical circuits, you can search for some other circuit newsgroups, I believe you will find more inspiration, such as you Can be made:
Oh, the picture above is the creation of my next article, first selling a car here.
Written in the last
Translator (AWEAY) is the first translation of C # and hardware articles. If there is any local translation, please refer to it, and you also welcome you to visit my site http://siney.yeah.net.