www.angelfire.com/dragon/letstry
cwave04 at yahoo dot com

Reading from the parallel port

This is slightly more tricky than writing to the parallel port. We shall need only a single input line which will be called MISO (according to the name of microcontroller pin it will be connected to). We shall choose the parallel port pin S5 (which is pin 12 on the DB-25 connector) for it (arbitrarily). So the mask is

#define MISO (1<<5)

To read this pin we can use the code

val = ((Inp32(PPORT_INP) & MISO)!=0); 

But rather than use this compact form I would suggest that you first go for the more elaborate (and easier to debug) form:

temp = Inp32(PPORT_INP);
printf("%2x",temp);
if(temp & MISO) 
  val = 1;
else
  val = 0;

First read without making any external connection to the port. You should get a 1. Then connect the pin S5 to ground through 1K. Your program should now read a 0.

Again, you may experiment (at your own risk) to see if the 1K resistance is really needed. I tried without it, and things worked.

Doing input and output simulataneously

The microcontroller will send signals to the PC along the MISO line, while the PC will send signals to the microcontroller down the MOSI line. The SCK will keep the timing correct. In fact, that is why the pins have these names. We consider the PC as the Master, and the microcontroller as its Slave.
  • MOSI (Master Out Slave In): Signals come out of the master and into the slave via this line.
  • MISO (Master In Slave Out): Signals come out of the slave and into the master via this line.
  • SCK (Serial ClocK): The clock to time the serial communication. Since the clock will be controlled by the PC (who is the master), this is also an output line from the viewpoint of the PC, and an input line from the viewpoint of the microcontroller.
As both MOSI and MISO will be carrying signal simulataneously, it is important to be able to carry out input and output together.

Now it so happens that we shall need to read the MISO line only when SCK is 1. This is because SCK acts like a window. It is open when it is 1, and closed when it is 0. Both the PC and microcontroller are supposed to hold their resepctive data lines (i.e., MOSI and MISO) at fixed levels while the window is open. All level changes must be done while the window is closed. The microcontroller reads the MOSI line and the PC the MISO line while SCK is 1. So we put this inside the outp function.

void outp(char *rem) {
  int val;
  Out32(PPORT_OUT, outByte);
  delay();


  if(outByte & SCK) {
    val = Inp32(PPORT_INP) & MISO;
    inpByte <<= 1;
    if(val) inpByte |= 1;
  }

  if(!verbose) return;

  fprintf(stderr,"!%4s: ",rem);

  drawWave(outByte,MOSI);
  drawWave(outByte,SCK);
  drawWave(outByte,RST);

  if(outByte & SCK)
    fprintf(stderr,(val ? "\t1" : "\t0"));

  fprintf(stderr,"\n");
}


PrevNext
© Arnab Chakraborty (2008)