For stepper motors we could just change directions of rotation by just reversing the order that we presented the coil patterns, for DC motors we have to reverse the direction of the current through the coil. Conceptually, we need to have two transistor switches in order to do this. One provides a current path in one direction through the motor, the other switch controls current in the opposite direction. Probably the simplest way to achieve this is to use a relay driven by transistor switches. But if we are going to be using PWM speed control relays are not a very attractive choice because the PWM will be cycling them too fast and they would quickly wear out even if they could switch at 1KHz. We can build a transistor switch if we combine a high-side and low-side switch into a single circuit, like in Figure 2. With this circuit we apply a PWM signal on one transistor and turn the other off to get one direction of rotation, and turn the first one off and apply a PWM signal to the second to get the opposite direction of rotation. The two transistors should not be simultaneously switched on.
This simple switch has the disadvantage of requiring a dual polarity power supply. It would be much more convienient to be able to have a circuit that gave us the ability to switch current directions without a dual-rail power supply. Such a circuit is known as an H-bridge. Unfortunately, the implementation of such a bi-directional current switch requires a combination of both low-side switching and high-side switching as before. In addition, it turns out to be more flexible if we do this by using n-type devices for the low side switches and p-type devices for high side switches. Now we have done two things that last time we concluded that we wanted to avoid: i) the use of high side switching, and ii) the use of p-type transistors.
The ultimate result is the relatively complicated circuit that we see in Figure 3 (I am showing a bipolar transistor implementation which requires a voltage level shift on the control signals because of the above listed compromises; This is what the LM-324 op-amps are for. H-bridges are also frequently implemented using MOSFET transistors). If we implemented the H-bridge with only n-type transistors, the control lines connect the bases of 1 and 4 together and 2 and 3 together instead the way we implement it here. The strictly n-type H-bridge requires that the motor voltage be significantly higher than the control voltage (say 12 Volts) whereas the type with both types of transistors can work down to slightly higher than the control voltage (like 6 volts). In any case this circuit is messy enough that we have crossed over a threshold of practicality: Unless you have a very exotic requirement or are doing this for pedagical reasons, its simpler to use an integrated circuit implementation of an H-bridge (such as the Motorola MPC1710A or the SGS Thompson L293D). If you decide to use an IC H-bridge, just make sure that it can handle the current loads that you anticipate will be required, typical chips can drive the motor with currents in the range of 1/2 to 3 or 4 amps.
Listing two, hbridge.fth, shows a typical H-bridge control program. Notice that we can do the typical Forth-thing and build upon the PWM code that we already have. To rotate in one direction transistors 1 and 4 are on, running current through the motor one way. The other rotation direction is accomplished by switching on only transistors 2 and 3.
Note that IC H-bridges are usually controlled by a clock and direction pin, but the discrete component one shown here uses two clock lines one for each direction (for a given direction of rotation one side gets clocked the other side is held low). So the driving code for an IC H-bridge will look somewhat different from the code shown here.
The use of the H-bridge also gives us the useful capability of active braking of the motor. Up until now, we stopped by turning off the controlling transistors and then coasted to a halt. But for some implementations of the H-bridge, one can actually draw energy out of the coils when the motor is spinning making the motor work against itself and thus achieve dynamic braking by switching on both transistors 1 and 2 simultaneously.