


   Reading data into the printer port isn't to tough, but there are a few
tricks. The first thing you should know is that reading 8 bits at a
time from the data lines isn't supported on all cards. The
original PC printer card didn't, but the feature was added later. The
problem is that the output latch Tri-state buffers are locked on, which
only lets you read back the data you wrote to the latch. It looks like
this:

                                     gnd
                                      
                 Ŀ    Ŀ
 8 bits out >latch   >buffer  >printer out
                        
                                            
                                            
 8 bits in <


   The newer cards used an unused latch output to drive the buffer, bit 5
of the command port (base+2). It looks like this:


 Bit 5
 Command port >Ŀ
                                      
                 Ŀ    Ŀ
 8 bits out >latch   >buffer  >printer out
                        
                                            
                                            
 8 bits in <



   Since Tri-state buffers "disconnect" the latch, data can be read from
the printer output pins. This is implimented on some, but not all, LSI
printer port chips as well. The SMC 8211 does not, while the
National PC16553 (dual serial, printer) does.

   One way to test your ports to see if they support this is DEBUG. Try
this:

C:\debug

-O 378 55       ;send 55h to the data output port
-I 378          ;read it back to check
55
-I 37A          ;read command port
04              ;typical, this might also be E4
-O 37A 24       ;write back with bit 5 set
-I 378
##              ;if this is 55h then the port is not bidirectional


  Reading 8 bits at a time can be an iffy thing. Reading 4 bits at a
time, while a litle more complex, is much more reliable. The file
transfer utilitys that use the printer port all work like this. See
reference 1 for more information. Generally they read 4 bits of data and
a status bit fron the status port (base+1).


Reading switch input.

  The printer port can also read up to 4 switches. Pins 1, 14, 16 and 17
all have pull-up resistors. The only hitch is that you have to disconnect
the outputs associated with these pins. The output drivers on the port
are 74LS05 open collector inverters (or equivilant). When the output on
these is zero, they go to ground, but when it is one the outputs
disconnect, allowing them to be driven from outside.


Command Port:
                                   +5
                                   | pull-up resistor
                                   >
       output command port latch   >
                Ŀ                         ______
  D0>        >Ĵ>oo pin 1 Strobe
                   74LS05
                                   
       input command port latch    
                Ŀ         
  D0<        <
                


                                   +5
                                   | pull-up resistor
                                   >
       output command port latch   >
                Ŀ                          _________
  D1>        >Ĵ>oo pin 14 Auto Feed
                   74LS05
                                   
       input command port latch    
                Ŀ         
  D1<        <
                


                                     5+
                                     |pull-up resistor
       output command port latch     >
                                     >
                Ŀ74LS04                    _________
  D2>        >Ĵ>oĴ>oo pin 16 Initalize
                     74LS05
                                     
       input command port latch      
                Ŀ           
  D2<        <o<
                    74LS04



                                   +5
                                   | pull-up resistor
                                   >
       output command port latch   >
                Ŀ                          ____________
  D3>        >Ĵ>oo pin 17 Select Input
                   74LS05
                                   
       input command port latch    
                Ŀ         
  D3<        <
                


  Because the LS05 is an inverter, you must write a zero to disconnect.
Bit 3 is a little different. Because of the 2 inverters in line, write a
one to disconnect it. Bit 3 is also inverted on the input.

  The switches should be connected to ground and the desired input pin.
Pins 18-25 on the port are grounded. I have included a short demo
program called BUTTON and includes the source. The code that reads the
buttons is:



;  code to read switches.
;
;  Circuit is pins 1, 14, 16, 17 to switches,
;                  from switches to common gnd (pins 18-25)
;

start:  mov     dx,037Ah        ;command port (base+2)
        mov     al,00000100b    ;enable input by floating OC outputs
        out     dx,al

readswitch:
        in      al,dx           ;read command port (base+2)
        and     al,0fh          ;mask hi bits
        xor     al,00000100b    ;invert bit 2





  Any comments, questions or criticisms are welcome (and encouraged).

Mark Phillips
CServe 73167,3216

References:

1) "Adapting the Parallel Port for Bidirectional Communication"
  by Ross M. Greenberg. Microsoft Systems Journal, September 1990, pg.107

2) "Magic in the Real World" by Bruce Eckel. Micro Cornucopia, #38
  Nov/Dec 1987, pg.28.
  Reprinted in "Computer Interfacing with Pascal and C"
  ISBN 0-89716-211-0
