next up previous
Next: An elaborate example: synchronous Up: number5 Previous: The PC parallel port

A simple example: reading a switch

Reading a simple switch is just a matter of reading the port (either #Status or #Command that has the switch attached to it), see Listing 1. There are, of course, a few minor complications. First, as mentioned above, we should be sure to use pullup resistors on the open collector lines. Second, bit 7 of the #Status port is inverted in hardware so that it reads a zero when the line is high and reads a one when the line is low, bits 0, 2 and 3 of #Command are also inverted. Further, on an MS-DOS system, if your Forth reads the I/O ports via calls through the BIOS, then bits 3 and 6 of #Status are also inverted.

If you are using PFE V0.9.14 and Linux you might have to make some minor patches to the source file support.c (this depends upon the version of the GNU C compiler you are using; I am using V2.7.0). If you can properly read the switch only the first time the ``file'' to the I/O ports is opened, and you get the same value for all subsequent reads then you will have to apply the following diff patch to support.c and rebuild Forth:




\begin{cprog}
1190c1190,1192
< return fseek (fid->f, pos, SEEK_SET) ? errno : 0;...
... fread (p, 1, *n, fid->f); */
> m = read( fileno(fid->f), p, *n);
>
\end{cprog}




next up previous
Next: An elaborate example: synchronous Up: number5 Previous: The PC parallel port
Skip Carter 2008-08-20