This is the first in a series of articles on using Forth to interact with the real world. We will explore how to control motors of various types (such as servomotors and stepper motors), switch power to devices, and sense the environment. Each article will present a project that can be used to demonstrate the ideas we are going to discuss.
In this first article, I want to lay the foundation for the future columns and discuss the use of the PC parallel port to control stepper motors. We will adopt the fantasy that we are working on some microprocessor-based control application and will be using the PC parallel port as a proxy for the digital I/O channels on our controller. To the extent possible, the code will be written in high level (so that we can illustrate the principles clearly), and will be in ANS Forth.
2. The PC Parallel Port
First, if you haven't already, go to your back issues of Forth Dimensions and find Ken Merk's article "Forth in Control," (FD XVII/2). In that article, Ken talks about using the PC parallel port for eight digital outputs. We will be expanding on that and use some of those other pins to get input as well as to provide output.
A parallel port on the PC is really three address locations which, for conventional use, could be called #Data, #Command, and #Status. The port #Data is at the base address of the parallel port, #Status is at the base address plus one, and #Command is at base plus two.
The base address depends upon which parallel port we are using and the hardware installed in your computer; usually, this address is one of the hex addresses 03BC, 0378, or 0278. The BIOS determines the address and maps it to the parallel ports at boot time. This allows an application to find out where the port is by simply reading the table in memory that starts at 0040:0008. Ken shows in his article how to get this value and set a constant containing the base address for the first port; we will do the same here.