Measurement Computing USB-4302 User Manual
Page 20

USB-4302 User's Guide
Functional Details
20
Example 2
Problem
: An external device generates an active high signal while a switch is depressed. The switch is
depressed for at most one second. You need to measure the time the switch is depressed with microsecond
accuracy.
Solution
: Microsecond accuracy for one second requires counting to at most 1,000,000. However, each counter
can count only to 65,535. You can use some counter features to cascade two of the counters in order to count
higher. Configure Counter 1 and Counter 2 as follows:
Configure Counter 1
:
1.
Using InstaCal, configure the counter 1 source for 1 MHz. As shown in the "Internal frequency values"
table on page 16, an internal base frequency of 1.0000 MHz corresponds to FREQ1.
The internal base frequency of 1 MHz provides an ideal way to measure time to the microsecond, since
1 count at 1 MHz equals 1 microsecond.
2.
The counter can count either up or down, so for this example set the counter to count up.
3.
The counter should count only while the input signal from the switch is active. To do this, connect the input
signal to the gate 1 pin (
CTR1GATE
), and configure the counter gate control for active high level Gate n.
4.
Load the Counter 1 Load Register with a value of zero, since you want the counter to reach as high a count
as possible before reaching TC.
5.
To cascade Counter 1 with Counter 2, turn Recycle mode on. This is done so Counter 1 continuously
counts to TC, then wraps around and starts counting at 0 again.
6.
Set reload to always load from the Load register, since you want to restart at 0 after every TC.
7.
Enable binary mode.
8.
Set the Counter 1output to inactive; Counter 1 requires no special gating.
Configure Counter 2
:
1.
Set the count source for Counter 2 to be the (TC n-1). This causes Counter 2 to increment when Counter 1
reaches TC.
2.
Load the Counter 2 Load Register 2 with a value of zero, since you want Counter 2 to start at 0 and count
up. Counter 2 requires no gating, no special gate, no reloading, and no recycling.
3.
Enable binary mode.
When the button is depressed, Counter 1 will start counting from 0 to 65,535 over and over again. Every time
Counter 1 reaches 65,535, Counter 2 increments by 1 (starting from 0). When the button is released, Counter 1
stops counting. At that point, the count values can both be read.
The total time the button is pressed in µs is (Counter 2 counts * 65,536) + Counter 1 counts µs. If Counter 2 is
at 9 and Counter 1 is at 34,671, the total time is (9 * 65,536) + 34,671 = 624,495 µs = 0.624495 s.