[alt.msdos.programmer] Programming the printer port to read in data.

slahiri@magnus.acs.ohio-state.edu (Santanu Lahiri) (05/22/91)

Hi folks,

I am trying to write a program that can read a switch status - 
switch is open or closed.  The switch is an ordinary switch that I hope
to hook up to the printer port and use data line D0 and the Gnd line to
detect closure.

Here is the background.  This program will be used for some sort of 
theraputic training of a handicapped child.  She will cause the program
to play music notes by closing the switch - the computer plays the notes
as long as she keeps the switch closed.  The program is pretty much
ready, except for this one part.

I'd like to know if this is possible, i.e, the chip can be programmed
to read in such data, and if so, how.  I suspect I need to reprogram
some of the chips control registers.  For checking the switch status I
plan to do continuous polling within a loop.  Also, I do not know the 
exact hardware setup that is to be used, except that it's an IBM clone.

I'd appreciate any pointers you can give, or even alternate methods,
perhaps using the RS-232 ports.  Any suggestion is welcome, as long as
hardware requirements are minimal.  

Thanks.

Santanu

-----------------------------------------
slahiri@magnus.acs.ohio-state.edu 

acm@Sun.COM (Andrew MacRae) (05/22/91)

In article <1991May21.202043.24725@magnus.acs.ohio-state.edu>, slahiri@magnus.acs.ohio-state.edu (Santanu Lahiri) writes:
> Hi folks,
> 
> I am trying to write a program that can read a switch status - 
> switch is open or closed.  The switch is an ordinary switch that I hope
> to hook up to the printer port and use data line D0 and the Gnd line to
> detect closure.

 [text deleted]

> I'd appreciate any pointers you can give, or even alternate methods,
> perhaps using the RS-232 ports.  Any suggestion is welcome, as long as
> hardware requirements are minimal.  

No doubt twenty-five others will respond with the same suggestion, but have
you considered using the game port?  There are lines there that indicate a
button has been pressed.  In fact you could use a modified joystick as your
device and save a little trouble building one.

						Andrew MacRae
						

jayh@ms.uky.edu (Jay Hofacker) (05/22/91)

slahiri@magnus.acs.ohio-state.edu (Santanu Lahiri) writes:

>I am trying to write a program that can read a switch status - 
>switch is open or closed.  The switch is an ordinary switch that I hope
>to hook up to the printer port and use data line D0 and the Gnd line to
>detect closure.
   ...
>I'd appreciate any pointers you can give, or even alternate methods,
>perhaps using the RS-232 ports.  Any suggestion is welcome, as long as
>hardware requirements are minimal.  

I friend of mine did a similar thing for a science project last year, he used
2 pairs of signals on the RS232 serial port to detect two switchs closing.  By
hooking the switchs between RTS/CTS and DTR/CD, you can set the Uart to set CTS
and DTR high, and then watch for RTS and CD to go high, either by poling the
port or setting up an interrupt.  I don't have the details for programming the
serial port handy at the moment, but it is only a matter of a few statements in
Pascal or C to set DTR and CTS high and check them.  If you can't find the
details you need on the serial port, e-mail me and I'll dig them up.


-- 
Jay Hofacker, sysop of the Audio/Visual Exchange, (606)254-1751 3/12/24 MNP 5
Mail: jayh@ms.uky.edu / uk02779@ukpr.uky.edu -- Yes, my signature is only 2 lin

draper@buster.cps.msu.edu (Patrick J Draper) (05/22/91)

Some people have given suggestions to use a joystick port, etc to read
a switch.

What if someone wanted to read a signal from a sound digitizer?
How do you get the port to read a parallel data stream?


------------------------------------------------------------------------
Patrick Draper     "College is supposed to prepare you for the future,
cps.msu.edu               but all my future's behind me." 
draper@cps.msu.edu      -- My GrandPa, age 85, Fall 1990 graduate
                           of Western Michigan University 
------------------------------------------------------------------------

maceache@fox.nstn.ns.ca (Tim Maceachern) (05/22/91)

You can find working code for this from the Microsoft Systems Journal
article of Sept 90.  Get the code from wuarchive.wustl.edu or simtel
on /mirrors/msdos/msjournal/msjv5-5.zip  (on wuarchive).
Tim MacEachern   Software Kinetics Ltd.
   maceache@corp.nstn.ns.ca
 

ressler@CS.Cornell.EDU (Gene Ressler) (05/22/91)

In article <1991May22.104513.22169@msuinfo.cl.msu.edu> draper@buster.cps.msu.edu (Patrick J Draper) writes:
>Some people have given suggestions to use a joystick port, etc to read
>a switch.
>
>What if someone wanted to read a signal from a sound digitizer?
>How do you get the port to read a parallel data stream?

This is to point out that the July 90 BYTE has a blurb on using a printer port
to read and write TTL level signals from/to a Mattel power glove.  There's
wiring and example code.  Only a couple of bits are used here, but it's easy
to generalize to other appications.  I'm sure there are other, perhaps more
general references for doing the same thing, perhaps even other BYTE articles.
One caveat is that I seem to remember the original PC (and therefore the oldest
and/or cheapest clone) printer ports couldn't read the data lines.  There was
a simple hardware hack to fix this, I believe cutting a trace and/or adding
a jumper to the board.

Gene

nengle@copper.ucs.indiana.edu (nathan engle) (05/22/91)

In article <1991May22.104513.22169@msuinfo.cl.msu.edu> draper@buster.cps.msu.edu (Patrick J Draper) writes:
>Some people have given suggestions to use a joystick port, etc to read
>a switch.
>
>What if someone wanted to read a signal from a sound digitizer?
>How do you get the port to read a parallel data stream?

    Different parrallel ports use different techniques. In the original
IBM printer card and many subsequent clones, the data register of a
parrallel port CAN be read and is supposed to come back with whatever
the last value was that you wrote to the outgoing data register. The
ways this works is as follows:

               +-----+  (the LS245 is a bidirectional transceiver that
               |     |   "buffers" (boosts and isolates) the signals
               |     |   from the data bus in the card slot)
===============|     |=========================================\
Bidirectional  |LS245|                                         I
Data from      |     | Bi-directional data on LPT card         I
PC card slot   |     |                                         I
===============|     |========\    /=====================\     /
               |     |        I    I                     I     I
               +-----+        I    I Out             In  I     I
                           +----------+               +-----------+
                           |  LS244   |               |  LS374    |
                           +----------+               +-----------+
                              I    I In              Out I     I
                              I    I                     I     I
                              I    \                     /     I
                              \     \===================/      /
                               \      Data wires to/from      /
                                \    the DB25 connector on   /
                                 \    the back of the PC    /
                                  \=======\     /==========/
                                          I     I
                                          I     I
                            DB25 connector going out to the "Real
                                         World" (tm)

    When you write data to the LPT card it passes from the card slot
through the LS245 transceiver and finally into the "In" side of an LS374
(octal latch). Once data is written to the 374 that data is also echoed on
the "Out" side of the 374, and subsequently is available to be read
either by a device attached to the DB25 or by the LS244 (one-directional
buffer).

    So the trick you need to use in reading back data from the outside
world is to somehow get the 374 to "shut up", thus allowing the inputs
of the LS244 to be driven exclusively by whatever is outside. There are
a two methods that can be used to do this, and different cards use
different methods.

    The first method doesn't require any hardware mods, but should only
be used when you're absolutely sure that the output chip is an LS374.
Basically what you do is write the value 0xFF to the 374 which will then
make all of the 374's data bits 1's (voltagewise that usually comes out to 
about 3V). Then you can individually ground any of the data pins on the
DB25 and the value for the bit that you read back from the LS244 will
be 0 for as long as that pin is pulled down to 0V.

    The drawback of method 1 is that the LS374 remains active during
this time, and grounding one of the pins that it's trying to drive to 3V
causes a little extra strain to be placed on the output driver.
Fortunately, LSTTL is a very hardy logic family and chances are that
you'll never damage anything this way.

    However, if the output driver is just about anything else (i.e. CMOS
or higher powered Schotsky TTL), then the strain could blow the output 
driver.

    So when people realized this, along came method 2. In newer LPT
ports, the card is modified so that the outputs of the 374 (or whatever
is being used for the output driver) can be completely disabled. Older
cards can also be modified to do this. The bit that is used to enable/ 
disable the outputs is usually bit 5 of the control register, but I've
also seen bit 7 used in a Toshiba laptop. Check your manual to see which
bit to use. I haven't bothered to list the I/O addresses of the data or
control registers but they're well documented. Try Thom Hogan's 
_PC_Sourcebook_ (ISBN 1-55615-118-7) for the hard facts.

    Obviously there's a lot more that could be said on this topic. If
you'd like to read something I recommend Bruce Eckel's _Computer_
_Interfacing_With_C_And_Pascal_ (not sure of the ISBN, try your local
bookstore).

>------------------------------------------------------------------------
>Patrick Draper     "College is supposed to prepare you for the future,
>cps.msu.edu               but all my future's behind me." 
>draper@cps.msu.edu      -- My GrandPa, age 85, Fall 1990 graduate
>                           of Western Michigan University 
>------------------------------------------------------------------------

--
Nathan Engle             Software Juggler
Indiana University       Dept of Psychology
nengle@copper.ucs.indiana.edu

mph@hp835.mitek.com (Mike Hare) (05/22/91)

In article <1991May22.104513.22169@msuinfo.cl.msu.edu> draper@buster.cps.msu.edu (Patrick J Draper) writes:
>Some people have given suggestions to use a joystick port, etc to read
>a switch.
>
>What if someone wanted to read a signal from a sound digitizer?
>How do you get the port to read a parallel data stream?
>

Now, you are getting more compilcated..  If the sound digitizer has an
outboard parallel data port and clock port, you may be able to read the LPT
port fast enough, though I don't know what you will have time to do
with the data once you get it. (I guess I don't know your sample rate..)
Real time data manipulation of music (say) sampled at 40K is right out.
(Even on a high speed 386, it is hard to sample higher than 12K and that
is with an inboard A2D controller.)

Good Luck
- Mike

gcary@SRC.Honeywell.COM (Greg Cary) (05/23/91)

Just as a note about writing to the parallel port, be warned that the
handshaking output pins on the printer port are open collector and are
pulled up by 4.7k resistors.  This is only a problem if you are using
the pins for something high speed and need the output to return high
right away after you have turned them on low (because they are open
collector they float back up at a rate that depends upon the resistor
value).  Also note that some of the the handshaking lines are inverted
so as to reflect the sense of the handshaking line (if a line is
active low, writing a "1" to that pin makes the output go low).  This
is the way I remember it when I worked on and old printer port card.
Newer cards might be somewhat different.
Greg Cary
gcary@SRC.honeywell.com    office: (612)782-7683   FAX: (612)782-7438

dougs@tvnews.tv.tek.com (Doug Stevens) (05/23/91)

It seems that it is not generally known that each printer port contains
three registers (some cheapo printer cards only implement two). 

The register at offset 0 from the I/O location of the printer is write-only,
for data (there is a diagnostic read-back, which is the same as what was
written if the card is working properly).

The register at offset 1 contains:

	Bit		Fxn
	---		---
	7		Busy			(~read)
	6		Ack			(read)
	5		Out of paper		(read)
	4		Printer selected	(read)
	3		Error			
	2		IRQ occurred		(read)
	1-0		Reserved		(read)

The register at offset 2 contains:

	Bit		Fxn
	---		---
	7-5		Reserved
	4		Enable IRQ
	3		Select printer		(~read, ~write)
	2		Init printer		(read, ~write)
	1		Auto LF			(~read, ~write)
	0		Strobe			(~read, ~write)

I think that it is at offset 2 that you must write all 1's to the port
before you can use it to read.

There may be typo's here -- caveat emptor!