You are on page 1of 59

TOPICS

Lesson 9
Counters
A. Counter Overview
ni.com/training
A. Counter Overview
B. Edge Counting
C. Pulse Generation
D. Pulse Measurement
E. Frequency Measurement
F. Position Measurement
What is a Counter?
Two basic functions
To count based on the comparison of input signals (Gate, Source)
To generate pulses based upon inputs and register values
Many applications are derived from basic counting
ni.com/training
Many applications are derived from basic counting
Edge Counting such as Simple Edge Counting and Time
Measurement
Pulse, Semi-Period, and Period Width Measurement of Input Signal
Frequency Measurement
Single Pulse and Pulse Train Generation
Position & Velocity Measurement
Out
Gate
Source
Count Reg
Counter Signals
Counters accept and generate TTL signals
+5.0 V
high
Maximum Rise/Fall Time = 50 ns
ni.com/training
+0.8 V
0 V
low
+2.0 V
high
indeterminate
Minimum Pulse Width = 10 ns
Parts of a Counter
Count Register
Stores the current count
Source
Input signal that changes the current count
Active edge (rising or falling) of input signal changes the count
Gate
Source
Out
Count Register
ni.com/training
Active edge (rising or falling) of input signal changes the count
Choose if count increments or decrements on an active edge
Gate
Input signal that controls when counting occurs
Counting can occur when gate is high, low, or between various
combinations of rising and falling edges
Out
Output signal used to generate pulses
Counter Pins
Counter gate and source are PFI pins
PFI stands for Programmable Function Input
Allows use of one pin for multiple applications
Example: Use pin 3 as
ni.com/training
Example: Use pin 3 as
digital trigger for
analog input
and counter gate
Source and Gate Selectors in DAQmx
Source and gate selection offers a great deal of flexibility
The source and gate pins for counters can be used for multiple
applications
You only need to specify the input terminal for your
ni.com/training
measurement and DAQmx will connect it to the appropriate
source or gate depending on the application
Counter signals can be input on any PFI pins
DAQ Assistant will give you the default PFI pin for the
application
Counter Terminology
Terminal Count
Value of the last count before a counter rolls over to 0
Resolution
The size of the counter register specified in bits
ni.com/training
The size of the counter register specified in bits
Counter register size = 2
(resolution)
1
Typical resolutions - 16, 24, 32 bit
Timebase
Internal signal that can be routed to the source
Common timebases 100kHz, 20MHz
Edge Counting
Types of Edge Counting
Simple
Pause Trigger (Gated)
Continuous Buffered
ni.com/training
Continuous Buffered
Finite Buffered
Edge Counting
Active edges on source
signal increment the count
Active edge can be either rising
or falling
Known frequency timebase
ni.com/training
Known frequency timebase
Time elapsed =
(Count) x (timebase period)
Edge Counting Simple
Count will increment for the specified edge on the source
You can change active edge to falling
Counter will roll over when it reaches terminal count
Terminal count = 2
(Counter resolution)
1
ni.com/training
Terminal count = 2
(Counter resolution)
1
0 1 2 3 0 TC-1 TC
Counter Armed
Source
Count
Edge Counting DAQ Assistant
Select Count Direction:
Count up
Select Rising or
Falling Edge
ni.com/training
Automatic PFI line
selection
Count up
Count down
Externally controlled
Edge Counting Simple
Use the Counter instance of the DAQmx Read VI
ni.com/training
Start
Task
Count
Edges
Clear
Task
Exercise 9-1: Simple Edge Counting
ni.com/training
G
O
A
L
To create a VI to count the number of edges produces by
rotating the knob of the Quadrature encoder.
Edge Counting Pause Trigger (Gated)
Non-buffered
Gate pauses increment/decrement of the count register
Active edges on source increment register only when gate is enabled
ni.com/training
Source
Gate
Source
Out
Count Register
Edge Counting Pause Trigger (Gated)
Counter increments or decrements when gate is high or low
(parameters are software selectable)
Counter will pause counting while in the non-active state
Counter Armed
ni.com/training
0 0 1 2
Counter Armed
Source
Count
Gate
2 2 3 4
Edge Counting Pause Trigger Example
Pause trigger is set with DAQmx
Trigger property node
ni.com/training
Create Virtual Channel Start
Task
Count
Edges
Clear
Task
Edge Counting Buffered
Gate latches current count into count register (onboard memory)
A DMA or interrupt then transfers value to PC memory
Active edges on source increment register
ni.com/training
Source
Gate
Source
Out
Count Register
Edge Counting Finite Buffered
Use the DAQmx Timing VI to configure the sample clock for
finite samples
Counter stops reading when the Samples per Channel value
has been reached
ni.com/training
has been reached
Edge Counting Continuous Buffered
Active edge of gate latches the count into PC memory
ni.com/training
Edge Counting Continuous Buffered
Use the DAQmx Timing VI
Sample Clock Source determines when a sample will be
inserted into the buffer
ni.com/training
Exercise 9-2: Advanced Edge Counting
ni.com/training
G
O
A
L
To use pause trigger, and finite buffered methods to
perform edge counting.
Pulse Generation
Generates a TTL signal on the counters out pin
Single Pulse Pulse Train
ni.com/training
Single Pulse or
Pulse Train
Timebase
Gate
Source
Out
Count Register
Pulse Characteristics
Pulse Polarity
Delay Width
High Polarity Low Polarity
Delay Width
Pulse Period = Delay + Width
ni.com/training
Pulse Period = Delay + Width
Pulse Frequency =
1
Pulse Period
Duty Cycle =
Width
Pulse Period
DAQmx Create Virtual Channel
Frequency Instance Time Instance Ticks Instance
ni.com/training
Frequency Instance Time Instance Ticks Instance
Single Pulse Generation
ni.com/training
Create Virtual Channel:
Select Frequency,
Time, or Ticks
Start
Task
Wait Until
Done
Clear
Task
Exercise 9-3: Pulse Generation
ni.com/training
G
O
A
L
To create a VI to generate a pulse.
Pulse Train Generation
Pulse Train Generates more than one pulse
Initial Delay How long the output remains in the idle state
before generation
High Time Amount of time the pulse is at a high level (5V)
ni.com/training
High Time Amount of time the pulse is at a high level (5V)
Low Time Amount of time the pulse is at a low level (0V)
One can generate either a finite or a continuous pulse train
Pulse Train Generation
ni.com/training
Timing VI:
Implicit and
continuous
Start
Task
Is Task
Done?
Clear
Task
Exercise 9-4: Pulse Train Generation
ni.com/training
G
O
A
L
To create a VI to generate a pulse train.
Finite Pulse Train
Requires the use of 2 counters
First counter generates continuous pulse train.
Second counter generates pulse that gates first counter.
Pulse width determines length of pulse train
ni.com/training
Pulse width determines length of pulse train
DAQmx uses pulse train settings to determine gating pulse
width
Everything is managed by DAQmx
User simply specifies where counter output is generated
Retriggerable Finite Pulse Train
Similar to a Finite Pulse Train
The counter pulse used for gating is retriggerable
Can use this property to create a retriggerable finite pulse train
Set with DAQmx Trigger Property Node
ni.com/training
Set with DAQmx Trigger Property Node
DAQmx Trigger Property Node
used for Retriggerable Operations
Exercise 9-5: Retriggerable Pulse Train Generation
ni.com/training
G
O
A
L
To create a VI to generate a retriggerable finite pulse train.
Pulse Measurement
Use a timebase with a known frequency to measure
characteristics of a unknown signal
Period Measurement Pulse Width Measurement
ni.com/training
Pulse Period Width
Source
Gate
Source
Out
Count Register
Pulse Width Measurement
Count will increment for each rising edge on source
Counting can start on either rising or falling edge
Width of Gate = (Count) x (1/source frequency)
ni.com/training
0 1 2 2 2 2
Armed
Source
Count 2
Gate
Yes No
Single Pulse Width Measurement
Set Maximum and Minimum values of the unknown pulse as
accurately as possible. This allows NI-DAQmx to choose the
best internal timebase.
ni.com/training
Period Measurement
Count will increment for each rising edge on source
Counting can either start and end on rising or falling edges
Period of Gate = (Count) x (1/source frequency)
ni.com/training
0 1 2 3 4 4
Armed
Source
Count 4
Gate
Yes No
Single Period Measurement
Like Pulse Width Measurement, select Counter InputPeriod
from the Create Virtual Channel pull-down menu (or select
Period in the DAQ Assistant)
ni.com/training
Semi Period Measurement
Time between consecutive edges
Select Counter InputSemi Period from the Create Virtual
Channel pull-down menu (or select Semi Period in the DAQ
Assistant)
ni.com/training
Assistant)
Exercise 9-6: Period, Semi Period, and Pulse
Width Measurement
ni.com/training
G
O
A
L
To create a VI to measure the period, semi period, and
pulse width of a digital signal.
Frequency Measurements
Three ways to measure frequency
Depends on signal frequency and required accuracy
Measure period and take the inverse
Good for low frequencies (depends on allowable error)
Uses 1 counter
ni.com/training
Uses 1 counter
Count number of edges during a known period of time
Good for high frequencies (depends on allowable error)
Utilizes 2 counters, DAQmx automatically reserves the second
Divide down the frequency
Good for varying frequencies
Utilizes 2 counters, DAQmx automatically reserves the second
Frequency Measurement Inverse Period
Use period measurement and take the inverse
Frequency = 1 / Period
Gate
Source
Count Reg
Internal Timebase
or other known
frequency
Unknown
Frequency
ni.com/training
Pros Cons
Only uses 1 counter
Good at low
frequencies
(f < timebase / 100)
Can see large error
at high frequencies
due to phenomenon
called synchronization
error
frequency
Frequency Measurement Inverse Period
Specify the counter input frequency measurement as the
application with Low Frequency with 1 Counter selected.
This is an example of a non-buffered, single measurement
ni.com/training
Frequency Measurement - Averaging
Count the number of edges of the unknown signal during a
known period of time (measurement time)
The longer the period of the known pulse, the less
significant the synchronization error
Unknown Frequency is calculated by dividing the number of
ni.com/training
Unknown Frequency is calculated by dividing the number of
pulses by the known period
Out
Gate
Source
Signal being measured
CTR 0: Pulse Train Generation CTR 1: Period Measurement
CTR 0 CTR 1
Source
Internal timebase
Specify the counter input frequency measurement as the
application with High Frequency with 2 Counters selected
Measurement Time specifies length of time for the measurement
DAQmx Timing.viImplicit
Frequency Measurement - Averaging
ni.com/training
Frequency Measurement Divide Down
Divide the unknown signal down to a slower frequency
Perform period measurement on that slower frequency
Multiply by divisor value to obtain the correct frequency
The larger the divisor, the slower it takes to perform period
ni.com/training
The larger the divisor, the slower it takes to perform period
measurement and the more accurate the measurement
Out
Gate
Source
Internal timebase
CTR 0: Pulse Train Generation CTR 1: Period Measurement
CTR 0 CTR 1
Source
Signal being measured
Frequency Measurement Divide Down
Specify the counter input frequency measurement as the
application with Large Range with 2 Counters selected
Divisor specifies value to divide the input signal by an integer
DAQmx Timing.vi Implicit
ni.com/training
Synchronization Error
Gate period is exactly four source cycles
Measurement could be off by +/- 1 source cycles
GATE
SOURCE
ni.com/training
SOURCE
Miss both edges 0 1 2 3 3
0 1 2 3 4
Miss one, catch one
1 2 3 4 5
Catch both edges
Actual
Frequency
Number
of 50 ns
Cycles
Measurement
Error of
+1 Cycle
Measurement
Error of
-1 Cycle
Frequency
with Error of
+1 Cycle
Frequency
with Error of
-1 Cycle
50 kHz 400 401 399 49.88 kHz 50.13 kHz
5 MHz 4 5 3 4 MHz 6.67 MHz
Effect of Synchronization Error
ni.com/training
5 MHz 4 5 3 4 MHz 6.67 MHz
A signal with frequency F will have error 0.x
F = F
SOURCE
- (F
SOURCE
/(1 + 0.x))
If frequency exceeds acceptable error, use the Averaging
Method
Exercise 9-7: Frequency Measurement
ni.com/training
G
O
A
L
To create a VI to measure the frequency of an unknown
signal.
Position Measurement
With a transducer called a
quadrature encoder, you can
measure position
DAQ Signal Accessory has a
ni.com/training
DAQ Signal Accessory has a
quadrature encoder
NI-TIO and NI-STC2 are the only
counter chips that directly support
quadrature encoders
How Does an Encoder Work?
Shaft and disk rotate
Code track either passes
or blocks light to sensor
Light sensor creates two
pulse trains
Light Source
Light Sensor
Quadrature Encoder
ni.com/training
pulse trains
Shaft
Rotating Disk
Code Track
Channel B
Channel A
Quadrature Encoder
Quadrature Encoders produce
two pulse trains 90 degrees out
of phase
Clockwise rotation
Channel A leads Channel B
90
0
phase difference
Channel B
Channel A
Clockwise Rotation
ni.com/training
Channel A leads Channel B
Counter-Clockwise rotation
Channel B leads Channel A
Channel B
90
0
phase difference
Channel B
Channel A
Counter-Clockwise Rotation
Up/Down Line
DAQ counters also have an up/down line
DIO6 is up/down for counter 0
DIO7 is up/down for counter 1
TTL High = Count up
ni.com/training
TTL High = Count up
TTL Low = Count down
Gate
Source
Out
Count Register U/D
DAQ-STC and Encoders
Count on the falling edge of Channel A
Clockwise - B is high so count increments
Counter-Clockwise - B is low so count decrements
ni.com/training
Channel B is hardwired to DIO6 on the DAQ Signal Accessory
Channel B
Channel A
Clockwise Rotation
Channel B
Channel A
Counter-Clockwise Rotation
DAQ-STC and Encoders
Gate Out
Count Register AUX
Simple Event Counting
Channel B
ni.com/training
Source
Count Register AUX
Channel A
DAQ-STC Position Measurements
Set the count direction to Externally Controlled
Polarity on DIO 6 and DIO 7 controls counting direction on
CTR 0 and CTR 1
Count edges as usual
ni.com/training
Count edges as usual
Continue by
programming a
finite or
continuous read
Exercise 9-8: Quadrature Encoder
ni.com/training
G
O
A
L
To create a VI to measure position of the Quadrature
encoder.
Quiz
1. Which of the following are
components of a counter?
a. Source
b. Gate
2. What is the terminal count
of a 24-bit counter?
ni.com/training
b. Gate
c. Multiplexer
d. Register
e. Output
Quiz
3. Explain the difference
between the 3 methods of
measuring a counter
frequency and why some
methods need 2 counters.
4. ______________ is the
name of the error that occurs
when the frequency being
measured approaches the
timebase of the measurement
ni.com/training
methods need 2 counters. timebase of the measurement
device?

You might also like