Zibb

Design Idea

Extend the timing capabilities of a PC

Edited by Bill Travis

Martin Connors, Athabasca University, Athabasca, AB, Canada -- EDN, 1/10/2002

Even when you use the internal timing registers and under DOS, a PC cannot easily measure time intervals with better time resolution than a millisecond. Measuring long intervals with even this precision is a waste of many CPU cycles. A microcontroller is well-suited for this task; you can easily integrate a PIC with a PC to extend the timing precision into the microsecond range for periods from tens of microseconds to more than 24 hours. The flash-programmable PIC16F84 microcontroller from Microchip Technology (www.microchip.com) is an inexpensive and widely used device. The precision timer in Figure 1 requires only the IC, two capacitors, and a crystal and accepts direct input of timing data to a PC via the parallel port. The PIC16F84 draws only 2 mA and can operate from an output pin in the parallel port without a battery. You can assemble the circuit on a small pc board with a male DB-25 connector glued or soldered to one end for connection to the parallel port, LPT1. In this example, the timing signal occurs when you block a photogate comprising a paired LED and a phototransistor.

Listing 1 represents the timing application implemented, which comprises two basic parts. The first part waits for a signal and starts a loop that checks the continuing presence of the signal and increments 32 timing bits while the signal is present. The second part transmits 32 bits of timing information to an external device, using one data-output line and two handshaking lines. With a 4-MHz crystal, most instructions take 1 µsec, so the timing loop is 5 µsec long. You can run newer PIC16F84s with a 20-MHz clock, so, in principle, the timing loop can be 1 µsec long. Port A of the PIC serves for the timing signal on bit 3 and for communication. A minor coding change allows you to use positive or negative logic levels. If the timing signal is present at the start of the program, an error flag arises, with an output of 4 bytes of 0xFF. A similar error occurs if the signal is present long enough (roughly a day) to cause overflow of the counter. DATO (data output) occurs through bit 0. The routine uses two handshake lines: VALID on bit 1 from the PIC to signal the presence of valid data on the DATO line and SEND from the PC to bit 2, signifying that the PC is ready to receive data. This robust transmission method does not depend on timing characteristics in a critical way.

Listing 2 (pg 76) shows sample C code for Borland Turbo C for DOS with a simple timing conversion that doesn't take account of the overhead of byte overflow. After the PIC times an event, it waits for the PC to signal that it wants to download data. The transmission protocol for transmitting 1 bit of data is as follows: PC SEND is low, and the PIC polls it. PIC VALID is initially low; the PC raises SEND and polls VALID. In response, the PIC puts DATA on the line. The PIC than raises VALID and polls SEND; in response, the PC reads DATA. The PC then lowers SEND, and the PIC lowers VALID. This operation repeats for 32 bits, starting with the lowest bit of the lowest byte and proceeding to the highest bit of the highest (fourth) byte. Although this transmission method is inefficient, it is robust, and the polling timing is unimportant. The efficiency matters little, because the method involves little data transfer. By referring to the Listing 1 and Listing 2, you can "step through" the process to see how the transfer takes place. Listing 2 includes a test routine that allows you to supply a signal from the PC to test the circuit's operation.

Is this the best Design Idea in this issue? Select at www.ednmag.com.



Reed Business Information Resource Center

Featured Company


Related Resources

ADVERTISEMENT

ADVERTISEMENT

Feedback Loop


Post a CommentPost a Comment

There are no comments posted for this article.

Related Content

 

By This Author

There are no additional articles written by this author.


ADVERTISEMENT

Knowledge Center



Technology Quick Links

EDN Marketplace


©1997-2009 Reed Business Information, a division of Reed Elsevier Inc. All rights reserved.
Use of this Web site is subject to its Terms of Use | Privacy Policy

Please visit these other Reed Business sites