<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0047)http://www.boondog.com//tutorials/8254/8254.htm -->
<HTML><HEAD><TITLE>8254</TITLE>
<META http-equiv=Content-Type content="text/html; charset=gb2312">
<META content="MSHTML 6.00.2800.1106" name=GENERATOR></HEAD>
<BODY bgColor=#ddddd0><IMG src="">
<H2>8254 Timer and Counter Board</H2>
<P><FONT color=brown>Status: (05/31/00) edited schematic</FONT>
<P><IMG src="8254.files/8254Card.gif" align=left> 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:
<P>
<CENTER><IMG src="8254.files/sqwave5000.gif"> </CENTER>
<H3>Audience</H3>Anyone who wants to:
<UL>
<LI>create pulse-width-modulated (PWM) signals (e.g. for motor speed control)
<LI>generate +5 V square waves
<LI>count the number of times a switch toggles
<LI>count pulses from a motor wheel encoder disk
<LI>generate timed interrupts for analog-to-digital data acquisition
<LI>implement a PC-controlled oneshot timer
<LI>have a PC-controlled rate generator
<LI>have fine resolution (e.g. 0.1 microsecond) timing instead of imprecise
for...next delay loops </LI></UL>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.
<H3>Motivation</H3>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: <BR><BR><PRE><TT>
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)
</TT>
</PRE><TT>N</TT> 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 <TT>FOR...NEXT<TT> 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)
<P>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.
<H3>The 8254 Solution</H3>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.
<P>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.
<P>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.
<P>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.
<P>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.
<P><IMG src="8254.files/pwm80.gif">
<P>
<P>This tutorial is broken down into the following sections:
<UL>
<LI><A href="http://www.boondog.com//tutorials/8254/8254.htm#Parts">Parts List
with Potential Vendor Source</A>
<LI><A href="http://www.boondog.com//tutorials/8254/8254.htm#Theory">Theory of
Operation</A>
<LI><A
href="http://www.boondog.com//tutorials/8254/8254.htm#Construction">Construction</A>
<LI><A
href="http://www.boondog.com//tutorials/8254/8254.htm#Programming">Programming</A>
<LI><A
href="http://www.boondog.com//tutorials/8254/8254.htm#Final Words">Final
Words</A>
<OL></OL>
<P><A name=Parts></A>
<H2>Parts List and Potential Vendor Source</H2>You can breadboard your 8254
card with parts that are widely available. US-based vendors include <A
href="http://www.jameco.com/">Jameco</A>, <A
href="http://www.digikey.com/">Digikey</A>, <A
href="http://www.jdr.com/">JDR</A> and Radio Shack. Below is the 8254 card's
parts list with part numbers, vendor and prices (in 1999). Note: Boondog has
no association with these vendors.
<P>
<CENTER>
<TABLE cellSpacing=3 border=5 COLSPEC="L20 L20 L20 L20 L20 L20">
<CAPTION align=top>TABLE 1: 8254 Timer/Counter Card</CAPTION>
<TBODY>
<TR>
<TD>PART DESCRIPTION</TD>
<TD>VENDOR</TD>
<TD>PART</TD>
<TD>PRICE EA (1999)</TD>
<TD>QTY</TD>
<TD>NOTES</TD></TR>
<TR>
<TD>8254 CMOS 10 MHZ TIMER CHIP</TD>
<TD>JAMECO</TD>
<TD>52409</TD>
<TD>5.95</TD>
<TD>1</TD>
<TD></TD></TR>
<TR>
<TD>24 PIN WIREWRAP SOCKET</TD>
<TD>JAMECO</TD>
<TD>75416</TD>
<TD>1.25</TD>
<TD>1</TD>
<TD></TD></TR>
<TR>
<TD>1.0 MHZ OSCILLATOR</TD>
<TD>JAMECO</TD>
<TD>27861</TD>
<TD>3.75</TD>
<TD>1</TD>
<TD>SEE BELOW</TD></TR>
<TR>
<TD>34-PIN IDC WIREWRAP HEADER</TD>
<TD>JDR</TD>
<TD>IDH34W</TD>
<TD>3.59</TD>
<TD>2</TD>
<TD>SEE BELOW</TD></TR>
<TR>
<TD>34-PIN IDC SOCKET</TD>
<TD>JAMECO</TD>
<TD>32643</TD>
<TD>0.45</TD>
<TD>2</TD>
<TD></TD></TR>
<TR>
<TD>34-PIN RIBBON CABLE 25 FEET</TD>
<TD>JAMECO</TD>
<TD>37840</TD>
<TD>10.50</TD>
<TD>1</TD>
<TD>MIN. 25' ORDER</TD></TR>
<TR>
<TD>2x8 HEADER PINS</TD>
<TD>JAMECO</TD>
<TD>109567</TD>
<TD>0.35</TD>
<TD>2</TD>
<TD>SEE BELOW</TD></TR>
<TR>
<TD>1x36 HEADER PINS</TD>
<TD>JAMECO</TD>
<TD>68339</TD>
<TD>0.49</TD>
<TD>1</TD>
<TD>SEE BELOW</TD></TR>
<TR>
<TD>JUMPER BLOCK</TD>
<TD>JAMECO</TD>
<TD>22023</TD>
<TD>0.19</TD>
<TD>2</TD>
<TD></TD></TR>
<TR>
<TD>74HC393 4-BIT RIPPLE COUNTER</TD>
<TD>JAMECO</TD>
<TD>45874</TD>
<TD>0.59</TD>
<TD>1</TD>
<TD></TD></TR>
<TR>
<TD>74LS09 AND-GATE</TD>
<TD>JAMECO</TD>
<TD>46391</TD>
<TD>0.25</TD>
<TD>1</TD>
<TD></TD></TR>
<TR>
<TD>14-PIN WIREWRAP SOCKET</TD>
<