Skip to content

Commit 7db3ca0

Browse files
authored
Add Counter Output examples (#583)
* Add Counter Output examples * reworked on comments * CtrFreq type hint and auto_start
1 parent 6ffec7b commit 7db3ca0

File tree

5 files changed

+105
-20
lines changed

5 files changed

+105
-20
lines changed

examples/counter_out/co_pulse_time.py

Lines changed: 0 additions & 20 deletions
This file was deleted.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
"""Example for continuously generating digital pulse train.
2+
3+
This example demonstrates how to generate a continuous digital
4+
pulse train from a Counter Output Channel. The Frequency, Duty
5+
Cycle, and Idle State are all configurable.
6+
"""
7+
8+
import nidaqmx
9+
from nidaqmx.constants import AcquisitionType, Level
10+
11+
with nidaqmx.Task() as task:
12+
channel = task.co_channels.add_co_pulse_chan_freq(
13+
"Dev1/ctr0", idle_state=Level.LOW, initial_delay=0.0, freq=1.0, duty_cycle=0.5
14+
)
15+
channel.co_pulse_term = "/Dev1/PFI12"
16+
task.timing.cfg_implicit_timing(sample_mode=AcquisitionType.CONTINUOUS)
17+
task.start()
18+
19+
input("Generating pulse train. Press Enter to stop.\n")
20+
21+
task.stop()
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
"""Example for continuously generating digital pulse train with external timing.
2+
3+
This example demonstrates how to generate a continuous buffered
4+
sample clocked digital pulse train from a Counter Output
5+
Channel. The Frequency, Duty Cycle, and Idle State are all
6+
configurable. The default data generated is a pulse train with a
7+
fixed frequency but a duty cycle that varies based on the Duty
8+
Cycle Max/Min and the signal type. The duty cycle will update
9+
with each sample clock edge.
10+
"""
11+
12+
import nidaqmx
13+
from nidaqmx.constants import AcquisitionType, Level
14+
from nidaqmx.types import CtrFreq
15+
16+
with nidaqmx.Task() as task:
17+
duty_min = 0.5
18+
duty_max = 0.8
19+
duty_step = (duty_max - duty_min) / 1000
20+
ctr_freq_data = [CtrFreq(1000, (duty_min + duty_step * i)) for i in range(1000)]
21+
22+
channel = task.co_channels.add_co_pulse_chan_freq(
23+
"Dev1/ctr0", idle_state=Level.LOW, initial_delay=0.0, freq=1.0, duty_cycle=0.5
24+
)
25+
channel.co_pulse_term = "/Dev1/PFI12"
26+
task.timing.cfg_samp_clk_timing(1000.0, "/Dev1/PFI0", sample_mode=AcquisitionType.CONTINUOUS)
27+
task.out_stream.output_buf_size = 1000
28+
task.write(ctr_freq_data)
29+
task.start()
30+
31+
input("Generating pulse train. Press Enter to stop.\n")
32+
33+
task.stop()
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
"""Example for continuously generating digital pulse train with implicit timing.
2+
3+
This example demonstrates how to generate a continuous buffered
4+
implicit timed digital pulse train from a Counter Output
5+
Channel. The Frequency, Duty Cycle, and Idle State are all
6+
configurable. The default data generated is a pulse train with a
7+
fixed frequency but a duty cycle that varies based on the Duty
8+
Cycle Max/Min and the signal type. The duty cycle will update
9+
with each sample generated.
10+
"""
11+
12+
import nidaqmx
13+
from nidaqmx.constants import AcquisitionType, Level
14+
from nidaqmx.types import CtrFreq
15+
16+
with nidaqmx.Task() as task:
17+
duty_min = 0.5
18+
duty_max = 0.8
19+
duty_step = (duty_max - duty_min) / 1000
20+
ctr_freq_data = [CtrFreq(1000, (duty_min + duty_step * i)) for i in range(1000)]
21+
22+
channel = task.co_channels.add_co_pulse_chan_freq(
23+
"Dev1/ctr0", idle_state=Level.LOW, initial_delay=0.0, freq=1.0, duty_cycle=0.5
24+
)
25+
channel.co_pulse_term = "/Dev1/PFI12"
26+
task.timing.cfg_implicit_timing(sample_mode=AcquisitionType.CONTINUOUS)
27+
task.write(ctr_freq_data)
28+
task.start()
29+
30+
input("Generating pulse train. Press Enter to stop.\n")
31+
32+
task.stop()
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
"""Example for generating a single digital pulse.
2+
3+
This example demonstrates how to generate a single digital pulse
4+
from a Counter Output Channel. The Initial Delay, High Time, Low
5+
Time, and Idle State are all configurable.
6+
"""
7+
8+
import nidaqmx
9+
from nidaqmx.constants import Level
10+
11+
with nidaqmx.Task() as task:
12+
channel = task.co_channels.add_co_pulse_chan_time(
13+
"Dev1/ctr0", idle_state=Level.LOW, initial_delay=0.0, low_time=0.5, high_time=1.0
14+
)
15+
channel.co_pulse_term = "/Dev1/PFI12"
16+
17+
task.start()
18+
task.wait_until_done(timeout=10)
19+
task.stop()

0 commit comments

Comments
 (0)