Counter Input Applications
- Updated2025-04-28
- 19 minute(s) read
Refer to the following sections for more information on the various counter input applications available on the USB-6421.
Counting Edges
In edge counting applications, the counter counts edges on its Source after the counter is armed. You can configure the counter to count rising or falling edges on its Source input.
You can also control the direction of counting (up or down), as described in the Controlling the Direction of Counting section. The counter values can be read on demand or with a sample clock.
Single Point (On-Demand) Edge Counting
With single point (on-demand) edge counting, the counter counts the number of edges on the Source input after the counter is armed.
On-demand refers to the fact that software can read the counter contents at any time without disturbing the counting process. The following figure shows an example of single point edge counting.
You can also use a pause trigger to pause (or gate) the counter. When the pause trigger is active, the counter ignores edges on its Source input. When the pause trigger is inactive, the counter counts edges normally.
You can route the pause trigger to the Gate input of the counter. You can configure the counter to pause counting when the pause trigger is high or when it is low. The following figure shows an example of on-demand edge counting with a pause trigger.
Buffered (Sample Clock) Edge Counting
With buffered edge counting (edge counting using a sample clock), the counter counts the number of edges on the Source input after the counter is armed.
The value of the counter is sampled on each active edge of a sample clock and stored in the FIFO. A direct memory access (DMA) controller transfers the sampled values to host memory.
The count values returned are the cumulative counts since the counter armed event. That is, the sample clock does not reset the counter.
You can configure the counter to sample on the rising or falling edge of the sample clock.
The following figure shows an example of buffered edge counting. Notice that counting begins when the counter is armed, which occurs before the first active edge on Sample Clock.
Controlling the Direction of Counting
In edge counting applications, the counter can count up or down. You can configure the counter to do the following:
- Always count up
- Always count down
- Count up when the Counter 0 B input is high; count down when it is low
Pulse-Width Measurement
In pulse-width measurements, the counter measures the width of a pulse on its Gate input signal. You can configure the counter to measure the width of high pulses or low pulses on the Gate signal.
You can route an internal or external periodic clock signal (with a known period) to the Source input of the counter. The counter counts the number of rising (or falling) edges on the Source signal while the pulse on the Gate signal is active.
You can calculate the pulse width by multiplying the period of the Source signal by the number of edges returned by the counter.
A pulse-width measurement is accurate even if the counter is armed while a pulse train is in progress. If a counter is armed while the pulse is in the active state, it waits for the next transition to the active state to begin the measurement.
Single Pulse-Width Measurement
With single pulse-width measurement, the counter counts the number of edges on the Source input while the Gate input remains active.
When the Gate input goes inactive, the counter stores the count in the FIFO and ignores other edges on the Gate and Source inputs. Software then reads the stored count. The following figure shows an example of a single pulse-width measurement.
Implicit Buffered Pulse-Width Measurement
An implicit buffered pulse-width measurement is similar to single pulse-width measurement, but buffered pulse-width measurement takes measurements over multiple pulses.
The counter counts the number of edges on the Source input while the Gate input remains active. On each trailing edge of the Gate signal, the counter stores the count in the counter FIFO. A DMA controller transfers the stored values to host memory. The following figure shows an example of an implicit buffered pulse-width measurement.
Sample Clocked Buffered Pulse-Width Measurement
A Sample Clocked Buffered pulse-width measurement is similar to single pulse-width measurement, but buffered pulse-width measurement takes measurements over multiple pulses correlated to a sample clock.
The counter counts the number of edges on the Source input while the Gate input remains active. On each sample clock edge, the counter stores the count in the FIFO of the last pulse width to complete. A DMA controller transfers the stored values to host memory. The following figure shows an example of a sample clocked buffered pulse-width measurement.
Pulse versus Semi-Period Measurements
In hardware, pulse measurement and semi-period are the same measurement. Both measure the high and low times of a pulse. The functional difference between the two measurements is how the data is returned.
In a semi-period measurement, each high or low time is considered one point of data and returned in units of seconds or ticks. In a pulse measurement, each pair of high and low times is considered one point of data and returned as a paired sample in units of frequency and duty cycle, high and low time or high and low ticks. When reading data, 10 points in a semi-period measurement gets an array of five high times and five low times. When you read 10 points in a pulse measurement, you get an array of 10 pairs of high and low times.
Also, pulse measurements support sample clock timing while semi-period measurements do not.
Pulse Measurement
In pulse measurements, the counter measures the high and low time of a pulse on its Gate input signal after the counter is armed.
A pulse is defined in terms of its high and low time, high and low ticks or frequency and duty cycle, which is similar to the pulse-width measurement, except that the inactive pulse is measured as well.
You can route an internal or external periodic clock signal (with a known period) to the Source input of the counter. The counter counts the number of rising (or falling) edges occurring on the Source input between two edges of the Gate signal.
You can calculate the high and low time of the Gate input by multiplying the period of the Source signal by the number of edges returned by the counter.
Single Pulse Measurement
Single (on-demand) pulse measurement is equivalent to two single pulse-width measurements on the high (H) and low (L) ticks of a pulse.
The following figure shows an example of a single (on-demand) pulse measurement.
Implicit Buffered Pulse Measurement
In an implicit buffered pulse measurement, on each edge of the Gate signal, the counter stores the count in the FIFO. A DMA controller transfers the stored values to host memory.
The counter begins counting when it is armed. The arm usually occurs between edges on the Gate input, but the counting does not start until the desired edge. You can select whether to read the high pulse or low pulse first using the StartingEdge property in NI-DAQmx.
The following figure shows an example of an implicit buffered pulse measurement.
Sample Clocked Buffered Pulse Measurement
A sample clocked buffered pulse measurement is similar to single pulse measurement, but a buffered pulse measurement takes measurements over multiple pulses correlated to a sample clock.
The counter performs a pulse measurement on the Gate. On each sample clock edge, the counter stores the high and low ticks in the FIFO of the last pulse to complete. A DMA controller transfers the stored values to host memory.
The following figure shows an example of a sample clocked buffered pulse measurement.
Semi-Period Measurement
In semi-period measurements, the counter measures a semi-period on its Gate input signal after the counter is armed. A semi-period is the time between any two consecutive edges on the Gate input.
You can route an internal or external periodic clock signal (with a known period) to the Source input of the counter. The counter counts the number of rising (or falling) edges occurring on the Source input between two edges of the Gate signal.
You can calculate the semi-period of the Gate input by multiplying the period of the Source signal by the number of edges returned by the counter.
Single Semi-Period Measurement
Single semi-period measurement is equivalent to single pulse-width measurement.
Implicit Buffered Semi-Period Measurement
In implicit buffered semi-period measurement, on each edge of the Gate signal, the counter stores the count in the FIFO. A DMA controller transfers the stored values to host memory.
The counter begins counting when it is armed. The arm usually occurs between edges on the Gate input. You can select whether to read the first active low or active high semi period using the CI.SemiPeriod.StartingEdge property in NI-DAQmx. The following figure shows an example of an implicit buffered semi-period measurement.
Frequency Measurement
You can use the counters to measure frequency in several different ways.
Low Frequency with One Counter
For low frequency measurements with one counter, you measure one period of your signal using a known timebase. You can route the signal to measure (fx) to the Gate of a counter.
You can route a known timebase (fk) to the Source of the counter. The known timebase can be an onboard timebase, such as 100 MHz Timebase, 20 MHz Timebase, or 100 kHz Timebase, or any other signal with a known rate.
You can configure the counter to measure one period of the gate signal. The frequency of fx is the inverse of the period. The following figure illustrates this method.
High Frequency with Two Counters
For high frequency measurements with two counters, you measure one pulse of a known width using your signal and derive the frequency of your signal from the result.
In this method, you route a pulse of known duration (T) to the Gate of a counter. You can generate the pulse using a second counter. You can also generate the pulse externally and connect it to a DIO terminal. You only need to use one counter if you generate the pulse externally.
Route the signal to measure (fx) to the Source of the counter. Configure the counter for a single pulse-width measurement. If you measure the width of pulse T to be N periods of fx, the frequency of fx is N/T. The following figure illustrates this method.
Another option is to measure the width of a known period instead of a known pulse.
Large Range of Frequencies with Two Counters
By using two counters, you can accurately measure a signal that might be high or low frequency. This technique is called reciprocal frequency measurement.
When measuring a large range of frequencies with two counters, you generate a long pulse using the signal to measure. You then measure the long pulse with a known timebase. The USB-6421 can measure this long pulse more accurately than the faster input signal.
You can route the signal to measure to the Source input of Counter 0, as shown in the following figure. Assume this signal to measure has frequency fx. NI-DAQmx automatically configures Counter 0 to generate a single pulse that is the width of N periods of the source input signal.
NI-DAQmx then routes the Counter 0 Internal Output signal to the gate of Counter 1. You can then route a signal of known frequency (fk) as a counter timebase to the Counter 1 Source input. NI-DAQmx configures Counter 1 to perform a single pulse-width measurement. Suppose the result is that the pulse width is J periods of the fk clock.
From Counter 0, the length of the pulse is N/fx. From Counter 1, the length of the same pulse is J/fk. Therefore, the frequency of fx is given by fk = fk * (N/J).
Choosing a Method for Measuring Frequency
The best method to measure frequency depends on several factors including the expected frequency of the signal to measure, the desired accuracy, how many counters are available, and how long the measurement can take.
Which Method is Best?
This depends on the frequency to be measured, the rate at which you want to monitor the frequency and the accuracy you desire.
- Low frequency measurements with one counter is a good method for many applications. However, the accuracy of the measurement decreases as the frequency increases.
- High frequency measurements with two counters is accurate for high frequency signals. However, the accuracy decreases as the frequency of the signal to measure decreases. At very low frequencies, this method may be too inaccurate for your application. Another disadvantage of this method is that it requires two counters (if you cannot provide an external signal of known width). An advantage of high frequency measurements with two counters is that the measurement completes in a known amount of time.
- Measuring a large range of frequencies with two counters measures high and low frequency signals accurately. However, it requires two counters, and it has a variable sample time and variable error % dependent on the input signal.
The following table summarizes some of the differences in methods of measuring frequency.
Method | Number of Counters Used | Number of Measurements Returned | Measures High Frequency Signals Accurately | Measures Low Frequency Signals Accurately |
---|---|---|---|---|
Low frequency with one counter | 1 | 1 | Poor | Good |
High frequency with two counters | 1 or 2 | 1 | Good | Poor |
Large range of frequencies with two counters | 2 | 1 | Good | Good |
Sample clocked (averaged) | 1 | 1 | Good | Good |
Considerations for Choosing Frequency Measurement Method
For all frequency measurement methods, assume the following:
fx | is the frequency to be measured if no error |
fk | is the known source or gate frequency |
measurement time (T) | is the time it takes to measure a single sample |
Divide down (N) | is the integer to divide down measured frequency, only used in large range two counters |
fs | is the sample clock rate, only used in sample clocked frequency measurements |
Here is how these variables apply to each method, with a summary in the following table:
Variable | One Counter | Two Counter | |
---|---|---|---|
High Frequency | Large Range | ||
f k | Known timebase | Known timebase | |
Measurement time | gating period | ||
Max. frequency error | f k | ||
Max. error % |
For a practical example, consider measuring a 50 kHz signal. Assuming that the measurement times for the sample clocked (with averaging) and two counter frequency measurements are configured the same, the following table summarizes the results.
Variable | One Counter | Two Counter | |
---|---|---|---|
High Frequency | Large Range | ||
fx | 50,000 | 50,000 | 50,000 |
f k | 100 M | 1,000 | 100 M |
Measurement time (ms) | .02 | 1 | 1 |
N | — | — | 50 |
Max. frequency error (Hz) | 25 | 1,000 | .5 |
Max. error % | .05 | 2 | .001 |
From these results, you can see that while the measurement time for one counter is shorter, the accuracy is best in the sample clocked and two counter large range measurements. For another example, the following table shows the results for 5 MHz.
Variable | One Counter | Two Counter | |
---|---|---|---|
High Frequency | Large Range | ||
fx | 5 M | 5 M | 5 M |
f k | 100 M | 1,000 | 100 M |
Measurement time (ms) | .0002 | 1 | 1 |
N | — | — | 5,000 |
Max. Frequency error (Hz) | 263 k | 1,000 | 50 |
Max. Error % | 5.26 | .02 | .001 |
The following table summarizes some of the differences in methods of measuring frequency.
Method | Number of Counters Used | Number of Measurements Returned | Measures High Frequency Signals Accurately | Measures Low Frequency Signals Accurately |
---|---|---|---|---|
Low frequency with one counter | 1 | 1 | Poor | Good |
High frequency with two counters | 1 or 2 | 1 | Good | Poor |
Large range of frequencies with two counters | 2 | 1 | Good | Good |
Period Measurement
In period measurements, the counter measures a period on its Gate input signal after the counter is armed. You can configure the counter to measure the period between two rising edges or two falling edges of the Gate input signal.
You can route an internal or external periodic clock signal (with a known period) to the Source input of the counter. The counter counts the number of rising (or falling) edges occurring on the Source input between the two active edges of the Gate signal.
You can calculate the period of the Gate input by multiplying the period of the Source signal by the number of edges returned by the counter.
Period measurements return the inverse results of frequency measurements.
Position Measurement
You can use the counters to perform position measurements with quadrature encoders or two-pulse encoders. You can measure angular position with X1, X2, and X4 angular encoders. Linear position can be measured with two-pulse encoders.
You can choose to do either a single point (on-demand) position measurement or a buffered (sample clock) position measurement. You must arm a counter to begin position measurements.
Measurements Using Quadrature Encoders with X1, X2, or X3 encoding
The counters can perform measurements of quadrature encoders that use X1, X2, or X4 encoding. A quadrature encoder can have up to three channels—channels A, B, and Z.
X1 Encoding
When channel A leads channel B in a quadrature cycle, the counter increments. When channel B leads channel A in a quadrature cycle, the counter decrements. The amount of increments and decrements per cycle depends on the type of encoding—X1, X2, or X4.
The following figure shows a quadrature cycle and the resulting increments and decrements for X1 encoding. When channel A leads channel B, the increment occurs on the rising edge of channel A. When channel B leads channel A, the decrement occurs on the falling edge of channel A.
X2 Encoding
The same behavior holds for X2 encoding except the counter increments or decrements on each edge of channel A, depending on which channel leads the other. Each cycle results in two increments or decrements, as shown in the following figure.
X4 Encoding
Similarly, the counter increments or decrements on each edge of channels A and B for X4 encoding. Whether the counter increments or decrements depends on which channel leads the other. Each cycle results in four increments or decrements, as shown in the following figure.
Channel Z Behavior
Some quadrature encoders have a third channel, channel Z, which is also referred to as the index channel. A high level on channel Z causes the counter to be reloaded with a specified value in a specified phase of the quadrature cycle. You can program the counter reload to occur in any one of the four phases in a quadrature cycle.
Channel Z behavior—when it goes high and how long it stays high—differs with quadrature encoder designs. You must refer to the documentation for your quadrature encoder to obtain timing of channel Z with respect to channels A and B. You must then ensure that channel Z is high during at least a portion of the phase you specify for reload. For instance, in Figure 7-21, channel Z is never high when channel A is high and channel B is low. Thus, the reload must occur in some other phase.
In the following figure, the reload phase is when both channel A and channel B are low. The reload occurs when the phase is true and channel Z is high. Incrementing and decrementing takes priority over reloading. Thus, when the channel B goes low to enter the reload phase, the increment occurs first. The reload occurs within one maximum timebase period after the reload phase becomes true. After the reload occurs, the counter continues to count as before. The following figure illustrates channel Z reload with X4 decoding.
Measurements Using Two Pulse Encoders
The counter supports two pulse encoders that have two channels—channels A and B.
The counter increments on each rising edge of channel A. The counter decrements on each rising edge of channel B, as shown in the following figure.
Buffered (Sample Clock) Position Measurement
With buffered position measurement (position measurement using a sample clock), the counter increments based on the encoding used after the counter is armed.
The value of the counter is sampled on each active edge of a sample clock. A DMA controller transfers the sampled values to host memory. The count values returned are the cumulative counts since the counter armed event; that is, the sample clock does not reset the counter. You can route the counter sample clock to the Gate input of the counter. You can configure the counter to sample on the rising or falling edge of the sample clock.
The following figure shows an example of a buffered X1 position measurement.
Two-Signal Edge-Separation Measurement
Two-signal edge-separation measurement is similar to pulse-width measurement, except that there are two measurement signals—Aux and Gate.
An active edge on the Aux input starts the counting and an active edge on the Gate input stops the counting. You must arm a counter to begin a two edge separation measurement.
After the counter has been armed and an active edge occurs on the Aux input, the counter counts the number of rising (or falling) edges on the Source. The counter ignores additional edges on the Aux input.
The counter stops counting upon receiving an active edge on the Gate input. The counter stores the count in the FIFO.
You can configure the rising or falling edge of the Aux input to be the active edge. You can configure the rising or falling edge of the Gate input to be the active edge.
Use this measurement type to count events or measure the time that occurs between edges on two signals. This type of measurement is sometimes referred to as start/stop trigger measurement, second gate measurement, or A-to-B measurement.
Single Two-Signal Edge-Separation Measurement
With single two-signal edge-separation measurement, the counter counts the number of rising (or falling) edges on the Source input occurring between an active edge of the Gate signal and an active edge of the Aux signal.
The counter then stores the count in the FIFO and ignores other edges on its inputs. Software then reads the stored count. The following figure shows an example of a single two-signal edge-separation measurement.
Implicit Buffered Two-Signal Edge-Separation Measurement
Implicit buffered and single two-signal edge-separation measurements are similar, but implicit buffered measurement measures multiple intervals.
The counter counts the number of rising (or falling) edges on the Source input occurring between an active edge of the Gate signal and an active edge of the Aux signal. The counter then stores the count in the FIFO. On the next active edge of the Gate signal, the counter begins another measurement. A DMA controller transfers the stored values to host memory. The following figure shows an example of an implicit buffered two-signal edge-separation measurement.
Sample Clocked Buffered Two-Signal Separation Measurement
A sample clocked buffered two-signal separation measurement is similar to single two-signal separation measurement, but buffered two-signal separation measurement takes measurements over multiple intervals correlated to a sample clock.
The counter counts the number of rising (or falling) edges on the Source input occurring between an active edge of the Gate signal and an active edge of the Aux signal. The counter then stores the count in the FIFO on a sample clock edge. On the next active edge of the Gate signal, the counter begins another measurement. A DMA controller transfers the stored values to host memory.
The following figure shows an example of a sample clocked buffered two-signal separation measurement.