Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

inport() in C

Status
Not open for further replies.

digital

New Member
I want to create a program to count the number of times the d0 pin of the parallel port goes from high to low and have this code so far. I tried sending +6V in pulses to d0 and its not counting, please help me out.

#include <stdio.h>
#include <conio.h>
#include <dos.h>

#define PORT 0x378

void main()
{
unsigned int data=0;
outport(PORT, 0);
clrscr();

while(!kbhit());
{
data=inportb(PORT)+data;
}

printf("\n count = %d", data);
sleep(3);
}
 
Is the address to your parallel port correct? What OS are you running this under? Do programs have direct access to the hardware?

Dan East
 
Dan East wrote:
Is the address to your parallel port correct? What OS are you running this under? Do programs have direct access to the hardware?

I am running win98
I believe that 0x378 is the correct address
I also believe that programs have direct access to hardware

How would you rewrite this code to make it work on your PC :?:
 
digital said:
Dan East wrote:
Is the address to your parallel port correct? What OS are you running this under? Do programs have direct access to the hardware?

I am running win98

Under any 16 bit Windows you can directly access the hardware - under 32 Windows you can't!.

I believe that 0x378 is the correct address

In both DOS and Windows there are functions available that return the installed parallel port addresses, you should use those functions to allocate your actual address!.

I also believe that programs have direct access to hardware

Yes they do! (under 16 bit Windows!).

How would you rewrite this code to make it work on your PC :?:

I don't use C, but a standard parallel port is unidirectional (it's only an output), for inputs you need to use some of the handshake lines, or use the port in a bidirectional mode - there is LOT'S of information about this on the net!.
 
Nigel Goodwin wrote:
Under any 16 bit Windows you can directly access the hardware - under 32 Windows you can't!.

What is ment by 16 & 32 bit Windows ?

How should I send the +V signal/s to the pin/s, by conecting a positive terminal of a battery to a pin, and thats it ?
 
digital said:
Nigel Goodwin wrote:
Under any 16 bit Windows you can directly access the hardware - under 32 Windows you can't!.

What is ment by 16 & 32 bit Windows ?

Windows 3.x, 95, 98 and ME are 16 bit, any of the NT based Windows (NT, 2000, XP) are 32 bit.

How should I send the +V signal/s to the pin/s, by conecting a positive terminal of a battery to a pin, and thats it ?

It sounds rather like you don't have the faintest clue what you're trying to do?, if you're not careful you are VERY likely to blow your motherboard!.

There are plenty of websites that explain how to use the parallel port, I suggest you try googling for 'parallel port tutorial' and do some reading.
 
that beyond logic site that instruite gave is loaded with info on the parallel port..
and please be careful you dont damage your motherboard ,it is not fun..
 
Digital wrote:
How should I send the +V signal/s to the pin/s, by conecting a positive terminal of a battery to a pin, and thats it ?
If you dont connect the Ground of the battery to the Ground of the parallel port then it can surely blow your motherboard
 
digital said:
I want to create a program to count the number of times the d0 pin of the parallel port goes from high to low and have this code so far. I tried sending +6V in pulses to d0 and its not counting, please help me out.

#include <stdio.h>
#include <conio.h>
#include <dos.h>

#define PORT 0x378

void main()
{
unsigned int data=0;
outport(PORT, 0);
clrscr();

while(!kbhit());
{
data=inportb(PORT)+data;
}

printf("\n count = %d", data);
sleep(3);
}

LOL

d0 - d7 pins are output pins only and they use the address 0x378 to output data
pins 10->13 and 15 are input and they use address 0x379 for input data

so you'd do something like this
data=inportb(0x379)+data; Or whatever
or this in assembler

mov dx, 0x379
IN al, dx

the data is in 5 Upper bits of AL (al is 8 bits wide)
 
I used a 9V battery with the + terminal connected to a 22k resistor(multimeter reads 5V) before connecting to a status pin of the parallel port(tried 13, 15) and the ground from the battery to a ground in the parallel port(pins 18-25 ?)

I keep getting a value of 120 for output, with the battery connected and then disconnected. I also tried sample code from
http://www.geocities.com/gear996/sub/parallelin.html

What is the problem :?:
 
I used a 9V battery with the + terminal connected to a 22k resistor(multimeter reads 5V) before connecting to a status pin of the parallel port(tried 13, 15) and the ground from the battery to a ground in the parallel port(pins 18-25 ?)
I assume that when you say before connecting to status pin then you connected your multimeter at open end of 22K resistor and ground
In this case the multimeter should read 9V and not 5V?
If my assumption is correct then either check your multimeter or your battery.

I keep getting a value of 120 for output, with the battery connected and then disconnected. I also tried sample code from
http://www.geocities.com/gear996/sub/parallelin.html
you have tied pin 13 and 15 to battery which means bit S4 and S3 are set to HIGH
you are getting data Decimal (120) = Hex ( 78 ) = BCD (0111 1000) = (/S7 S6 S5 S4 S3 - - - )
from above statement S4 = S3 = 1 which is correct
I guess you have left pins 10, 11, 12 open so which makes the pin state HIGH and its giving the corresponding data
 
I just want to use a single pin(including ground of course) but want I ment was that I tRied(NOT tied lol) pins 13 and 15 and that I keep getting an output value in my program of 120, regardless if I set voltage on or off to a pin.

Whats this value(120) supposed to mean, could it be an error code :?:

Must the voltage be a regulated constant 5V such as using a 7805, depicted in http://www.geocities.com/gear996/sub/parallelin.html :?:
 
Setting a voltage on or off may be (give ) the same results..
it depends on how you are doing it..
as instruite said your parallel port (input) pins float high ..
SO if you want to read a low you must ground the input pin (Through a resistor is probably best )...
 
Whats this value(120) supposed to mean, could it be an error code
As I explained before how value 120 is achieved
you are getting data Decimal (120) = Hex ( 78 ) = BCD (0111 1000) = (/S7 S6 S5 S4 S3 - - - )
from above statement S4 = S3 = 1 which is correct
as you can see this value is the basic value which you will get if you dont connect anything to the parallel port or connect them to logic HIGH (because all the inputs float to high)
So if you want to check the working of your software you need to assure logic LOW at your port
which you can do as suggested by WilliB (connect the port pin to ground using a resistor

Must the voltage be a regulated constant 5V such as using a 7805, depicted in http://www.geocities.com/gear996/sub/parallelin.html
It is good if you use the regulator IC but you can do it without the IC also
Since you have a 9V battery just make a voltage divider network such that it will suppy 5V to the port input

If you use the switch to ON/OFF the port then make sure switch is after the Ground resistor (one connecte to ground and port)
 
if you only want to read 1 pin you need to extract the single bit you need from the byte returned by the inport function...

Easely done with a 'AND'
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top