8254 Timer and Counter Board

Status: (05/31/00) edited schematic

This is a photo of a general purpose timer/counter card you can wirewrap in a weekend. It plugs into your PC's ISA bus just like a sound or modem card. It gives high-resolution timing (microseconds). Because of its programmablity, it is very powerful and flexible. You can even control DC or servo motors using pulse-width modulated (PWM), or stepper motors using its square wave capabilities. The figure below is the actual output (square wave) from the 8254 card captured by an oscilloscope:

Audience

Anyone who wants to: This is a small list of common timing and counting applications that the 8254 card solves. It requires very little programming (QBasic or Turbo C for example) and parts are cheap and easy to find.

Motivation

Timing and counting are two very common requirements in PC interfacing of devices (e.g. motors, temperature sensors). There are many different solutions but typically are either software or hardware based. For example, suppose you want your PC to control an event every X seconds. In software, you could program a dummy delay loop such as:


   DO
      REM A dummy delay which does nothing
      FOR X = 0 TO N
      REM do nothing
      NEXT X
      REM Delay finished, do the event
      GOSUB EVENT
   WHILE(userDidNotQuit)			  

N is some integer you choose (using a stopwatch for instance) that approximates the delay time you need. There are several problems with this approach. First, it is imprecise (depending on your reflexes with a stopwatch). Second, the FOR...NEXT loop executes faster on different computers (e.g. faster on a Pentium than on a 286). Third, N is hard to tune for very short times (e.g millisecond or microsecond ranges)

One can use more sophisticated software solutions, which commonly use the PC's native timer. But typically 55 ms is the shortest time they can measure which is too long for many applications. There are software tricks (such as interrupts and DMA) for shorter times but they are not always intuitive and easy to use.

The 8254 Solution

The 8254 was designed originally by Intel as a one-chip solution to many timing and counting problems. It is easy to both use and interface to. It has three independent 16-bit counters with six programmable modes. For example, the oscilloscope output above used one counter programmed for square wave mode.

The 8254 increments its on-chip counters using pulses. It can be any pulse (0 to +5V transition). For example, you can add a SPST switch to the 8254 and the counter will increment by one for every toggle.

Motor encoder disks also generate pulses and can be used to increment the 8254's counters. Used in this manner, the 8254 can count the number of motor rotations. You can also combine counters to make a single 32 or 48-bit counter which may be useful in quadrature applications.

The 8254 is also a timer. This is because you can increment the counter with an oscillator. Thus if you use a 1.0 MHz oscillator, the counter would increment every 1 microsecond. By reading the counter's value at any instant, you can determine how much time has passed. For this reason, the 8254 is often called a timer chip, but technically, it is a counter chip.

Lastly, each counter has a gate which allows you to instantly start or stop counting. The figure below shows a pulse-width-modulated (PWM) signal captured by an oscilloscope using two of the 8254's three counters. You can easily program any duty cycle you want. PWM is a commonly used for controlling motor speeds.

This tutorial is broken down into the following sections: